]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
entry: Stop setting state flags on the style context for drawing
[~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_STATE_FLAG_NORMAL;
2465
2466   if (!gtk_widget_is_sensitive (widget) || icon_info->insensitive)
2467     state |= GTK_STATE_FLAG_INSENSITIVE;
2468   else if (icon_info->prelight)
2469     state |= GTK_STATE_FLAG_PRELIGHT;
2470
2471   gtk_style_context_save (context);
2472
2473   gtk_style_context_set_state (context, state);
2474   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
2475 }
2476
2477 static gint
2478 get_icon_width (GtkEntry             *entry,
2479                 GtkEntryIconPosition  icon_pos)
2480 {
2481   GtkEntryPrivate *priv = entry->priv;
2482   EntryIconInfo *icon_info = priv->icons[icon_pos];
2483   GtkStyleContext *context;
2484   gint width;
2485
2486   if (!icon_info)
2487     return 0;
2488
2489   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
2490   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
2491   _gtk_icon_helper_get_size (icon_info->icon_helper, context,
2492                              &width, NULL);
2493   gtk_style_context_restore (context);
2494
2495   return width;
2496 }
2497
2498 static void
2499 get_icon_allocations (GtkEntry      *entry,
2500                       GtkAllocation *primary,
2501                       GtkAllocation *secondary)
2502
2503 {
2504   GtkEntryPrivate *priv = entry->priv;
2505   gint x, y, width, height;
2506
2507   get_text_area_size (entry, &x, &y, &width, &height);
2508
2509   if (gtk_widget_has_focus (GTK_WIDGET (entry)) && !priv->interior_focus)
2510     y += priv->focus_width;
2511
2512   primary->y = y;
2513   primary->height = height;
2514   primary->width = get_icon_width (entry, GTK_ENTRY_ICON_PRIMARY);
2515   if (primary->width > 0)
2516     primary->width += 2 * priv->icon_margin;
2517
2518   secondary->y = y;
2519   secondary->height = height;
2520   secondary->width = get_icon_width (entry, GTK_ENTRY_ICON_SECONDARY);
2521   if (secondary->width > 0)
2522     secondary->width += 2 * priv->icon_margin;
2523
2524   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
2525     {
2526       primary->x = x + width - primary->width;
2527       secondary->x = x;
2528     }
2529   else
2530     {
2531       primary->x = x;
2532       secondary->x = x + width - secondary->width;
2533     }
2534 }
2535
2536
2537 static void
2538 begin_change (GtkEntry *entry)
2539 {
2540   GtkEntryPrivate *priv = entry->priv;
2541
2542   priv->change_count++;
2543
2544   g_object_freeze_notify (G_OBJECT (entry));
2545 }
2546
2547 static void
2548 end_change (GtkEntry *entry)
2549 {
2550   GtkEditable *editable = GTK_EDITABLE (entry);
2551   GtkEntryPrivate *priv = entry->priv;
2552
2553   g_return_if_fail (priv->change_count > 0);
2554
2555   g_object_thaw_notify (G_OBJECT (entry));
2556
2557   priv->change_count--;
2558
2559   if (priv->change_count == 0)
2560     {
2561        if (priv->real_changed)
2562          {
2563            g_signal_emit_by_name (editable, "changed");
2564            priv->real_changed = FALSE;
2565          }
2566     }
2567 }
2568
2569 static void
2570 emit_changed (GtkEntry *entry)
2571 {
2572   GtkEditable *editable = GTK_EDITABLE (entry);
2573   GtkEntryPrivate *priv = entry->priv;
2574
2575   if (priv->change_count == 0)
2576     g_signal_emit_by_name (editable, "changed");
2577   else
2578     priv->real_changed = TRUE;
2579 }
2580
2581 static void
2582 gtk_entry_destroy (GtkWidget *widget)
2583 {
2584   GtkEntry *entry = GTK_ENTRY (widget);
2585   GtkEntryPrivate *priv = entry->priv;
2586
2587   priv->current_pos = priv->selection_bound = 0;
2588   _gtk_entry_reset_im_context (entry);
2589   gtk_entry_reset_layout (entry);
2590
2591   if (priv->blink_timeout)
2592     {
2593       g_source_remove (priv->blink_timeout);
2594       priv->blink_timeout = 0;
2595     }
2596
2597   if (priv->recompute_idle)
2598     {
2599       g_source_remove (priv->recompute_idle);
2600       priv->recompute_idle = 0;
2601     }
2602
2603   GTK_WIDGET_CLASS (gtk_entry_parent_class)->destroy (widget);
2604 }
2605
2606 static void
2607 gtk_entry_dispose (GObject *object)
2608 {
2609   GtkEntry *entry = GTK_ENTRY (object);
2610   GtkEntryPrivate *priv = entry->priv;
2611   GdkKeymap *keymap;
2612
2613   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2614   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2615   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2616   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2617
2618   if (priv->buffer)
2619     {
2620       buffer_disconnect_signals (entry);
2621       g_object_unref (priv->buffer);
2622       priv->buffer = NULL;
2623     }
2624
2625   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (object)));
2626   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
2627   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
2628   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2629 }
2630
2631 static void
2632 gtk_entry_finalize (GObject *object)
2633 {
2634   GtkEntry *entry = GTK_ENTRY (object);
2635   GtkEntryPrivate *priv = entry->priv;
2636   EntryIconInfo *icon_info = NULL;
2637   gint i;
2638
2639   for (i = 0; i < MAX_ICONS; i++)
2640     {
2641       if ((icon_info = priv->icons[i]) != NULL)
2642         {
2643           if (icon_info->target_list != NULL)
2644             {
2645               gtk_target_list_unref (icon_info->target_list);
2646               icon_info->target_list = NULL;
2647             }
2648
2649           g_clear_object (&icon_info->icon_helper);
2650
2651           g_slice_free (EntryIconInfo, icon_info);
2652           priv->icons[i] = NULL;
2653         }
2654     }
2655
2656   gtk_entry_set_completion (entry, NULL);
2657
2658   if (priv->cached_layout)
2659     g_object_unref (priv->cached_layout);
2660
2661   g_object_unref (priv->im_context);
2662
2663   if (priv->blink_timeout)
2664     g_source_remove (priv->blink_timeout);
2665
2666   if (priv->recompute_idle)
2667     g_source_remove (priv->recompute_idle);
2668
2669   g_free (priv->placeholder_text);
2670   g_free (priv->im_module);
2671
2672   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2673 }
2674
2675 static DisplayMode
2676 gtk_entry_get_display_mode (GtkEntry *entry)
2677 {
2678   GtkEntryPrivate *priv = entry->priv;
2679
2680   if (priv->visible)
2681     return DISPLAY_NORMAL;
2682
2683   if (priv->invisible_char == 0 && priv->invisible_char_set)
2684     return DISPLAY_BLANK;
2685
2686   return DISPLAY_INVISIBLE;
2687 }
2688
2689 static gchar*
2690 gtk_entry_get_display_text (GtkEntry *entry,
2691                             gint      start_pos,
2692                             gint      end_pos)
2693 {
2694   GtkEntryPasswordHint *password_hint;
2695   GtkEntryPrivate *priv;
2696   gunichar invisible_char;
2697   const gchar *start;
2698   const gchar *end;
2699   const gchar *text;
2700   gchar char_str[7];
2701   gint char_len;
2702   GString *str;
2703   guint length;
2704   gint i;
2705
2706   priv = entry->priv;
2707   text = gtk_entry_buffer_get_text (get_buffer (entry));
2708   length = gtk_entry_buffer_get_length (get_buffer (entry));
2709
2710   if (end_pos < 0)
2711     end_pos = length;
2712   if (start_pos > length)
2713     start_pos = length;
2714
2715   if (end_pos <= start_pos)
2716       return g_strdup ("");
2717   else if (priv->visible)
2718     {
2719       start = g_utf8_offset_to_pointer (text, start_pos);
2720       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2721       return g_strndup (start, end - start);
2722     }
2723   else
2724     {
2725       str = g_string_sized_new (length * 2);
2726
2727       /* Figure out what our invisible char is and encode it */
2728       if (!priv->invisible_char)
2729           invisible_char = priv->invisible_char_set ? ' ' : '*';
2730       else
2731           invisible_char = priv->invisible_char;
2732       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2733
2734       /*
2735        * Add hidden characters for each character in the text
2736        * buffer. If there is a password hint, then keep that
2737        * character visible.
2738        */
2739
2740       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2741       for (i = start_pos; i < end_pos; ++i)
2742         {
2743           if (password_hint && i == password_hint->position)
2744             {
2745               start = g_utf8_offset_to_pointer (text, i);
2746               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2747             }
2748           else
2749             {
2750               g_string_append_len (str, char_str, char_len);
2751             }
2752         }
2753
2754       return g_string_free (str, FALSE);
2755     }
2756 }
2757
2758 static void
2759 update_cursors (GtkWidget *widget)
2760 {
2761   GtkEntry *entry = GTK_ENTRY (widget);
2762   GtkEntryPrivate *priv = entry->priv;
2763   EntryIconInfo *icon_info = NULL;
2764   GdkDisplay *display;
2765   GdkCursor *cursor;
2766   gint i;
2767
2768   for (i = 0; i < MAX_ICONS; i++)
2769     {
2770       if ((icon_info = priv->icons[i]) != NULL)
2771         {
2772           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2773               icon_info->window != NULL)
2774             gdk_window_show_unraised (icon_info->window);
2775
2776           /* The icon windows are not children of the visible entry window,
2777            * thus we can't just inherit the xterm cursor. Slight complication 
2778            * here is that for the entry, insensitive => arrow cursor, but for 
2779            * an icon in a sensitive entry, insensitive => xterm cursor.
2780            */
2781           if (gtk_widget_is_sensitive (widget) &&
2782               (icon_info->insensitive || 
2783                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2784             {
2785               display = gtk_widget_get_display (widget);
2786               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2787               gdk_window_set_cursor (icon_info->window, cursor);
2788               g_object_unref (cursor);
2789             }
2790           else
2791             {
2792               gdk_window_set_cursor (icon_info->window, NULL);
2793             }
2794         }
2795     }
2796 }
2797
2798 static void
2799 realize_icon_info (GtkWidget            *widget, 
2800                    GtkEntryIconPosition  icon_pos)
2801 {
2802   GtkEntry *entry = GTK_ENTRY (widget);
2803   GtkEntryPrivate *priv = entry->priv;
2804   EntryIconInfo *icon_info = priv->icons[icon_pos];
2805   GdkWindowAttr attributes;
2806   gint attributes_mask;
2807
2808   g_return_if_fail (icon_info != NULL);
2809
2810   attributes.x = 0;
2811   attributes.y = 0;
2812   attributes.width = 1;
2813   attributes.height = 1;
2814   attributes.window_type = GDK_WINDOW_CHILD;
2815   attributes.wclass = GDK_INPUT_ONLY;
2816   attributes.event_mask = gtk_widget_get_events (widget);
2817   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2818                                 GDK_BUTTON_RELEASE_MASK |
2819                                 GDK_BUTTON1_MOTION_MASK |
2820                                 GDK_BUTTON3_MOTION_MASK |
2821                                 GDK_POINTER_MOTION_HINT_MASK |
2822                                 GDK_POINTER_MOTION_MASK |
2823                                 GDK_ENTER_NOTIFY_MASK |
2824                             GDK_LEAVE_NOTIFY_MASK);
2825   attributes_mask = GDK_WA_X | GDK_WA_Y;
2826
2827   icon_info->window = gdk_window_new (gtk_widget_get_window (widget),
2828                                       &attributes,
2829                                       attributes_mask);
2830   gdk_window_set_user_data (icon_info->window, widget);
2831
2832   gtk_widget_queue_resize (widget);
2833 }
2834
2835 static EntryIconInfo*
2836 construct_icon_info (GtkWidget            *widget, 
2837                      GtkEntryIconPosition  icon_pos)
2838 {
2839   GtkEntry *entry = GTK_ENTRY (widget);
2840   GtkEntryPrivate *priv = entry->priv;
2841   EntryIconInfo *icon_info;
2842
2843   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2844
2845   icon_info = g_slice_new0 (EntryIconInfo);
2846   priv->icons[icon_pos] = icon_info;
2847
2848   icon_info->icon_helper = _gtk_icon_helper_new ();
2849
2850   if (gtk_widget_get_realized (widget))
2851     realize_icon_info (widget, icon_pos);
2852
2853   return icon_info;
2854 }
2855
2856 static void
2857 gtk_entry_map (GtkWidget *widget)
2858 {
2859   GtkEntry *entry = GTK_ENTRY (widget);
2860   GtkEntryPrivate *priv = entry->priv;
2861   EntryIconInfo *icon_info = NULL;
2862   gint i;
2863
2864   GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2865
2866   gdk_window_show (priv->text_area);
2867
2868   for (i = 0; i < MAX_ICONS; i++)
2869     {
2870       if ((icon_info = priv->icons[i]) != NULL)
2871         {
2872           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) &&
2873               icon_info->window != NULL)
2874             gdk_window_show (icon_info->window);
2875         }
2876     }
2877
2878   update_cursors (widget);
2879 }
2880
2881 static void
2882 gtk_entry_unmap (GtkWidget *widget)
2883 {
2884   GtkEntry *entry = GTK_ENTRY (widget);
2885   GtkEntryPrivate *priv = entry->priv;
2886   EntryIconInfo *icon_info = NULL;
2887   gint i;
2888
2889   for (i = 0; i < MAX_ICONS; i++)
2890     {
2891       if ((icon_info = priv->icons[i]) != NULL)
2892         {
2893           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2894               icon_info->window != NULL)
2895             gdk_window_hide (icon_info->window);
2896         }
2897     }
2898
2899   gdk_window_hide (priv->text_area);
2900
2901   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2902 }
2903
2904 static void
2905 gtk_entry_realize (GtkWidget *widget)
2906 {
2907   GtkEntry *entry;
2908   GtkEntryPrivate *priv;
2909   EntryIconInfo *icon_info;
2910   GdkWindow *window;
2911   GdkWindowAttr attributes;
2912   gint attributes_mask;
2913   gint frame_x, frame_y;
2914   int i;
2915
2916   gtk_widget_set_realized (widget, TRUE);
2917   window = gtk_widget_get_parent_window (widget);
2918   gtk_widget_set_window (widget, window);
2919   g_object_ref (window);
2920
2921   entry = GTK_ENTRY (widget);
2922   priv = entry->priv;
2923
2924   attributes.window_type = GDK_WINDOW_CHILD;
2925   attributes.wclass = GDK_INPUT_ONLY;
2926   attributes.event_mask = gtk_widget_get_events (widget);
2927   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2928                             GDK_BUTTON_RELEASE_MASK |
2929                             GDK_BUTTON1_MOTION_MASK |
2930                             GDK_BUTTON3_MOTION_MASK |
2931                             GDK_POINTER_MOTION_HINT_MASK |
2932                             GDK_POINTER_MOTION_MASK |
2933                             GDK_ENTER_NOTIFY_MASK |
2934                             GDK_LEAVE_NOTIFY_MASK);
2935   attributes_mask = GDK_WA_X | GDK_WA_Y;
2936
2937   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2938
2939   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
2940   attributes.x += frame_x;
2941   attributes.y += frame_y;
2942
2943   if (gtk_widget_is_sensitive (widget))
2944     {
2945       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2946       attributes_mask |= GDK_WA_CURSOR;
2947     }
2948
2949   priv->text_area = gdk_window_new (gtk_widget_get_window (widget),
2950                                     &attributes,
2951                                     attributes_mask);
2952
2953   gdk_window_set_user_data (priv->text_area, entry);
2954
2955   if (attributes_mask & GDK_WA_CURSOR)
2956     g_object_unref (attributes.cursor);
2957
2958   gtk_im_context_set_client_window (priv->im_context, priv->text_area);
2959
2960   gtk_entry_adjust_scroll (entry);
2961   gtk_entry_update_primary_selection (entry);
2962
2963
2964   /* If the icon positions are already setup, create their windows.
2965    * Otherwise if they don't exist yet, then construct_icon_info()
2966    * will create the windows once the widget is already realized.
2967    */
2968   for (i = 0; i < MAX_ICONS; i++)
2969     {
2970       if ((icon_info = priv->icons[i]) != NULL)
2971         {
2972           if (icon_info->window == NULL)
2973             realize_icon_info (widget, i);
2974         }
2975     }
2976 }
2977
2978 static void
2979 gtk_entry_unrealize (GtkWidget *widget)
2980 {
2981   GtkEntry *entry = GTK_ENTRY (widget);
2982   GtkEntryPrivate *priv = entry->priv;
2983   GtkClipboard *clipboard;
2984   EntryIconInfo *icon_info;
2985   gint i;
2986
2987   gtk_entry_reset_layout (entry);
2988   
2989   gtk_im_context_set_client_window (priv->im_context, NULL);
2990
2991   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2992   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2993     gtk_clipboard_clear (clipboard);
2994   
2995   if (priv->text_area)
2996     {
2997       gdk_window_set_user_data (priv->text_area, NULL);
2998       gdk_window_destroy (priv->text_area);
2999       priv->text_area = NULL;
3000     }
3001
3002   if (priv->popup_menu)
3003     {
3004       gtk_widget_destroy (priv->popup_menu);
3005       priv->popup_menu = NULL;
3006     }
3007
3008   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
3009
3010   for (i = 0; i < MAX_ICONS; i++)
3011     {
3012       if ((icon_info = priv->icons[i]) != NULL)
3013         {
3014           if (icon_info->window != NULL)
3015             {
3016               gdk_window_destroy (icon_info->window);
3017               icon_info->window = NULL;
3018             }
3019         }
3020     }
3021 }
3022
3023 void
3024 _gtk_entry_get_borders (GtkEntry *entry,
3025                         gint     *xborder,
3026                         gint     *yborder)
3027 {
3028   GtkEntryPrivate *priv = entry->priv;
3029   GtkWidget *widget = GTK_WIDGET (entry);
3030
3031   if (priv->has_frame)
3032     {
3033       GtkStyleContext *context;
3034       GtkBorder padding;
3035
3036       context = gtk_widget_get_style_context (widget);
3037       gtk_style_context_get_padding (context, 0, &padding);
3038
3039       *xborder = padding.left;
3040       *yborder = padding.top;
3041     }
3042   else
3043     {
3044       *xborder = 0;
3045       *yborder = 0;
3046     }
3047
3048   if (!priv->interior_focus)
3049     {
3050       *xborder += priv->focus_width;
3051       *yborder += priv->focus_width;
3052     }
3053 }
3054
3055 static void
3056 gtk_entry_get_preferred_width (GtkWidget *widget,
3057                                gint      *minimum,
3058                                gint      *natural)
3059 {
3060   GtkEntry *entry = GTK_ENTRY (widget);
3061   GtkEntryPrivate *priv = entry->priv;
3062   PangoFontMetrics *metrics;
3063   gint xborder, yborder;
3064   GtkBorder inner_border;
3065   PangoContext *context;
3066   GtkStyleContext *style_context;
3067   GtkStateFlags state;
3068   gint icon_widths = 0;
3069   gint icon_width, i;
3070   gint width;
3071
3072   context = gtk_widget_get_pango_context (widget);
3073
3074   style_context = gtk_widget_get_style_context (widget);
3075   state = gtk_widget_get_state_flags (widget);
3076
3077   metrics = pango_context_get_metrics (context,
3078                                        gtk_style_context_get_font (style_context, state),
3079                                        pango_context_get_language (context));
3080
3081   _gtk_entry_get_borders (entry, &xborder, &yborder);
3082   _gtk_entry_effective_inner_border (entry, &inner_border);
3083
3084   if (priv->width_chars < 0)
3085     width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
3086   else
3087     {
3088       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
3089       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3090       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
3091
3092       width = char_pixels * priv->width_chars + xborder * 2 + inner_border.left + inner_border.right;
3093     }
3094
3095   for (i = 0; i < MAX_ICONS; i++)
3096     {
3097       icon_width = get_icon_width (entry, i);
3098       if (icon_width > 0)
3099         icon_widths += icon_width + 2 * priv->icon_margin;
3100     }
3101
3102   if (icon_widths > width)
3103     width += icon_widths;
3104
3105   pango_font_metrics_unref (metrics);
3106
3107   *minimum = width;
3108   *natural = width;
3109 }
3110
3111 static void
3112 gtk_entry_get_preferred_height (GtkWidget *widget,
3113                                 gint      *minimum,
3114                                 gint      *natural)
3115 {
3116   GtkEntry *entry = GTK_ENTRY (widget);
3117   GtkEntryPrivate *priv = entry->priv;
3118   PangoFontMetrics *metrics;
3119   gint xborder, yborder;
3120   GtkBorder inner_border;
3121   GtkStyleContext *style_context;
3122   GtkStateFlags state;
3123   PangoContext *context;
3124   gint height;
3125
3126   context = gtk_widget_get_pango_context (widget);
3127
3128   style_context = gtk_widget_get_style_context (widget);
3129   state = gtk_widget_get_state_flags (widget);
3130
3131   metrics = pango_context_get_metrics (context,
3132                                        gtk_style_context_get_font (style_context, state),
3133                                        pango_context_get_language (context));
3134
3135   priv->ascent = pango_font_metrics_get_ascent (metrics);
3136   priv->descent = pango_font_metrics_get_descent (metrics);
3137
3138   _gtk_entry_get_borders (entry, &xborder, &yborder);
3139   _gtk_entry_effective_inner_border (entry, &inner_border);
3140
3141   height = PANGO_PIXELS (priv->ascent + priv->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
3142
3143   pango_font_metrics_unref (metrics);
3144
3145   *minimum = height;
3146   *natural = height;
3147 }
3148
3149 static void
3150 place_windows (GtkEntry *entry)
3151 {
3152   GtkWidget *widget = GTK_WIDGET (entry);
3153   GtkEntryPrivate *priv = entry->priv;
3154   gint x, y, width, height;
3155   gint frame_x, frame_y;
3156   GtkAllocation primary;
3157   GtkAllocation secondary;
3158   EntryIconInfo *icon_info = NULL;
3159
3160   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
3161   get_text_area_size (entry, &x, &y, &width, &height);
3162   get_icon_allocations (entry, &primary, &secondary);
3163
3164   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3165     y += priv->focus_width;
3166
3167   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3168     x += secondary.width;
3169   else
3170     x += primary.width;
3171   width -= primary.width + secondary.width;
3172
3173   x += frame_x;
3174   y += frame_y;
3175   primary.x += frame_x;
3176   primary.y += frame_y;
3177   secondary.x += frame_x;
3178   secondary.y += frame_y;
3179
3180   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
3181     gdk_window_move_resize (icon_info->window,
3182                             primary.x, primary.y,
3183                             primary.width, primary.height);
3184
3185   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
3186     gdk_window_move_resize (icon_info->window,
3187                             secondary.x, secondary.y,
3188                             secondary.width, secondary.height);
3189
3190   gdk_window_move_resize (priv->text_area, x, y, width, height);
3191 }
3192
3193 static void
3194 gtk_entry_get_text_area_size (GtkEntry *entry,
3195                               gint     *x,
3196                               gint     *y,
3197                               gint     *width,
3198                               gint     *height)
3199 {
3200   GtkEntryPrivate *priv = entry->priv;
3201   GtkWidget *widget = GTK_WIDGET (entry);
3202   GtkAllocation allocation;
3203   GtkRequisition requisition;
3204   gint req_height;
3205   gint frame_height;
3206   gint xborder, yborder;
3207
3208   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3209   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3210
3211   gtk_widget_get_allocation (widget, &allocation);
3212   _gtk_entry_get_borders (entry, &xborder, &yborder);
3213
3214   if (gtk_widget_get_realized (widget))
3215     get_frame_size (entry, TRUE, NULL, NULL, NULL, &frame_height);
3216   else
3217     frame_height = req_height;
3218
3219   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3220     frame_height -= 2 * priv->focus_width;
3221
3222   if (x)
3223     *x = xborder;
3224
3225   if (y)
3226     *y = frame_height / 2 - (req_height - yborder * 2) / 2;
3227
3228   if (width)
3229     *width = allocation.width - xborder * 2;
3230
3231   if (height)
3232     *height = req_height - yborder * 2;
3233 }
3234
3235 static void
3236 get_text_area_size (GtkEntry *entry,
3237                     gint     *x,
3238                     gint     *y,
3239                     gint     *width,
3240                     gint     *height)
3241 {
3242   GtkEntryClass *class;
3243
3244   g_return_if_fail (GTK_IS_ENTRY (entry));
3245
3246   class = GTK_ENTRY_GET_CLASS (entry);
3247
3248   if (class->get_text_area_size)
3249     class->get_text_area_size (entry, x, y, width, height);
3250 }
3251
3252
3253 static void
3254 get_frame_size (GtkEntry *entry,
3255                 gboolean  relative_to_window,
3256                 gint     *x,
3257                 gint     *y,
3258                 gint     *width,
3259                 gint     *height)
3260 {
3261   GtkEntryPrivate *priv = entry->priv;
3262   GtkAllocation allocation;
3263   GtkRequisition requisition;
3264   GtkWidget *widget = GTK_WIDGET (entry);
3265   gint req_height;
3266
3267   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3268
3269   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3270
3271   gtk_widget_get_allocation (widget, &allocation);
3272
3273   if (x)
3274     *x = relative_to_window ? allocation.x : 0;
3275
3276   if (y)
3277     {
3278       if (priv->is_cell_renderer)
3279         *y = 0;
3280       else
3281         *y = (allocation.height - req_height) / 2;
3282
3283       if (relative_to_window)
3284         *y += allocation.y;
3285     }
3286
3287   if (width)
3288     *width = allocation.width;
3289
3290   if (height)
3291     {
3292       if (priv->is_cell_renderer)
3293         *height = allocation.height;
3294       else
3295         *height = req_height;
3296     }
3297 }
3298
3299 void
3300 _gtk_entry_effective_inner_border (GtkEntry  *entry,
3301                                    GtkBorder *border)
3302 {
3303   GtkBorder *tmp_border;
3304
3305   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
3306
3307   if (tmp_border)
3308     {
3309       *border = *tmp_border;
3310       return;
3311     }
3312
3313   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
3314
3315   if (tmp_border)
3316     {
3317       *border = *tmp_border;
3318       gtk_border_free (tmp_border);
3319       return;
3320     }
3321
3322   *border = default_inner_border;
3323 }
3324
3325 static void
3326 gtk_entry_size_allocate (GtkWidget     *widget,
3327                          GtkAllocation *allocation)
3328 {
3329   GtkEntry *entry = GTK_ENTRY (widget);
3330
3331   gtk_widget_set_allocation (widget, allocation);
3332
3333   if (gtk_widget_get_realized (widget))
3334     {
3335       GtkEntryCompletion* completion;
3336
3337       place_windows (entry);
3338       gtk_entry_recompute (entry);
3339
3340       completion = gtk_entry_get_completion (entry);
3341       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3342         _gtk_entry_completion_resize_popup (completion);
3343     }
3344 }
3345
3346 static gboolean
3347 should_prelight (GtkEntry             *entry,
3348                  GtkEntryIconPosition  icon_pos)
3349 {
3350   GtkEntryPrivate *priv = entry->priv;
3351   EntryIconInfo *icon_info = priv->icons[icon_pos];
3352   gboolean prelight;
3353
3354   if (!icon_info)
3355     return FALSE;
3356
3357   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3358     return FALSE;
3359
3360   if (icon_info->pressed)
3361     return FALSE;
3362
3363   gtk_widget_style_get (GTK_WIDGET (entry),
3364                         "icon-prelight", &prelight,
3365                         NULL);
3366
3367   return prelight;
3368 }
3369
3370 static void
3371 draw_icon (GtkWidget            *widget,
3372            cairo_t              *cr,
3373            GtkEntryIconPosition  icon_pos)
3374 {
3375   GtkEntry *entry = GTK_ENTRY (widget);
3376   GtkEntryPrivate *priv = entry->priv;
3377   EntryIconInfo *icon_info = priv->icons[icon_pos];
3378   gint x, y, width, height, pix_width, pix_height;
3379   GtkStyleContext *context;
3380
3381   if (!icon_info)
3382     return;
3383
3384   width = gdk_window_get_width (icon_info->window);
3385   height = gdk_window_get_height (icon_info->window);
3386
3387   /* size_allocate hasn't been called yet. These are the default values.
3388    */
3389   if (width == 1 || height == 1)
3390     return;
3391
3392   cairo_save (cr);
3393   gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3394
3395   context = gtk_widget_get_style_context (widget);
3396   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
3397   _gtk_icon_helper_get_size (icon_info->icon_helper, context, &pix_width, &pix_height);
3398
3399   x = MAX (0, (width  - pix_width) / 2);
3400   y = MAX (0, (height - pix_height) / 2);
3401
3402   _gtk_icon_helper_draw (icon_info->icon_helper,
3403                          context, cr,
3404                          x, y);
3405
3406   gtk_style_context_restore (context);
3407   cairo_restore (cr);
3408 }
3409
3410
3411 static void
3412 gtk_entry_draw_frame (GtkWidget       *widget,
3413                       GtkStyleContext *context,
3414                       cairo_t         *cr)
3415 {
3416   GtkEntry *entry = GTK_ENTRY (widget);
3417   GtkEntryPrivate *priv = entry->priv;
3418   gint x = 0, y = 0, width, height;
3419   gint frame_x, frame_y;
3420
3421   cairo_save (cr);
3422
3423   get_frame_size (GTK_ENTRY (widget), FALSE, &frame_x, &frame_y, &width, &height);
3424
3425   cairo_translate (cr, frame_x, frame_y);
3426
3427   /* Fix a problem with some themes which assume that entry->text_area's
3428    * width equals widget->window's width
3429    * http://bugzilla.gnome.org/show_bug.cgi?id=466000 */
3430   if (GTK_IS_SPIN_BUTTON (widget))
3431     {
3432       gint xborder, yborder;
3433
3434       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3435       _gtk_entry_get_borders (GTK_ENTRY (widget), &xborder, &yborder);
3436
3437       x -= xborder;
3438       width += xborder * 2;
3439     }
3440
3441   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3442     {
3443       x += priv->focus_width;
3444       y += priv->focus_width;
3445       width -= 2 * priv->focus_width;
3446       height -= 2 * priv->focus_width;
3447     }
3448
3449   gtk_render_background (context, cr,
3450                          x, y, width, height);
3451
3452   if (priv->has_frame)
3453     gtk_render_frame (context, cr,
3454                       x, y, width, height);
3455
3456   gtk_entry_draw_progress (widget, context, cr);
3457
3458   if (gtk_widget_has_visible_focus (widget) && !priv->interior_focus)
3459     {
3460       x -= priv->focus_width;
3461       y -= priv->focus_width;
3462       width += 2 * priv->focus_width;
3463       height += 2 * priv->focus_width;
3464
3465       gtk_render_focus (context, cr,
3466                         0, 0, width, height);
3467     }
3468
3469   cairo_restore (cr);
3470 }
3471
3472 static void
3473 get_progress_area (GtkWidget *widget,
3474                    gint       *x,
3475                    gint       *y,
3476                    gint       *width,
3477                    gint       *height)
3478 {
3479   GtkEntry *entry = GTK_ENTRY (widget);
3480   GtkEntryPrivate *private = entry->priv;
3481   GtkBorder *progress_border;
3482
3483   get_text_area_size (entry, x, y, width, height);
3484
3485   if (!private->interior_focus)
3486     {
3487       *x -= private->focus_width;
3488       *y -= private->focus_width;
3489       *width += 2 * private->focus_width;
3490       *height += 2 * private->focus_width;
3491     }
3492
3493   gtk_widget_style_get (widget, "progress-border", &progress_border, NULL);
3494
3495   if (progress_border)
3496     {
3497       *x += progress_border->left;
3498       *y += progress_border->top;
3499       *width -= progress_border->left + progress_border->right;
3500       *height -= progress_border->top + progress_border->bottom;
3501
3502       gtk_border_free (progress_border);
3503     }
3504
3505   if (private->progress_pulse_mode)
3506     {
3507       gdouble value = private->progress_pulse_current;
3508
3509       *x += (gint) floor(value * (*width));
3510       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3511     }
3512   else if (private->progress_fraction > 0)
3513     {
3514       gdouble value = private->progress_fraction;
3515
3516       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3517         {
3518           gint bar_width;
3519
3520           bar_width = floor(value * (*width) + 0.5);
3521           *x += *width - bar_width;
3522           *width = bar_width;
3523         }
3524       else
3525         {
3526           *width = (gint) floor(value * (*width) + 0.5);
3527         }
3528     }
3529   else
3530     {
3531       *width = 0;
3532       *height = 0;
3533     }
3534 }
3535
3536 static void
3537 gtk_entry_draw_progress (GtkWidget       *widget,
3538                          GtkStyleContext *context,
3539                          cairo_t         *cr)
3540 {
3541   GtkEntry *entry = GTK_ENTRY (widget);
3542   GtkEntryPrivate *private = entry->priv;
3543   gint x, y, width, height;
3544
3545   get_progress_area (widget, &x, &y, &width, &height);
3546
3547   if ((width <= 0) || (height <= 0))
3548     return;
3549
3550   gtk_style_context_save (context);
3551   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
3552   if (private->progress_pulse_mode)
3553     gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
3554
3555   gtk_render_activity (context, cr,
3556                        x, y, width, height);
3557
3558   gtk_style_context_restore (context);
3559 }
3560
3561 static gint
3562 gtk_entry_draw (GtkWidget *widget,
3563                 cairo_t   *cr)
3564 {
3565   GtkEntry *entry = GTK_ENTRY (widget);
3566   GtkStyleContext *context;
3567   GtkEntryPrivate *priv = entry->priv;
3568   int i;
3569
3570   context = gtk_widget_get_style_context (widget);
3571
3572   /* Draw entry_bg, shadow, progress and focus */
3573   gtk_entry_draw_frame (widget, context, cr);
3574
3575   /* Draw text and cursor */
3576   cairo_save (cr);
3577
3578   gtk_cairo_transform_to_window (cr, widget, priv->text_area);
3579
3580   if (priv->dnd_position != -1)
3581     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3582   
3583   gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3584
3585   /* When no text is being displayed at all, don't show the cursor */
3586   if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3587       gtk_widget_has_focus (widget) &&
3588       priv->selection_bound == priv->current_pos && priv->cursor_visible)
3589     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3590
3591   cairo_restore (cr);
3592
3593   /* Draw icons */
3594   for (i = 0; i < MAX_ICONS; i++)
3595     {
3596       EntryIconInfo *icon_info = priv->icons[i];
3597
3598       if (icon_info != NULL)
3599         draw_icon (widget, cr, i);
3600     }
3601
3602   return FALSE;
3603 }
3604
3605 static gint
3606 gtk_entry_enter_notify (GtkWidget *widget,
3607                         GdkEventCrossing *event)
3608 {
3609   GtkEntry *entry = GTK_ENTRY (widget);
3610   GtkEntryPrivate *priv = entry->priv;
3611   gint i;
3612
3613   for (i = 0; i < MAX_ICONS; i++)
3614     {
3615       EntryIconInfo *icon_info = priv->icons[i];
3616
3617       if (icon_info != NULL && event->window == icon_info->window)
3618         {
3619           if (should_prelight (entry, i))
3620             {
3621               icon_info->prelight = TRUE;
3622               gtk_widget_queue_draw (widget);
3623             }
3624
3625           break;
3626         }
3627     }
3628
3629     return FALSE;
3630 }
3631
3632 static gint
3633 gtk_entry_leave_notify (GtkWidget        *widget,
3634                         GdkEventCrossing *event)
3635 {
3636   GtkEntry *entry = GTK_ENTRY (widget);
3637   GtkEntryPrivate *priv = entry->priv;
3638   gint i;
3639
3640   for (i = 0; i < MAX_ICONS; i++)
3641     {
3642       EntryIconInfo *icon_info = priv->icons[i];
3643
3644       if (icon_info != NULL && event->window == icon_info->window)
3645         {
3646           /* a grab means that we may never see the button release */
3647           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3648             icon_info->pressed = FALSE;
3649
3650           if (should_prelight (entry, i))
3651             {
3652               icon_info->prelight = FALSE;
3653               gtk_widget_queue_draw (widget);
3654             }
3655
3656           break;
3657         }
3658     }
3659
3660   return FALSE;
3661 }
3662
3663 static void
3664 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3665                             gint     **ranges,
3666                             gint      *n_ranges)
3667 {
3668   gint start_char, end_char;
3669
3670   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3671     {
3672       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3673       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3674       const char *text = pango_layout_get_text (layout);
3675       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3676       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3677       gint real_n_ranges, i;
3678
3679       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3680
3681       if (ranges)
3682         {
3683           gint *r = *ranges;
3684           
3685           for (i = 0; i < real_n_ranges; ++i)
3686             {
3687               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3688               r[2 * i] = r[2 * i] / PANGO_SCALE;
3689             }
3690         }
3691       
3692       if (n_ranges)
3693         *n_ranges = real_n_ranges;
3694     }
3695   else
3696     {
3697       if (n_ranges)
3698         *n_ranges = 0;
3699       if (ranges)
3700         *ranges = NULL;
3701     }
3702 }
3703
3704 static gboolean
3705 in_selection (GtkEntry *entry,
3706               gint      x)
3707 {
3708   gint *ranges;
3709   gint n_ranges, i;
3710   gint retval = FALSE;
3711
3712   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3713
3714   for (i = 0; i < n_ranges; ++i)
3715     {
3716       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3717         {
3718           retval = TRUE;
3719           break;
3720         }
3721     }
3722
3723   g_free (ranges);
3724   return retval;
3725 }
3726               
3727 static gint
3728 gtk_entry_button_press (GtkWidget      *widget,
3729                         GdkEventButton *event)
3730 {
3731   GtkEntry *entry = GTK_ENTRY (widget);
3732   GtkEditable *editable = GTK_EDITABLE (widget);
3733   GtkEntryPrivate *priv = entry->priv;
3734   EntryIconInfo *icon_info = NULL;
3735   gint tmp_pos;
3736   gint sel_start, sel_end;
3737   gint i;
3738
3739   for (i = 0; i < MAX_ICONS; i++)
3740     {
3741       icon_info = priv->icons[i];
3742
3743       if (!icon_info || icon_info->insensitive)
3744         continue;
3745
3746       if (event->window == icon_info->window)
3747         {
3748           if (should_prelight (entry, i))
3749             {
3750               icon_info->prelight = FALSE;
3751               gtk_widget_queue_draw (widget);
3752             }
3753
3754           priv->start_x = event->x;
3755           priv->start_y = event->y;
3756           icon_info->pressed = TRUE;
3757
3758           if (!icon_info->nonactivatable)
3759             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3760
3761           return TRUE;
3762         }
3763     }
3764
3765   if (event->window != priv->text_area ||
3766       (priv->button && event->button != priv->button))
3767     return FALSE;
3768
3769   gtk_entry_reset_blink_time (entry);
3770
3771   priv->button = event->button;
3772   
3773   if (!gtk_widget_has_focus (widget))
3774     {
3775       priv->in_click = TRUE;
3776       gtk_widget_grab_focus (widget);
3777       priv->in_click = FALSE;
3778     }
3779
3780   tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
3781
3782   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
3783     {
3784       gtk_entry_do_popup (entry, event);
3785       priv->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3786
3787       return TRUE;
3788     }
3789   else if (event->button == 1)
3790     {
3791       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3792
3793       priv->select_words = FALSE;
3794       priv->select_lines = FALSE;
3795
3796       if (event->state &
3797           gtk_widget_get_modifier_mask (widget,
3798                                         GDK_MODIFIER_INTENT_EXTEND_SELECTION))
3799         {
3800           _gtk_entry_reset_im_context (entry);
3801
3802           if (!have_selection) /* select from the current position to the clicked position */
3803             sel_start = sel_end = priv->current_pos;
3804           
3805           if (tmp_pos > sel_start && tmp_pos < sel_end)
3806             {
3807               /* Truncate current selection, but keep it as big as possible */
3808               if (tmp_pos - sel_start > sel_end - tmp_pos)
3809                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3810               else
3811                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3812             }
3813           else
3814             {
3815               gboolean extend_to_left;
3816               gint start, end;
3817
3818               /* Figure out what click selects and extend current selection */
3819               switch (event->type)
3820                 {
3821                 case GDK_BUTTON_PRESS:
3822                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3823                   break;
3824                   
3825                 case GDK_2BUTTON_PRESS:
3826                   priv->select_words = TRUE;
3827                   gtk_entry_select_word (entry);
3828                   break;
3829                   
3830                 case GDK_3BUTTON_PRESS:
3831                   priv->select_lines = TRUE;
3832                   gtk_entry_select_line (entry);
3833                   break;
3834
3835                 default:
3836                   break;
3837                 }
3838
3839               start = MIN (priv->current_pos, priv->selection_bound);
3840               start = MIN (sel_start, start);
3841               
3842               end = MAX (priv->current_pos, priv->selection_bound);
3843               end = MAX (sel_end, end);
3844
3845               if (tmp_pos == sel_start || tmp_pos == sel_end)
3846                 extend_to_left = (tmp_pos == start);
3847               else
3848                 extend_to_left = (end == sel_end);
3849               
3850               if (extend_to_left)
3851                 gtk_entry_set_positions (entry, start, end);
3852               else
3853                 gtk_entry_set_positions (entry, end, start);
3854             }
3855         }
3856       else /* no shift key */
3857         switch (event->type)
3858         {
3859         case GDK_BUTTON_PRESS:
3860           if (in_selection (entry, event->x + priv->scroll_offset))
3861             {
3862               /* Click inside the selection - we'll either start a drag, or
3863                * clear the selection
3864                */
3865               priv->in_drag = TRUE;
3866               priv->drag_start_x = event->x + priv->scroll_offset;
3867               priv->drag_start_y = event->y;
3868             }
3869           else
3870             gtk_editable_set_position (editable, tmp_pos);
3871           break;
3872  
3873         case GDK_2BUTTON_PRESS:
3874           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3875            * receiving a GDK_2BUTTON_PRESS so we need to reset
3876            * priv->in_drag which may have been set above
3877            */
3878           priv->in_drag = FALSE;
3879           priv->select_words = TRUE;
3880           gtk_entry_select_word (entry);
3881           break;
3882         
3883         case GDK_3BUTTON_PRESS:
3884           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3885            * receiving a GDK_3BUTTON_PRESS so we need to reset
3886            * priv->in_drag which may have been set above
3887            */
3888           priv->in_drag = FALSE;
3889           priv->select_lines = TRUE;
3890           gtk_entry_select_line (entry);
3891           break;
3892
3893         default:
3894           break;
3895         }
3896
3897       return TRUE;
3898     }
3899   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3900     {
3901       if (priv->editable)
3902         {
3903           priv->insert_pos = tmp_pos;
3904           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3905           return TRUE;
3906         }
3907       else
3908         {
3909           gtk_widget_error_bell (widget);
3910         }
3911     }
3912
3913   return FALSE;
3914 }
3915
3916 static gint
3917 gtk_entry_button_release (GtkWidget      *widget,
3918                           GdkEventButton *event)
3919 {
3920   GtkEntry *entry = GTK_ENTRY (widget);
3921   GtkEntryPrivate *priv = entry->priv;
3922   EntryIconInfo *icon_info = NULL;
3923   gint i;
3924
3925   for (i = 0; i < MAX_ICONS; i++)
3926     {
3927       icon_info = priv->icons[i];
3928
3929       if (!icon_info || icon_info->insensitive)
3930         continue;
3931
3932       if (event->window == icon_info->window)
3933         {
3934           icon_info->pressed = FALSE;
3935
3936           if (should_prelight (entry, i) &&
3937               event->x >= 0 && event->y >= 0 &&
3938               event->x < gdk_window_get_width (icon_info->window) &&
3939               event->y < gdk_window_get_height (icon_info->window))
3940             {
3941               icon_info->prelight = TRUE;
3942               gtk_widget_queue_draw (widget);
3943             }
3944
3945           if (!icon_info->nonactivatable)
3946             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3947
3948           return TRUE;
3949         }
3950     }
3951
3952   if (event->window != priv->text_area || priv->button != event->button)
3953     return FALSE;
3954
3955   if (priv->in_drag)
3956     {
3957       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
3958
3959       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3960
3961       priv->in_drag = 0;
3962     }
3963   
3964   priv->button = 0;
3965   
3966   gtk_entry_update_primary_selection (entry);
3967               
3968   return TRUE;
3969 }
3970
3971 static gchar *
3972 _gtk_entry_get_selected_text (GtkEntry *entry)
3973 {
3974   GtkEditable *editable = GTK_EDITABLE (entry);
3975   gint         start_text, end_text;
3976   gchar       *text = NULL;
3977
3978   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3979     text = gtk_editable_get_chars (editable, start_text, end_text);
3980
3981   return text;
3982 }
3983
3984 static gint
3985 gtk_entry_motion_notify (GtkWidget      *widget,
3986                          GdkEventMotion *event)
3987 {
3988   GtkEntry *entry = GTK_ENTRY (widget);
3989   GtkEntryPrivate *priv = entry->priv;
3990   EntryIconInfo *icon_info = NULL;
3991   gint tmp_pos;
3992   gint i;
3993
3994   for (i = 0; i < MAX_ICONS; i++)
3995     {
3996       icon_info = priv->icons[i];
3997
3998       if (!icon_info || icon_info->insensitive)
3999         continue;
4000
4001       if (event->window == icon_info->window)
4002         {
4003           if (icon_info->pressed &&
4004               icon_info->target_list != NULL &&
4005               gtk_drag_check_threshold (widget,
4006                                         priv->start_x,
4007                                         priv->start_y,
4008                                         event->x,
4009                                         event->y))
4010             {
4011               icon_info->in_drag = TRUE;
4012               icon_info->pressed = FALSE;
4013               gtk_drag_begin (widget,
4014                               icon_info->target_list,
4015                               icon_info->actions,
4016                               1,
4017                               (GdkEvent*)event);
4018             }
4019
4020           return TRUE;
4021         }
4022     }
4023
4024   if (priv->mouse_cursor_obscured)
4025     {
4026       GdkCursor *cursor;
4027
4028       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4029       gdk_window_set_cursor (priv->text_area, cursor);
4030       g_object_unref (cursor);
4031       priv->mouse_cursor_obscured = FALSE;
4032     }
4033
4034   if (event->window != priv->text_area || priv->button != 1)
4035     return FALSE;
4036
4037   if (priv->select_lines)
4038     return TRUE;
4039
4040   gdk_event_request_motions (event);
4041
4042   if (priv->in_drag)
4043     {
4044       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
4045           gtk_drag_check_threshold (widget,
4046                                     priv->drag_start_x, priv->drag_start_y,
4047                                     event->x + priv->scroll_offset, event->y))
4048         {
4049           GdkDragContext *context;
4050           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
4051           guint actions = priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
4052           gchar *text = NULL;
4053           cairo_surface_t *surface;
4054
4055           gtk_target_list_add_text_targets (target_list, 0);
4056
4057           text = _gtk_entry_get_selected_text (entry);
4058           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
4059
4060           context = gtk_drag_begin (widget, target_list, actions,
4061                                     priv->button, (GdkEvent *)event);
4062           
4063           if (surface)
4064             gtk_drag_set_icon_surface (context, surface);
4065           else
4066             gtk_drag_set_icon_default (context);
4067           
4068           if (surface)
4069             cairo_surface_destroy (surface);
4070           g_free (text);
4071
4072           priv->in_drag = FALSE;
4073           priv->button = 0;
4074           
4075           gtk_target_list_unref (target_list);
4076         }
4077     }
4078   else
4079     {
4080       if (event->y < 0)
4081         tmp_pos = 0;
4082       else if (event->y >= gdk_window_get_height (priv->text_area))
4083         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4084       else
4085         tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
4086
4087       if (priv->select_words)
4088         {
4089           gint min, max;
4090           gint old_min, old_max;
4091           gint pos, bound;
4092           
4093           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4094           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4095
4096           pos = priv->current_pos;
4097           bound = priv->selection_bound;
4098
4099           old_min = MIN(priv->current_pos, priv->selection_bound);
4100           old_max = MAX(priv->current_pos, priv->selection_bound);
4101           
4102           if (min < old_min)
4103             {
4104               pos = min;
4105               bound = old_max;
4106             }
4107           else if (old_max < max) 
4108             {
4109               pos = max;
4110               bound = old_min;
4111             }
4112           else if (pos == old_min) 
4113             {
4114               if (priv->current_pos != min)
4115                 pos = max;
4116             }
4117           else 
4118             {
4119               if (priv->current_pos != max)
4120                 pos = min;
4121             }
4122         
4123           gtk_entry_set_positions (entry, pos, bound);
4124         }
4125       else
4126         gtk_entry_set_positions (entry, tmp_pos, -1);
4127     }
4128       
4129   return TRUE;
4130 }
4131
4132 static void
4133 set_invisible_cursor (GdkWindow *window)
4134 {
4135   GdkDisplay *display;
4136   GdkCursor *cursor;
4137
4138   display = gdk_window_get_display (window);
4139   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4140
4141   gdk_window_set_cursor (window, cursor);
4142
4143   g_object_unref (cursor);
4144 }
4145
4146 static void
4147 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4148 {
4149   GtkEntryPrivate *priv = entry->priv;
4150
4151   if (priv->mouse_cursor_obscured)
4152     return;
4153
4154   set_invisible_cursor (priv->text_area);
4155
4156   priv->mouse_cursor_obscured = TRUE;
4157 }
4158
4159 static gint
4160 gtk_entry_key_press (GtkWidget   *widget,
4161                      GdkEventKey *event)
4162 {
4163   GtkEntry *entry = GTK_ENTRY (widget);
4164   GtkEntryPrivate *priv = entry->priv;
4165
4166   gtk_entry_reset_blink_time (entry);
4167   gtk_entry_pend_cursor_blink (entry);
4168
4169   if (priv->editable)
4170     {
4171       if (gtk_im_context_filter_keypress (priv->im_context, event))
4172         {
4173           gtk_entry_obscure_mouse_cursor (entry);
4174           priv->need_im_reset = TRUE;
4175           return TRUE;
4176         }
4177     }
4178
4179   if (event->keyval == GDK_KEY_Return || 
4180       event->keyval == GDK_KEY_KP_Enter || 
4181       event->keyval == GDK_KEY_ISO_Enter || 
4182       event->keyval == GDK_KEY_Escape)
4183     {
4184       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4185
4186       if (completion && completion->priv->completion_timeout)
4187         {
4188           g_source_remove (completion->priv->completion_timeout);
4189           completion->priv->completion_timeout = 0;
4190         }
4191
4192       _gtk_entry_reset_im_context (entry);
4193     }
4194
4195   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4196     /* Activate key bindings
4197      */
4198     return TRUE;
4199
4200   if (!priv->editable && event->length)
4201     gtk_widget_error_bell (widget);
4202
4203   return FALSE;
4204 }
4205
4206 static gint
4207 gtk_entry_key_release (GtkWidget   *widget,
4208                        GdkEventKey *event)
4209 {
4210   GtkEntry *entry = GTK_ENTRY (widget);
4211   GtkEntryPrivate *priv = entry->priv;
4212
4213   if (priv->editable)
4214     {
4215       if (gtk_im_context_filter_keypress (priv->im_context, event))
4216         {
4217           priv->need_im_reset = TRUE;
4218           return TRUE;
4219         }
4220     }
4221
4222   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4223 }
4224
4225 static gint
4226 gtk_entry_focus_in (GtkWidget     *widget,
4227                     GdkEventFocus *event)
4228 {
4229   GtkEntry *entry = GTK_ENTRY (widget);
4230   GtkEntryPrivate *priv = entry->priv;
4231   GdkKeymap *keymap;
4232
4233   gtk_widget_queue_draw (widget);
4234
4235   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4236
4237   if (priv->editable)
4238     {
4239       priv->need_im_reset = TRUE;
4240       gtk_im_context_focus_in (priv->im_context);
4241       keymap_state_changed (keymap, entry);
4242       g_signal_connect (keymap, "state-changed", 
4243                         G_CALLBACK (keymap_state_changed), entry);
4244     }
4245
4246   g_signal_connect (keymap, "direction-changed",
4247                     G_CALLBACK (keymap_direction_changed), entry);
4248
4249   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4250       priv->placeholder_text != NULL)
4251     {
4252       gtk_entry_recompute (entry);
4253     }
4254   else
4255     {
4256       gtk_entry_reset_blink_time (entry);
4257       gtk_entry_check_cursor_blink (entry);
4258     }
4259
4260   return FALSE;
4261 }
4262
4263 static gint
4264 gtk_entry_focus_out (GtkWidget     *widget,
4265                      GdkEventFocus *event)
4266 {
4267   GtkEntry *entry = GTK_ENTRY (widget);
4268   GtkEntryPrivate *priv = entry->priv;
4269   GtkEntryCompletion *completion;
4270   GdkKeymap *keymap;
4271
4272   gtk_widget_queue_draw (widget);
4273
4274   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4275
4276   if (priv->editable)
4277     {
4278       priv->need_im_reset = TRUE;
4279       gtk_im_context_focus_out (priv->im_context);
4280       remove_capslock_feedback (entry);
4281     }
4282
4283   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4284       priv->placeholder_text != NULL)
4285     {
4286       gtk_entry_recompute (entry);
4287     }
4288   else
4289     {
4290       gtk_entry_check_cursor_blink (entry);
4291     }
4292
4293   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4294   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4295
4296   completion = gtk_entry_get_completion (entry);
4297   if (completion)
4298     _gtk_entry_completion_popdown (completion);
4299
4300   return FALSE;
4301 }
4302
4303 static void
4304 gtk_entry_grab_focus (GtkWidget *widget)
4305 {
4306   GtkEntry *entry = GTK_ENTRY (widget);
4307   GtkEntryPrivate *priv = entry->priv;
4308   gboolean select_on_focus;
4309
4310   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4311
4312   if (priv->editable && !priv->in_click)
4313     {
4314       g_object_get (gtk_widget_get_settings (widget),
4315                     "gtk-entry-select-on-focus",
4316                     &select_on_focus,
4317                     NULL);
4318
4319       if (select_on_focus)
4320         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4321     }
4322 }
4323
4324 static void
4325 gtk_entry_direction_changed (GtkWidget        *widget,
4326                              GtkTextDirection  previous_dir)
4327 {
4328   GtkEntry *entry = GTK_ENTRY (widget);
4329
4330   gtk_entry_recompute (entry);
4331
4332   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4333 }
4334
4335 static void
4336 gtk_entry_state_flags_changed (GtkWidget     *widget,
4337                                GtkStateFlags  previous_state)
4338 {
4339   GtkEntry *entry = GTK_ENTRY (widget);
4340   GtkEntryPrivate *priv = entry->priv;
4341   GdkCursor *cursor;
4342
4343   if (gtk_widget_get_realized (widget))
4344     {
4345       if (gtk_widget_is_sensitive (widget))
4346         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4347       else
4348         cursor = NULL;
4349
4350       gdk_window_set_cursor (priv->text_area, cursor);
4351
4352       if (cursor)
4353         g_object_unref (cursor);
4354
4355       priv->mouse_cursor_obscured = FALSE;
4356
4357       update_cursors (widget);
4358     }
4359
4360   if (!gtk_widget_is_sensitive (widget))
4361     {
4362       /* Clear any selection */
4363       gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
4364     }
4365
4366   gtk_entry_update_cached_style_values (entry);
4367 }
4368
4369 static void
4370 gtk_entry_screen_changed (GtkWidget *widget,
4371                           GdkScreen *old_screen)
4372 {
4373   gtk_entry_recompute (GTK_ENTRY (widget));
4374 }
4375
4376 /* GtkEditable method implementations
4377  */
4378 static void
4379 gtk_entry_insert_text (GtkEditable *editable,
4380                        const gchar *new_text,
4381                        gint         new_text_length,
4382                        gint        *position)
4383 {
4384   g_object_ref (editable);
4385
4386   /*
4387    * The incoming text may a password or other secret. We make sure
4388    * not to copy it into temporary buffers.
4389    */
4390
4391   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4392
4393   g_object_unref (editable);
4394 }
4395
4396 static void
4397 gtk_entry_delete_text (GtkEditable *editable,
4398                        gint         start_pos,
4399                        gint         end_pos)
4400 {
4401   g_object_ref (editable);
4402
4403   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4404
4405   g_object_unref (editable);
4406 }
4407
4408 static gchar *    
4409 gtk_entry_get_chars      (GtkEditable   *editable,
4410                           gint           start_pos,
4411                           gint           end_pos)
4412 {
4413   GtkEntry *entry = GTK_ENTRY (editable);
4414   const gchar *text;
4415   gint text_length;
4416   gint start_index, end_index;
4417
4418   text = gtk_entry_buffer_get_text (get_buffer (entry));
4419   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4420
4421   if (end_pos < 0)
4422     end_pos = text_length;
4423
4424   start_pos = MIN (text_length, start_pos);
4425   end_pos = MIN (text_length, end_pos);
4426
4427   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4428   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4429
4430   return g_strndup (text + start_index, end_index - start_index);
4431 }
4432
4433 static void
4434 gtk_entry_real_set_position (GtkEditable *editable,
4435                              gint         position)
4436 {
4437   GtkEntry *entry = GTK_ENTRY (editable);
4438   GtkEntryPrivate *priv = entry->priv;
4439
4440   guint length;
4441
4442   length = gtk_entry_buffer_get_length (get_buffer (entry));
4443   if (position < 0 || position > length)
4444     position = length;
4445
4446   if (position != priv->current_pos ||
4447       position != priv->selection_bound)
4448     {
4449       _gtk_entry_reset_im_context (entry);
4450       gtk_entry_set_positions (entry, position, position);
4451     }
4452 }
4453
4454 static gint
4455 gtk_entry_get_position (GtkEditable *editable)
4456 {
4457   GtkEntry *entry = GTK_ENTRY (editable);
4458   GtkEntryPrivate *priv = entry->priv;
4459
4460   return priv->current_pos;
4461 }
4462
4463 static void
4464 gtk_entry_set_selection_bounds (GtkEditable *editable,
4465                                 gint         start,
4466                                 gint         end)
4467 {
4468   GtkEntry *entry = GTK_ENTRY (editable);
4469   guint length;
4470
4471   length = gtk_entry_buffer_get_length (get_buffer (entry));
4472   if (start < 0)
4473     start = length;
4474   if (end < 0)
4475     end = length;
4476   
4477   _gtk_entry_reset_im_context (entry);
4478
4479   gtk_entry_set_positions (entry,
4480                            MIN (end, length),
4481                            MIN (start, length));
4482
4483   gtk_entry_update_primary_selection (entry);
4484 }
4485
4486 static gboolean
4487 gtk_entry_get_selection_bounds (GtkEditable *editable,
4488                                 gint        *start,
4489                                 gint        *end)
4490 {
4491   GtkEntry *entry = GTK_ENTRY (editable);
4492   GtkEntryPrivate *priv = entry->priv;
4493
4494   *start = priv->selection_bound;
4495   *end = priv->current_pos;
4496
4497   return (priv->selection_bound != priv->current_pos);
4498 }
4499
4500 static void
4501 icon_theme_changed (GtkEntry *entry)
4502 {
4503   GtkEntryPrivate *priv = entry->priv;
4504   gint i;
4505
4506   for (i = 0; i < MAX_ICONS; i++)
4507     {
4508       EntryIconInfo *icon_info = priv->icons[i];
4509       if (icon_info != NULL) 
4510         _gtk_icon_helper_invalidate (icon_info->icon_helper);
4511     }
4512
4513   gtk_widget_queue_draw (GTK_WIDGET (entry));
4514 }
4515
4516 static void
4517 icon_margin_changed (GtkEntry *entry)
4518 {
4519   GtkEntryPrivate *priv = entry->priv;
4520   GtkBorder border;
4521
4522   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4523
4524   priv->icon_margin = border.left;
4525 }
4526
4527 static void
4528 gtk_entry_update_cached_style_values (GtkEntry *entry)
4529 {
4530   GtkEntryPrivate *priv = entry->priv;
4531   gint focus_width;
4532   gboolean interior_focus;
4533
4534   gtk_widget_style_get (GTK_WIDGET (entry),
4535                         "focus-line-width", &focus_width,
4536                         "interior-focus", &interior_focus,
4537                         NULL);
4538   priv->focus_width = focus_width;
4539   priv->interior_focus = interior_focus;
4540
4541   if (!priv->invisible_char_set)
4542     {
4543       gunichar ch = find_invisible_char (GTK_WIDGET (entry));
4544
4545       if (priv->invisible_char != ch)
4546         {
4547           priv->invisible_char = ch;
4548           g_object_notify (G_OBJECT (entry), "invisible-char");
4549         }
4550     }
4551 }
4552
4553 static void 
4554 gtk_entry_style_updated (GtkWidget *widget)
4555 {
4556   GtkEntry *entry = GTK_ENTRY (widget);
4557
4558   GTK_WIDGET_CLASS (gtk_entry_parent_class)->style_updated (widget);
4559
4560   gtk_entry_update_cached_style_values (entry);
4561
4562   gtk_entry_recompute (entry);
4563
4564   icon_theme_changed (entry);
4565   icon_margin_changed (entry);
4566 }
4567
4568 /* GtkCellEditable method implementations
4569  */
4570 static void
4571 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4572 {
4573   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4574   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4575 }
4576
4577 static gboolean
4578 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4579                                    GdkEventKey *key_event,
4580                                    gpointer     data)
4581 {
4582   GtkEntryPrivate *priv = entry->priv;
4583
4584   if (key_event->keyval == GDK_KEY_Escape)
4585     {
4586       priv->editing_canceled = TRUE;
4587       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4588       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4589
4590       return TRUE;
4591     }
4592
4593   /* override focus */
4594   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4595     {
4596       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4597       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4598
4599       return TRUE;
4600     }
4601
4602   return FALSE;
4603 }
4604
4605 static void
4606 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4607                          GdkEvent        *event)
4608 {
4609   GtkEntry *entry = GTK_ENTRY (cell_editable);
4610   GtkEntryPrivate *priv = entry->priv;
4611
4612   priv->is_cell_renderer = TRUE;
4613
4614   g_signal_connect (cell_editable, "activate",
4615                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4616   g_signal_connect (cell_editable, "key-press-event",
4617                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4618 }
4619
4620 static void
4621 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4622 {
4623   if (password_hint->source_id)
4624     g_source_remove (password_hint->source_id);
4625
4626   g_slice_free (GtkEntryPasswordHint, password_hint);
4627 }
4628
4629
4630 static gboolean
4631 gtk_entry_remove_password_hint (gpointer data)
4632 {
4633   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4634   password_hint->position = -1;
4635
4636   /* Force the string to be redrawn, but now without a visible character */
4637   gtk_entry_recompute (GTK_ENTRY (data));
4638   return FALSE;
4639 }
4640
4641 /* Default signal handlers
4642  */
4643 static void
4644 gtk_entry_real_insert_text (GtkEditable *editable,
4645                             const gchar *new_text,
4646                             gint         new_text_length,
4647                             gint        *position)
4648 {
4649   guint n_inserted;
4650   gint n_chars;
4651
4652   n_chars = g_utf8_strlen (new_text, new_text_length);
4653
4654   /*
4655    * The actual insertion into the buffer. This will end up firing the
4656    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4657    * buffer_notify_text(), buffer_notify_length()
4658    */
4659   begin_change (GTK_ENTRY (editable));
4660
4661   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4662
4663   end_change (GTK_ENTRY (editable));
4664
4665   if (n_inserted != n_chars)
4666       gtk_widget_error_bell (GTK_WIDGET (editable));
4667
4668   *position += n_inserted;
4669 }
4670
4671 static void
4672 gtk_entry_real_delete_text (GtkEditable *editable,
4673                             gint         start_pos,
4674                             gint         end_pos)
4675 {
4676   /*
4677    * The actual deletion from the buffer. This will end up firing the
4678    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4679    * buffer_notify_text(), buffer_notify_length()
4680    */
4681
4682   begin_change (GTK_ENTRY (editable));
4683
4684   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4685
4686   end_change (GTK_ENTRY (editable));
4687 }
4688
4689 /* GtkEntryBuffer signal handlers
4690  */
4691 static void
4692 buffer_inserted_text (GtkEntryBuffer *buffer,
4693                       guint           position,
4694                       const gchar    *chars,
4695                       guint           n_chars,
4696                       GtkEntry       *entry)
4697 {
4698   GtkEntryPrivate *priv = entry->priv;
4699   guint password_hint_timeout;
4700   guint current_pos;
4701   gint selection_bound;
4702
4703   current_pos = priv->current_pos;
4704   if (current_pos > position)
4705     current_pos += n_chars;
4706
4707   selection_bound = priv->selection_bound;
4708   if (selection_bound > position)
4709     selection_bound += n_chars;
4710
4711   gtk_entry_set_positions (entry, current_pos, selection_bound);
4712
4713   /* Calculate the password hint if it needs to be displayed. */
4714   if (n_chars == 1 && !priv->visible)
4715     {
4716       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4717                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4718                     NULL);
4719
4720       if (password_hint_timeout > 0)
4721         {
4722           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4723                                                                     quark_password_hint);
4724           if (!password_hint)
4725             {
4726               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4727               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4728                                        (GDestroyNotify)gtk_entry_password_hint_free);
4729             }
4730
4731           password_hint->position = position;
4732           if (password_hint->source_id)
4733             g_source_remove (password_hint->source_id);
4734           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4735                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4736         }
4737     }
4738 }
4739
4740 static void
4741 buffer_deleted_text (GtkEntryBuffer *buffer,
4742                      guint           position,
4743                      guint           n_chars,
4744                      GtkEntry       *entry)
4745 {
4746   GtkEntryPrivate *priv = entry->priv;
4747   guint end_pos = position + n_chars;
4748   gint selection_bound;
4749   guint current_pos;
4750
4751   current_pos = priv->current_pos;
4752   if (current_pos > position)
4753     current_pos -= MIN (current_pos, end_pos) - position;
4754
4755   selection_bound = priv->selection_bound;
4756   if (selection_bound > position)
4757     selection_bound -= MIN (selection_bound, end_pos) - position;
4758
4759   gtk_entry_set_positions (entry, current_pos, selection_bound);
4760
4761   /* We might have deleted the selection */
4762   gtk_entry_update_primary_selection (entry);
4763
4764   /* Disable the password hint if one exists. */
4765   if (!priv->visible)
4766     {
4767       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4768                                                                 quark_password_hint);
4769       if (password_hint)
4770         {
4771           if (password_hint->source_id)
4772             g_source_remove (password_hint->source_id);
4773           password_hint->source_id = 0;
4774           password_hint->position = -1;
4775         }
4776     }
4777 }
4778
4779 static void
4780 buffer_notify_text (GtkEntryBuffer *buffer,
4781                     GParamSpec     *spec,
4782                     GtkEntry       *entry)
4783 {
4784   gtk_entry_recompute (entry);
4785   emit_changed (entry);
4786   g_object_notify (G_OBJECT (entry), "text");
4787 }
4788
4789 static void
4790 buffer_notify_length (GtkEntryBuffer *buffer,
4791                       GParamSpec     *spec,
4792                       GtkEntry       *entry)
4793 {
4794   g_object_notify (G_OBJECT (entry), "text-length");
4795 }
4796
4797 static void
4798 buffer_notify_max_length (GtkEntryBuffer *buffer,
4799                           GParamSpec     *spec,
4800                           GtkEntry       *entry)
4801 {
4802   g_object_notify (G_OBJECT (entry), "max-length");
4803 }
4804
4805 static void
4806 buffer_connect_signals (GtkEntry *entry)
4807 {
4808   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4809   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4810   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4811   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4812   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4813 }
4814
4815 static void
4816 buffer_disconnect_signals (GtkEntry *entry)
4817 {
4818   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4819   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4820   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4821   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4822   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4823 }
4824
4825 /* Compute the X position for an offset that corresponds to the "more important
4826  * cursor position for that offset. We use this when trying to guess to which
4827  * end of the selection we should go to when the user hits the left or
4828  * right arrow key.
4829  */
4830 static gint
4831 get_better_cursor_x (GtkEntry *entry,
4832                      gint      offset)
4833 {
4834   GtkEntryPrivate *priv = entry->priv;
4835   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4836   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4837   gboolean split_cursor;
4838   
4839   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4840   const gchar *text = pango_layout_get_text (layout);
4841   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4842   
4843   PangoRectangle strong_pos, weak_pos;
4844   
4845   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4846                 "gtk-split-cursor", &split_cursor,
4847                 NULL);
4848
4849   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4850
4851   if (split_cursor)
4852     return strong_pos.x / PANGO_SCALE;
4853   else
4854     return (keymap_direction == priv->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4855 }
4856
4857 static void
4858 gtk_entry_move_cursor (GtkEntry       *entry,
4859                        GtkMovementStep step,
4860                        gint            count,
4861                        gboolean        extend_selection)
4862 {
4863   GtkEntryPrivate *priv = entry->priv;
4864   gint new_pos = priv->current_pos;
4865
4866   _gtk_entry_reset_im_context (entry);
4867
4868   if (priv->current_pos != priv->selection_bound && !extend_selection)
4869     {
4870       /* If we have a current selection and aren't extending it, move to the
4871        * start/or end of the selection as appropriate
4872        */
4873       switch (step)
4874         {
4875         case GTK_MOVEMENT_VISUAL_POSITIONS:
4876           {
4877             gint current_x = get_better_cursor_x (entry, priv->current_pos);
4878             gint bound_x = get_better_cursor_x (entry, priv->selection_bound);
4879
4880             if (count <= 0)
4881               new_pos = current_x < bound_x ? priv->current_pos : priv->selection_bound;
4882             else 
4883               new_pos = current_x > bound_x ? priv->current_pos : priv->selection_bound;
4884             break;
4885           }
4886         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4887         case GTK_MOVEMENT_WORDS:
4888           if (count < 0)
4889             new_pos = MIN (priv->current_pos, priv->selection_bound);
4890           else
4891             new_pos = MAX (priv->current_pos, priv->selection_bound);
4892           break;
4893         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4894         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4895         case GTK_MOVEMENT_BUFFER_ENDS:
4896           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4897           break;
4898         case GTK_MOVEMENT_DISPLAY_LINES:
4899         case GTK_MOVEMENT_PARAGRAPHS:
4900         case GTK_MOVEMENT_PAGES:
4901         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4902           break;
4903         }
4904     }
4905   else
4906     {
4907       switch (step)
4908         {
4909         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4910           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4911           break;
4912         case GTK_MOVEMENT_VISUAL_POSITIONS:
4913           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4914           if (priv->current_pos == new_pos)
4915             {
4916               if (!extend_selection)
4917                 {
4918                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4919                                                  count > 0 ?
4920                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4921                     {
4922                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4923
4924                       if (toplevel)
4925                         gtk_widget_child_focus (toplevel,
4926                                                 count > 0 ?
4927                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4928                     }
4929                 }
4930               else
4931                 {
4932                   gtk_widget_error_bell (GTK_WIDGET (entry));
4933                 }
4934             }
4935           break;
4936         case GTK_MOVEMENT_WORDS:
4937           while (count > 0)
4938             {
4939               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4940               count--;
4941             }
4942           while (count < 0)
4943             {
4944               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4945               count++;
4946             }
4947           if (priv->current_pos == new_pos)
4948             gtk_widget_error_bell (GTK_WIDGET (entry));
4949           break;
4950         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4951         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4952         case GTK_MOVEMENT_BUFFER_ENDS:
4953           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4954           if (priv->current_pos == new_pos)
4955             gtk_widget_error_bell (GTK_WIDGET (entry));
4956           break;
4957         case GTK_MOVEMENT_DISPLAY_LINES:
4958         case GTK_MOVEMENT_PARAGRAPHS:
4959         case GTK_MOVEMENT_PAGES:
4960         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4961           break;
4962         }
4963     }
4964
4965   if (extend_selection)
4966     gtk_editable_select_region (GTK_EDITABLE (entry), priv->selection_bound, new_pos);
4967   else
4968     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4969   
4970   gtk_entry_pend_cursor_blink (entry);
4971 }
4972
4973 static void
4974 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4975                             const gchar *str)
4976 {
4977   GtkEntryPrivate *priv = entry->priv;
4978   GtkEditable *editable = GTK_EDITABLE (entry);
4979   gint pos = priv->current_pos;
4980
4981   if (priv->editable)
4982     {
4983       _gtk_entry_reset_im_context (entry);
4984
4985       gtk_editable_insert_text (editable, str, -1, &pos);
4986       gtk_editable_set_position (editable, pos);
4987     }
4988 }
4989
4990 static void
4991 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4992                               GtkDeleteType   type,
4993                               gint            count)
4994 {
4995   GtkEntryPrivate *priv = entry->priv;
4996   GtkEditable *editable = GTK_EDITABLE (entry);
4997   gint start_pos = priv->current_pos;
4998   gint end_pos = priv->current_pos;
4999   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
5000   
5001   _gtk_entry_reset_im_context (entry);
5002
5003   if (!priv->editable)
5004     {
5005       gtk_widget_error_bell (GTK_WIDGET (entry));
5006       return;
5007     }
5008
5009   if (priv->selection_bound != priv->current_pos)
5010     {
5011       gtk_editable_delete_selection (editable);
5012       return;
5013     }
5014   
5015   switch (type)
5016     {
5017     case GTK_DELETE_CHARS:
5018       end_pos = gtk_entry_move_logically (entry, priv->current_pos, count);
5019       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
5020       break;
5021     case GTK_DELETE_WORDS:
5022       if (count < 0)
5023         {
5024           /* Move to end of current word, or if not on a word, end of previous word */
5025           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
5026           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5027         }
5028       else if (count > 0)
5029         {
5030           /* Move to beginning of current word, or if not on a word, begining of next word */
5031           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
5032           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5033         }
5034         
5035       /* Fall through */
5036     case GTK_DELETE_WORD_ENDS:
5037       while (count < 0)
5038         {
5039           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5040           count++;
5041         }
5042       while (count > 0)
5043         {
5044           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5045           count--;
5046         }
5047       gtk_editable_delete_text (editable, start_pos, end_pos);
5048       break;
5049     case GTK_DELETE_DISPLAY_LINE_ENDS:
5050     case GTK_DELETE_PARAGRAPH_ENDS:
5051       if (count < 0)
5052         gtk_editable_delete_text (editable, 0, priv->current_pos);
5053       else
5054         gtk_editable_delete_text (editable, priv->current_pos, -1);
5055       break;
5056     case GTK_DELETE_DISPLAY_LINES:
5057     case GTK_DELETE_PARAGRAPHS:
5058       gtk_editable_delete_text (editable, 0, -1);  
5059       break;
5060     case GTK_DELETE_WHITESPACE:
5061       gtk_entry_delete_whitespace (entry);
5062       break;
5063     }
5064
5065   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
5066     gtk_widget_error_bell (GTK_WIDGET (entry));
5067
5068   gtk_entry_pend_cursor_blink (entry);
5069 }
5070
5071 static void
5072 gtk_entry_backspace (GtkEntry *entry)
5073 {
5074   GtkEntryPrivate *priv = entry->priv;
5075   GtkEditable *editable = GTK_EDITABLE (entry);
5076   gint prev_pos;
5077
5078   _gtk_entry_reset_im_context (entry);
5079
5080   if (!priv->editable)
5081     {
5082       gtk_widget_error_bell (GTK_WIDGET (entry));
5083       return;
5084     }
5085
5086   if (priv->selection_bound != priv->current_pos)
5087     {
5088       gtk_editable_delete_selection (editable);
5089       return;
5090     }
5091
5092   prev_pos = gtk_entry_move_logically (entry, priv->current_pos, -1);
5093
5094   if (prev_pos < priv->current_pos)
5095     {
5096       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5097       PangoLogAttr *log_attrs;
5098       gint n_attrs;
5099
5100       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5101
5102       /* Deleting parts of characters */
5103       if (log_attrs[priv->current_pos].backspace_deletes_character)
5104         {
5105           gchar *cluster_text;
5106           gchar *normalized_text;
5107           glong  len;
5108
5109           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5110                                                      priv->current_pos);
5111           normalized_text = g_utf8_normalize (cluster_text,
5112                                               strlen (cluster_text),
5113                                               G_NORMALIZE_NFD);
5114           len = g_utf8_strlen (normalized_text, -1);
5115
5116           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5117           if (len > 1)
5118             {
5119               gint pos = priv->current_pos;
5120
5121               gtk_editable_insert_text (editable, normalized_text,
5122                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5123                                         &pos);
5124               gtk_editable_set_position (editable, pos);
5125             }
5126
5127           g_free (normalized_text);
5128           g_free (cluster_text);
5129         }
5130       else
5131         {
5132           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5133         }
5134       
5135       g_free (log_attrs);
5136     }
5137   else
5138     {
5139       gtk_widget_error_bell (GTK_WIDGET (entry));
5140     }
5141
5142   gtk_entry_pend_cursor_blink (entry);
5143 }
5144
5145 static void
5146 gtk_entry_copy_clipboard (GtkEntry *entry)
5147 {
5148   GtkEntryPrivate *priv = entry->priv;
5149   GtkEditable *editable = GTK_EDITABLE (entry);
5150   gint start, end;
5151   gchar *str;
5152
5153   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5154     {
5155       if (!priv->visible)
5156         {
5157           gtk_widget_error_bell (GTK_WIDGET (entry));
5158           return;
5159         }
5160
5161       str = gtk_entry_get_display_text (entry, start, end);
5162       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5163                                                         GDK_SELECTION_CLIPBOARD),
5164                               str, -1);
5165       g_free (str);
5166     }
5167 }
5168
5169 static void
5170 gtk_entry_cut_clipboard (GtkEntry *entry)
5171 {
5172   GtkEntryPrivate *priv = entry->priv;
5173   GtkEditable *editable = GTK_EDITABLE (entry);
5174   gint start, end;
5175
5176   if (!priv->visible)
5177     {
5178       gtk_widget_error_bell (GTK_WIDGET (entry));
5179       return;
5180     }
5181
5182   gtk_entry_copy_clipboard (entry);
5183
5184   if (priv->editable)
5185     {
5186       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5187         gtk_editable_delete_text (editable, start, end);
5188     }
5189   else
5190     {
5191       gtk_widget_error_bell (GTK_WIDGET (entry));
5192     }
5193 }
5194
5195 static void
5196 gtk_entry_paste_clipboard (GtkEntry *entry)
5197 {
5198   GtkEntryPrivate *priv = entry->priv;
5199
5200   if (priv->editable)
5201     gtk_entry_paste (entry, GDK_SELECTION_CLIPBOARD);
5202   else
5203     gtk_widget_error_bell (GTK_WIDGET (entry));
5204 }
5205
5206 static void
5207 gtk_entry_delete_cb (GtkEntry *entry)
5208 {
5209   GtkEntryPrivate *priv = entry->priv;
5210   GtkEditable *editable = GTK_EDITABLE (entry);
5211   gint start, end;
5212
5213   if (priv->editable)
5214     {
5215       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5216         gtk_editable_delete_text (editable, start, end);
5217     }
5218 }
5219
5220 static void
5221 gtk_entry_toggle_overwrite (GtkEntry *entry)
5222 {
5223   GtkEntryPrivate *priv = entry->priv;
5224
5225   priv->overwrite_mode = !priv->overwrite_mode;
5226   gtk_entry_pend_cursor_blink (entry);
5227   gtk_widget_queue_draw (GTK_WIDGET (entry));
5228 }
5229
5230 static void
5231 gtk_entry_select_all (GtkEntry *entry)
5232 {
5233   gtk_entry_select_line (entry);
5234 }
5235
5236 static void
5237 gtk_entry_real_activate (GtkEntry *entry)
5238 {
5239   GtkEntryPrivate *priv = entry->priv;
5240   GtkWindow *window;
5241   GtkWidget *default_widget, *focus_widget;
5242   GtkWidget *toplevel;
5243   GtkWidget *widget;
5244
5245   widget = GTK_WIDGET (entry);
5246
5247   if (priv->activates_default)
5248     {
5249       toplevel = gtk_widget_get_toplevel (widget);
5250       if (GTK_IS_WINDOW (toplevel))
5251         {
5252           window = GTK_WINDOW (toplevel);
5253
5254           if (window)
5255             {
5256               default_widget = gtk_window_get_default_widget (window);
5257               focus_widget = gtk_window_get_focus (window);
5258               if (widget != default_widget &&
5259                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5260                 gtk_window_activate_default (window);
5261             }
5262         }
5263     }
5264 }
5265
5266 static void
5267 keymap_direction_changed (GdkKeymap *keymap,
5268                           GtkEntry  *entry)
5269 {
5270   gtk_entry_recompute (entry);
5271 }
5272
5273 /* IM Context Callbacks
5274  */
5275
5276 static void
5277 gtk_entry_commit_cb (GtkIMContext *context,
5278                      const gchar  *str,
5279                      GtkEntry     *entry)
5280 {
5281   GtkEntryPrivate *priv = entry->priv;
5282
5283   if (priv->editable)
5284     gtk_entry_enter_text (entry, str);
5285 }
5286
5287 static void 
5288 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5289                               GtkEntry     *entry)
5290 {
5291   GtkEntryPrivate *priv = entry->priv;
5292
5293   if (priv->editable)
5294     {
5295       gchar *preedit_string;
5296       gint cursor_pos;
5297
5298       gtk_im_context_get_preedit_string (priv->im_context,
5299                                          &preedit_string, NULL,
5300                                          &cursor_pos);
5301       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5302       priv->preedit_length = strlen (preedit_string);
5303       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5304       priv->preedit_cursor = cursor_pos;
5305       g_free (preedit_string);
5306     
5307       gtk_entry_recompute (entry);
5308     }
5309 }
5310
5311 static gboolean
5312 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5313                                    GtkEntry     *entry)
5314 {
5315   GtkEntryPrivate *priv = entry->priv;
5316   gchar *text;
5317
5318   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5319   text = gtk_entry_get_display_text (entry, 0, -1);
5320   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5321                                   g_utf8_offset_to_pointer (text, priv->current_pos) - text);
5322   g_free (text);
5323
5324   return TRUE;
5325 }
5326
5327 static gboolean
5328 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5329                                  gint          offset,
5330                                  gint          n_chars,
5331                                  GtkEntry     *entry)
5332 {
5333   GtkEntryPrivate *priv = entry->priv;
5334
5335   if (priv->editable)
5336     gtk_editable_delete_text (GTK_EDITABLE (entry),
5337                               priv->current_pos + offset,
5338                               priv->current_pos + offset + n_chars);
5339
5340   return TRUE;
5341 }
5342
5343 /* Internal functions
5344  */
5345
5346 /* Used for im_commit_cb and inserting Unicode chars */
5347 static void
5348 gtk_entry_enter_text (GtkEntry       *entry,
5349                       const gchar    *str)
5350 {
5351   GtkEntryPrivate *priv = entry->priv;
5352   GtkEditable *editable = GTK_EDITABLE (entry);
5353   gint tmp_pos;
5354   gboolean old_need_im_reset;
5355
5356   old_need_im_reset = priv->need_im_reset;
5357   priv->need_im_reset = FALSE;
5358
5359   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5360     gtk_editable_delete_selection (editable);
5361   else
5362     {
5363       if (priv->overwrite_mode)
5364         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5365     }
5366
5367   tmp_pos = priv->current_pos;
5368   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5369   gtk_editable_set_position (editable, tmp_pos);
5370
5371   priv->need_im_reset = old_need_im_reset;
5372 }
5373
5374 /* All changes to priv->current_pos and priv->selection_bound
5375  * should go through this function.
5376  */
5377 static void
5378 gtk_entry_set_positions (GtkEntry *entry,
5379                          gint      current_pos,
5380                          gint      selection_bound)
5381 {
5382   GtkEntryPrivate *priv = entry->priv;
5383   gboolean changed = FALSE;
5384
5385   g_object_freeze_notify (G_OBJECT (entry));
5386   
5387   if (current_pos != -1 &&
5388       priv->current_pos != current_pos)
5389     {
5390       priv->current_pos = current_pos;
5391       changed = TRUE;
5392
5393       g_object_notify (G_OBJECT (entry), "cursor-position");
5394     }
5395
5396   if (selection_bound != -1 &&
5397       priv->selection_bound != selection_bound)
5398     {
5399       priv->selection_bound = selection_bound;
5400       changed = TRUE;
5401       
5402       g_object_notify (G_OBJECT (entry), "selection-bound");
5403     }
5404
5405   g_object_thaw_notify (G_OBJECT (entry));
5406
5407   if (changed) 
5408     {
5409       gtk_entry_move_adjustments (entry);
5410       gtk_entry_recompute (entry);
5411     }
5412 }
5413
5414 static void
5415 gtk_entry_reset_layout (GtkEntry *entry)
5416 {
5417   GtkEntryPrivate *priv = entry->priv;
5418
5419   if (priv->cached_layout)
5420     {
5421       g_object_unref (priv->cached_layout);
5422       priv->cached_layout = NULL;
5423     }
5424 }
5425
5426 static void
5427 update_im_cursor_location (GtkEntry *entry)
5428 {
5429   GtkEntryPrivate *priv = entry->priv;
5430   GdkRectangle area;
5431   gint strong_x;
5432   gint strong_xoffset;
5433   gint area_width, area_height;
5434
5435   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5436   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5437
5438   strong_xoffset = strong_x - priv->scroll_offset;
5439   if (strong_xoffset < 0)
5440     {
5441       strong_xoffset = 0;
5442     }
5443   else if (strong_xoffset > area_width)
5444     {
5445       strong_xoffset = area_width;
5446     }
5447   area.x = strong_xoffset;
5448   area.y = 0;
5449   area.width = 0;
5450   area.height = area_height;
5451
5452   gtk_im_context_set_cursor_location (priv->im_context, &area);
5453 }
5454
5455 static gboolean
5456 recompute_idle_func (gpointer data)
5457 {
5458   GtkEntry *entry = GTK_ENTRY (data);
5459   GtkEntryPrivate *priv = entry->priv;
5460
5461   priv->recompute_idle = 0;
5462
5463   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5464     {
5465       gtk_entry_adjust_scroll (entry);
5466       gtk_widget_queue_draw (GTK_WIDGET (entry));
5467
5468       update_im_cursor_location (entry);
5469     }
5470
5471   return FALSE;
5472 }
5473
5474 static void
5475 gtk_entry_recompute (GtkEntry *entry)
5476 {
5477   GtkEntryPrivate *priv = entry->priv;
5478
5479   gtk_entry_reset_layout (entry);
5480   gtk_entry_check_cursor_blink (entry);
5481
5482   if (!priv->recompute_idle)
5483     {
5484       priv->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5485                                                recompute_idle_func, entry, NULL); 
5486     }
5487 }
5488
5489 static void
5490 gtk_entry_get_placeholder_text_color (GtkEntry   *entry,
5491                                       PangoColor *color)
5492 {
5493   GtkWidget *widget = GTK_WIDGET (entry);
5494   GtkStyleContext *context;
5495   GdkRGBA fg = { 0.5, 0.5, 0.5 };
5496
5497   context = gtk_widget_get_style_context (widget);
5498   gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
5499
5500   color->red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
5501   color->green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
5502   color->blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
5503 }
5504
5505 static inline gboolean
5506 show_placeholder_text (GtkEntry *entry)
5507 {
5508   GtkEntryPrivate *priv = entry->priv;
5509
5510   if (!gtk_widget_has_focus (GTK_WIDGET (entry)) &&
5511       gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
5512       priv->placeholder_text != NULL)
5513     return TRUE;
5514
5515   return FALSE;
5516 }
5517
5518 static PangoLayout *
5519 gtk_entry_create_layout (GtkEntry *entry,
5520                          gboolean  include_preedit)
5521 {
5522   GtkEntryPrivate *priv = entry->priv;
5523   GtkWidget *widget = GTK_WIDGET (entry);
5524   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5525   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5526   gboolean placeholder_layout = show_placeholder_text (entry);
5527
5528   gchar *preedit_string = NULL;
5529   gint preedit_length = 0;
5530   PangoAttrList *preedit_attrs = NULL;
5531
5532   gchar *display;
5533   guint n_bytes;
5534
5535   pango_layout_set_single_paragraph_mode (layout, TRUE);
5536
5537   display = placeholder_layout ? g_strdup (priv->placeholder_text) : gtk_entry_get_display_text (entry, 0, -1);
5538   n_bytes = strlen (display);
5539
5540   if (!placeholder_layout && include_preedit)
5541     {
5542       gtk_im_context_get_preedit_string (priv->im_context,
5543                                          &preedit_string, &preedit_attrs, NULL);
5544       preedit_length = priv->preedit_length;
5545     }
5546   else if (placeholder_layout)
5547     {
5548       PangoColor color;
5549       PangoAttribute *attr;
5550
5551       gtk_entry_get_placeholder_text_color (entry, &color);
5552       attr = pango_attr_foreground_new (color.red, color.green, color.blue);
5553       attr->start_index = 0;
5554       attr->end_index = G_MAXINT;
5555       pango_attr_list_insert (tmp_attrs, attr);
5556     }
5557
5558   if (preedit_length)
5559     {
5560       GString *tmp_string = g_string_new (display);
5561       gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
5562
5563       g_string_insert (tmp_string, cursor_index, preedit_string);
5564       
5565       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5566       
5567       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5568                               cursor_index, preedit_length);
5569       
5570       g_string_free (tmp_string, TRUE);
5571     }
5572   else
5573     {
5574       PangoDirection pango_dir;
5575       
5576       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5577         pango_dir = pango_find_base_dir (display, n_bytes);
5578       else
5579         pango_dir = PANGO_DIRECTION_NEUTRAL;
5580
5581       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5582         {
5583           if (gtk_widget_has_focus (widget))
5584             {
5585               GdkDisplay *display = gtk_widget_get_display (widget);
5586               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5587               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5588                 pango_dir = PANGO_DIRECTION_RTL;
5589               else
5590                 pango_dir = PANGO_DIRECTION_LTR;
5591             }
5592           else
5593             {
5594               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5595                 pango_dir = PANGO_DIRECTION_RTL;
5596               else
5597                 pango_dir = PANGO_DIRECTION_LTR;
5598             }
5599         }
5600
5601       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5602                                   pango_dir);
5603
5604       priv->resolved_dir = pango_dir;
5605
5606       pango_layout_set_text (layout, display, n_bytes);
5607     }
5608       
5609   pango_layout_set_attributes (layout, tmp_attrs);
5610
5611   g_free (preedit_string);
5612   g_free (display);
5613
5614   if (preedit_attrs)
5615     pango_attr_list_unref (preedit_attrs);
5616       
5617   pango_attr_list_unref (tmp_attrs);
5618
5619   return layout;
5620 }
5621
5622 static PangoLayout *
5623 gtk_entry_ensure_layout (GtkEntry *entry,
5624                          gboolean  include_preedit)
5625 {
5626   GtkEntryPrivate *priv = entry->priv;
5627
5628   if (priv->preedit_length > 0 &&
5629       !include_preedit != !priv->cache_includes_preedit)
5630     gtk_entry_reset_layout (entry);
5631
5632   if (!priv->cached_layout)
5633     {
5634       priv->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5635       priv->cache_includes_preedit = include_preedit;
5636     }
5637
5638   return priv->cached_layout;
5639 }
5640
5641 static void
5642 get_layout_position (GtkEntry *entry,
5643                      gint     *x,
5644                      gint     *y)
5645 {
5646   GtkEntryPrivate *priv = entry->priv;
5647   PangoLayout *layout;
5648   PangoRectangle logical_rect;
5649   gint area_width, area_height;
5650   GtkBorder inner_border;
5651   gint y_pos;
5652   PangoLayoutLine *line;
5653   
5654   layout = gtk_entry_ensure_layout (entry, TRUE);
5655
5656   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5657   _gtk_entry_effective_inner_border (entry, &inner_border);
5658
5659   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5660
5661   line = pango_layout_get_lines_readonly (layout)->data;
5662   pango_layout_line_get_extents (line, NULL, &logical_rect);
5663   
5664   /* Align primarily for locale's ascent/descent */
5665   y_pos = ((area_height - priv->ascent - priv->descent) / 2 +
5666            priv->ascent + logical_rect.y);
5667
5668   /* Now see if we need to adjust to fit in actual drawn string */
5669   if (logical_rect.height > area_height)
5670     y_pos = (area_height - logical_rect.height) / 2;
5671   else if (y_pos < 0)
5672     y_pos = 0;
5673   else if (y_pos + logical_rect.height > area_height)
5674     y_pos = area_height - logical_rect.height;
5675   
5676   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5677
5678   if (x)
5679     *x = inner_border.left - priv->scroll_offset;
5680
5681   if (y)
5682     *y = y_pos;
5683 }
5684
5685 static void
5686 draw_text_with_color (GtkEntry *entry,
5687                       cairo_t  *cr,
5688                       GdkRGBA  *default_color)
5689 {
5690   GtkEntryPrivate *priv = entry->priv;
5691   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5692   GtkWidget *widget;
5693   gint x, y;
5694   gint start_pos, end_pos;
5695
5696   widget = GTK_WIDGET (entry);
5697
5698   cairo_save (cr);
5699
5700   get_layout_position (entry, &x, &y);
5701
5702   cairo_move_to (cr, x, y);
5703   gdk_cairo_set_source_rgba (cr, default_color);
5704   pango_cairo_show_layout (cr, layout);
5705
5706   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5707     {
5708       gint *ranges;
5709       gint n_ranges, i;
5710       PangoRectangle logical_rect;
5711       GdkRGBA selection_color, text_color;
5712       GtkBorder inner_border;
5713       GtkStyleContext *context;
5714       GtkStateFlags state;
5715
5716       context = gtk_widget_get_style_context (widget);
5717       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5718       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5719
5720       state = GTK_STATE_FLAG_SELECTED;
5721
5722       if (gtk_widget_has_focus (widget))
5723         state |= GTK_STATE_FLAG_FOCUSED;
5724
5725       gtk_style_context_get_background_color (context, state, &selection_color);
5726       gtk_style_context_get_color (context, state, &text_color);
5727
5728       _gtk_entry_effective_inner_border (entry, &inner_border);
5729
5730       for (i = 0; i < n_ranges; ++i)
5731         cairo_rectangle (cr,
5732                          inner_border.left - priv->scroll_offset + ranges[2 * i],
5733                          y,
5734                          ranges[2 * i + 1],
5735                          logical_rect.height);
5736
5737       cairo_clip (cr);
5738           
5739       gdk_cairo_set_source_rgba (cr, &selection_color);
5740       cairo_paint (cr);
5741
5742       cairo_move_to (cr, x, y);
5743       gdk_cairo_set_source_rgba (cr, &text_color);
5744       pango_cairo_show_layout (cr, layout);
5745   
5746       g_free (ranges);
5747     }
5748   cairo_restore (cr);
5749 }
5750
5751 static void
5752 gtk_entry_draw_text (GtkEntry *entry,
5753                      cairo_t  *cr)
5754 {
5755   GtkEntryPrivate *priv = entry->priv;
5756   GtkWidget *widget = GTK_WIDGET (entry);
5757   GtkStateFlags state = 0;
5758   GdkRGBA text_color, bar_text_color;
5759   GtkStyleContext *context;
5760   gint width, height;
5761   gint progress_x, progress_y, progress_width, progress_height;
5762   gint clip_width, clip_height;
5763
5764   /* Nothing to display at all */
5765   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5766     return;
5767
5768   state = gtk_widget_get_state_flags (widget);
5769   context = gtk_widget_get_style_context (widget);
5770
5771   gtk_style_context_get_color (context, state, &text_color);
5772
5773   /* Get foreground color for progressbars */
5774   gtk_style_context_save (context);
5775   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
5776   gtk_style_context_get_color (context, state, &bar_text_color);
5777   gtk_style_context_restore (context);
5778
5779   get_progress_area (widget,
5780                      &progress_x, &progress_y,
5781                      &progress_width, &progress_height);
5782
5783   cairo_save (cr);
5784
5785   clip_width = gdk_window_get_width (priv->text_area);
5786   clip_height = gdk_window_get_height (priv->text_area);
5787   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5788   cairo_clip (cr);
5789
5790   /* If the color is the same, or the progress area has a zero
5791    * size, then we only need to draw once. */
5792   if (gdk_rgba_equal (&text_color, &bar_text_color) ||
5793       ((progress_width == 0) || (progress_height == 0)))
5794     {
5795       draw_text_with_color (entry, cr, &text_color);
5796     }
5797   else
5798     {
5799       int frame_x, frame_y, area_x, area_y;
5800
5801       width = gdk_window_get_width (priv->text_area);
5802       height = gdk_window_get_height (priv->text_area);
5803
5804       cairo_save (cr);
5805
5806       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5807       cairo_rectangle (cr, 0, 0, width, height);
5808
5809       /* progres area is frame-relative, we need it text-area window
5810        * relative */
5811       get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
5812       gdk_window_get_position (priv->text_area, &area_x, &area_y);
5813       progress_x += frame_x - area_x;
5814       progress_y += frame_y - area_y;
5815
5816       cairo_rectangle (cr, progress_x, progress_y,
5817                        progress_width, progress_height);
5818       cairo_clip (cr);
5819       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5820   
5821       draw_text_with_color (entry, cr, &text_color);
5822       cairo_restore (cr);
5823
5824       cairo_save (cr);
5825
5826       cairo_rectangle (cr, progress_x, progress_y,
5827                        progress_width, progress_height);
5828       cairo_clip (cr);
5829
5830       draw_text_with_color (entry, cr, &bar_text_color);
5831
5832       cairo_restore (cr);
5833     }
5834
5835   cairo_restore (cr);
5836 }
5837
5838 static void
5839 gtk_entry_draw_cursor (GtkEntry  *entry,
5840                        cairo_t   *cr,
5841                        CursorType type)
5842 {
5843   GtkEntryPrivate *priv = entry->priv;
5844   GtkWidget *widget = GTK_WIDGET (entry);
5845   GtkStyleContext *context;
5846   PangoRectangle cursor_rect;
5847   gint cursor_index;
5848   gboolean block;
5849   gboolean block_at_line_end;
5850   PangoLayout *layout;
5851   const char *text;
5852
5853   context = gtk_widget_get_style_context (widget);
5854
5855   layout = gtk_entry_ensure_layout (entry, TRUE);
5856   text = pango_layout_get_text (layout);
5857   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
5858
5859   if (!priv->overwrite_mode)
5860     block = FALSE;
5861   else
5862     block = _gtk_text_util_get_block_cursor_location (layout,
5863                                                       cursor_index, &cursor_rect, &block_at_line_end);
5864
5865   if (!block)
5866     {
5867       GtkBorder inner_border;
5868
5869       _gtk_entry_effective_inner_border (entry, &inner_border);
5870
5871       gtk_render_insertion_cursor (context, cr,
5872                                    inner_border.left - priv->scroll_offset, inner_border.top,
5873                                    layout, cursor_index, priv->resolved_dir);
5874     }
5875   else /* overwrite_mode */
5876     {
5877       GdkRGBA cursor_color;
5878       GdkRectangle rect;
5879       gint x, y;
5880
5881       cairo_save (cr);
5882
5883       get_layout_position (entry, &x, &y);
5884
5885       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5886       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5887       rect.width = PANGO_PIXELS (cursor_rect.width);
5888       rect.height = PANGO_PIXELS (cursor_rect.height);
5889
5890       _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
5891       gdk_cairo_set_source_rgba (cr, &cursor_color);
5892       gdk_cairo_rectangle (cr, &rect);
5893       cairo_fill (cr);
5894
5895       if (!block_at_line_end)
5896         {
5897           GtkStateFlags state;
5898           GdkRGBA color;
5899
5900           state = gtk_widget_get_state_flags (widget);
5901           gtk_style_context_get_background_color (context, state, &color);
5902
5903           gdk_cairo_rectangle (cr, &rect);
5904           cairo_clip (cr);
5905           cairo_move_to (cr, x, y);
5906           gdk_cairo_set_source_rgba (cr, &color);
5907           pango_cairo_show_layout (cr, layout);
5908         }
5909
5910       cairo_restore (cr);
5911     }
5912 }
5913
5914 void
5915 _gtk_entry_reset_im_context (GtkEntry *entry)
5916 {
5917   GtkEntryPrivate *priv = entry->priv;
5918
5919   if (priv->need_im_reset)
5920     {
5921       priv->need_im_reset = FALSE;
5922       gtk_im_context_reset (priv->im_context);
5923     }
5924 }
5925
5926 /**
5927  * gtk_entry_reset_im_context:
5928  * @entry: a #GtkEntry
5929  *
5930  * Reset the input method context of the entry if needed.
5931  *
5932  * This can be necessary in the case where modifying the buffer
5933  * would confuse on-going input method behavior.
5934  *
5935  * Since: 2.22
5936  */
5937 void
5938 gtk_entry_reset_im_context (GtkEntry *entry)
5939 {
5940   g_return_if_fail (GTK_IS_ENTRY (entry));
5941
5942   _gtk_entry_reset_im_context (entry);
5943 }
5944
5945 /**
5946  * gtk_entry_im_context_filter_keypress:
5947  * @entry: a #GtkEntry
5948  * @event: (type Gdk.EventKey): the key event
5949  *
5950  * Allow the #GtkEntry input method to internally handle key press
5951  * and release events. If this function returns %TRUE, then no further
5952  * processing should be done for this key event. See
5953  * gtk_im_context_filter_keypress().
5954  *
5955  * Note that you are expected to call this function from your handler
5956  * when overriding key event handling. This is needed in the case when
5957  * you need to insert your own key handling between the input method
5958  * and the default key event handling of the #GtkEntry.
5959  * See gtk_text_view_reset_im_context() for an example of use.
5960  *
5961  * Return value: %TRUE if the input method handled the key event.
5962  *
5963  * Since: 2.22
5964  */
5965 gboolean
5966 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
5967                                       GdkEventKey *event)
5968 {
5969   GtkEntryPrivate *priv;
5970
5971   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
5972
5973   priv = entry->priv;
5974
5975   return gtk_im_context_filter_keypress (priv->im_context, event);
5976 }
5977
5978 GtkIMContext*
5979 _gtk_entry_get_im_context (GtkEntry *entry)
5980 {
5981   return entry->priv->im_context;
5982 }
5983
5984 static gint
5985 gtk_entry_find_position (GtkEntry *entry,
5986                          gint      x)
5987 {
5988   GtkEntryPrivate *priv = entry->priv;
5989   PangoLayout *layout;
5990   PangoLayoutLine *line;
5991   gint index;
5992   gint pos;
5993   gint trailing;
5994   const gchar *text;
5995   gint cursor_index;
5996   
5997   layout = gtk_entry_ensure_layout (entry, TRUE);
5998   text = pango_layout_get_text (layout);
5999   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
6000
6001   line = pango_layout_get_lines_readonly (layout)->data;
6002   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
6003
6004   if (index >= cursor_index && priv->preedit_length)
6005     {
6006       if (index >= cursor_index + priv->preedit_length)
6007         index -= priv->preedit_length;
6008       else
6009         {
6010           index = cursor_index;
6011           trailing = 0;
6012         }
6013     }
6014
6015   pos = g_utf8_pointer_to_offset (text, text + index);
6016   pos += trailing;
6017
6018   return pos;
6019 }
6020
6021 static void
6022 gtk_entry_get_cursor_locations (GtkEntry   *entry,
6023                                 CursorType  type,
6024                                 gint       *strong_x,
6025                                 gint       *weak_x)
6026 {
6027   GtkEntryPrivate *priv = entry->priv;
6028   DisplayMode mode = gtk_entry_get_display_mode (entry);
6029
6030   /* Nothing to display at all, so no cursor is relevant */
6031   if (mode == DISPLAY_BLANK)
6032     {
6033       if (strong_x)
6034         *strong_x = 0;
6035       
6036       if (weak_x)
6037         *weak_x = 0;
6038     }
6039   else
6040     {
6041       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
6042       const gchar *text = pango_layout_get_text (layout);
6043       PangoRectangle strong_pos, weak_pos;
6044       gint index;
6045   
6046       if (type == CURSOR_STANDARD)
6047         {
6048           index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
6049         }
6050       else /* type == CURSOR_DND */
6051         {
6052           index = g_utf8_offset_to_pointer (text, priv->dnd_position) - text;
6053
6054           if (priv->dnd_position > priv->current_pos)
6055             {
6056               if (mode == DISPLAY_NORMAL)
6057                 index += priv->preedit_length;
6058               else
6059                 {
6060                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
6061                   index += preedit_len_chars * g_unichar_to_utf8 (priv->invisible_char, NULL);
6062                 }
6063             }
6064         }
6065       
6066       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
6067       
6068       if (strong_x)
6069         *strong_x = strong_pos.x / PANGO_SCALE;
6070       
6071       if (weak_x)
6072         *weak_x = weak_pos.x / PANGO_SCALE;
6073     }
6074 }
6075
6076 static void
6077 gtk_entry_adjust_scroll (GtkEntry *entry)
6078 {
6079   GtkEntryPrivate *priv = entry->priv;
6080   gint min_offset, max_offset;
6081   gint text_area_width, text_width;
6082   GtkBorder inner_border;
6083   gint strong_x, weak_x;
6084   gint strong_xoffset, weak_xoffset;
6085   gfloat xalign;
6086   PangoLayout *layout;
6087   PangoLayoutLine *line;
6088   PangoRectangle logical_rect;
6089
6090   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6091     return;
6092
6093   _gtk_entry_effective_inner_border (entry, &inner_border);
6094
6095   text_area_width = gdk_window_get_width (priv->text_area);
6096   text_area_width -= inner_border.left + inner_border.right;
6097   if (text_area_width < 0)
6098     text_area_width = 0;
6099
6100   layout = gtk_entry_ensure_layout (entry, TRUE);
6101   line = pango_layout_get_lines_readonly (layout)->data;
6102
6103   pango_layout_line_get_extents (line, NULL, &logical_rect);
6104
6105   /* Display as much text as we can */
6106
6107   if (priv->resolved_dir == PANGO_DIRECTION_LTR)
6108       xalign = priv->xalign;
6109   else
6110       xalign = 1.0 - priv->xalign;
6111
6112   text_width = PANGO_PIXELS(logical_rect.width);
6113
6114   if (text_width > text_area_width)
6115     {
6116       min_offset = 0;
6117       max_offset = text_width - text_area_width;
6118     }
6119   else
6120     {
6121       min_offset = (text_width - text_area_width) * xalign;
6122       max_offset = min_offset;
6123     }
6124
6125   priv->scroll_offset = CLAMP (priv->scroll_offset, min_offset, max_offset);
6126
6127   /* And make sure cursors are on screen. Note that the cursor is
6128    * actually drawn one pixel into the INNER_BORDER space on
6129    * the right, when the scroll is at the utmost right. This
6130    * looks better to to me than confining the cursor inside the
6131    * border entirely, though it means that the cursor gets one
6132    * pixel closer to the edge of the widget on the right than
6133    * on the left. This might need changing if one changed
6134    * INNER_BORDER from 2 to 1, as one would do on a
6135    * small-screen-real-estate display.
6136    *
6137    * We always make sure that the strong cursor is on screen, and
6138    * put the weak cursor on screen if possible.
6139    */
6140
6141   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
6142   
6143   strong_xoffset = strong_x - priv->scroll_offset;
6144
6145   if (strong_xoffset < 0)
6146     {
6147       priv->scroll_offset += strong_xoffset;
6148       strong_xoffset = 0;
6149     }
6150   else if (strong_xoffset > text_area_width)
6151     {
6152       priv->scroll_offset += strong_xoffset - text_area_width;
6153       strong_xoffset = text_area_width;
6154     }
6155
6156   weak_xoffset = weak_x - priv->scroll_offset;
6157
6158   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
6159     {
6160       priv->scroll_offset += weak_xoffset;
6161     }
6162   else if (weak_xoffset > text_area_width &&
6163            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
6164     {
6165       priv->scroll_offset += weak_xoffset - text_area_width;
6166     }
6167
6168   g_object_notify (G_OBJECT (entry), "scroll-offset");
6169 }
6170
6171 static void
6172 gtk_entry_move_adjustments (GtkEntry *entry)
6173 {
6174   GtkWidget *widget = GTK_WIDGET (entry);
6175   GtkAllocation allocation;
6176   GtkAdjustment *adjustment;
6177   PangoContext *context;
6178   PangoFontMetrics *metrics;
6179   GtkStyleContext *style_context;
6180   GtkStateFlags state;
6181   gint x, layout_x, border_x, border_y;
6182   gint char_width;
6183
6184   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6185   if (!adjustment)
6186     return;
6187
6188   gtk_widget_get_allocation (widget, &allocation);
6189
6190   /* Cursor position, layout offset, border width, and widget allocation */
6191   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6192   get_layout_position (entry, &layout_x, NULL);
6193   _gtk_entry_get_borders (entry, &border_x, &border_y);
6194   x += allocation.x + layout_x + border_x;
6195
6196   /* Approximate width of a char, so user can see what is ahead/behind */
6197   context = gtk_widget_get_pango_context (widget);
6198   style_context = gtk_widget_get_style_context (widget);
6199   state = gtk_widget_get_state_flags (widget);
6200
6201   metrics = pango_context_get_metrics (context,
6202                                        gtk_style_context_get_font (style_context, state),
6203                                        pango_context_get_language (context));
6204   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6205
6206   /* Scroll it */
6207   gtk_adjustment_clamp_page (adjustment, 
6208                              x - (char_width + 1),   /* one char + one pixel before */
6209                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6210 }
6211
6212 static gint
6213 gtk_entry_move_visually (GtkEntry *entry,
6214                          gint      start,
6215                          gint      count)
6216 {
6217   GtkEntryPrivate *priv = entry->priv;
6218   gint index;
6219   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6220   const gchar *text;
6221
6222   text = pango_layout_get_text (layout);
6223   
6224   index = g_utf8_offset_to_pointer (text, start) - text;
6225
6226   while (count != 0)
6227     {
6228       int new_index, new_trailing;
6229       gboolean split_cursor;
6230       gboolean strong;
6231
6232       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6233                     "gtk-split-cursor", &split_cursor,
6234                     NULL);
6235
6236       if (split_cursor)
6237         strong = TRUE;
6238       else
6239         {
6240           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6241           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6242
6243           strong = keymap_direction == priv->resolved_dir;
6244         }
6245       
6246       if (count > 0)
6247         {
6248           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6249           count--;
6250         }
6251       else
6252         {
6253           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6254           count++;
6255         }
6256
6257       if (new_index < 0)
6258         index = 0;
6259       else if (new_index != G_MAXINT)
6260         index = new_index;
6261       
6262       while (new_trailing--)
6263         index = g_utf8_next_char (text + index) - text;
6264     }
6265   
6266   return g_utf8_pointer_to_offset (text, text + index);
6267 }
6268
6269 static gint
6270 gtk_entry_move_logically (GtkEntry *entry,
6271                           gint      start,
6272                           gint      count)
6273 {
6274   gint new_pos = start;
6275   guint length;
6276
6277   length = gtk_entry_buffer_get_length (get_buffer (entry));
6278
6279   /* Prevent any leak of information */
6280   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6281     {
6282       new_pos = CLAMP (start + count, 0, length);
6283     }
6284   else
6285     {
6286       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6287       PangoLogAttr *log_attrs;
6288       gint n_attrs;
6289
6290       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6291
6292       while (count > 0 && new_pos < length)
6293         {
6294           do
6295             new_pos++;
6296           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6297           
6298           count--;
6299         }
6300       while (count < 0 && new_pos > 0)
6301         {
6302           do
6303             new_pos--;
6304           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6305           
6306           count++;
6307         }
6308       
6309       g_free (log_attrs);
6310     }
6311
6312   return new_pos;
6313 }
6314
6315 static gint
6316 gtk_entry_move_forward_word (GtkEntry *entry,
6317                              gint      start,
6318                              gboolean  allow_whitespace)
6319 {
6320   gint new_pos = start;
6321   guint length;
6322
6323   length = gtk_entry_buffer_get_length (get_buffer (entry));
6324
6325   /* Prevent any leak of information */
6326   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6327     {
6328       new_pos = length;
6329     }
6330   else if (new_pos < length)
6331     {
6332       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6333       PangoLogAttr *log_attrs;
6334       gint n_attrs;
6335
6336       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6337       
6338       /* Find the next word boundary */
6339       new_pos++;
6340       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6341                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6342         new_pos++;
6343
6344       g_free (log_attrs);
6345     }
6346
6347   return new_pos;
6348 }
6349
6350
6351 static gint
6352 gtk_entry_move_backward_word (GtkEntry *entry,
6353                               gint      start,
6354                               gboolean  allow_whitespace)
6355 {
6356   gint new_pos = start;
6357
6358   /* Prevent any leak of information */
6359   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6360     {
6361       new_pos = 0;
6362     }
6363   else if (start > 0)
6364     {
6365       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6366       PangoLogAttr *log_attrs;
6367       gint n_attrs;
6368
6369       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6370
6371       new_pos = start - 1;
6372
6373       /* Find the previous word boundary */
6374       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6375                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6376         new_pos--;
6377
6378       g_free (log_attrs);
6379     }
6380
6381   return new_pos;
6382 }
6383
6384 static void
6385 gtk_entry_delete_whitespace (GtkEntry *entry)
6386 {
6387   GtkEntryPrivate *priv = entry->priv;
6388   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6389   PangoLogAttr *log_attrs;
6390   gint n_attrs;
6391   gint start, end;
6392
6393   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6394
6395   start = end = priv->current_pos;
6396   
6397   while (start > 0 && log_attrs[start-1].is_white)
6398     start--;
6399
6400   while (end < n_attrs && log_attrs[end].is_white)
6401     end++;
6402
6403   g_free (log_attrs);
6404
6405   if (start != end)
6406     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6407 }
6408
6409
6410 static void
6411 gtk_entry_select_word (GtkEntry *entry)
6412 {
6413   GtkEntryPrivate *priv = entry->priv;
6414   gint start_pos = gtk_entry_move_backward_word (entry, priv->current_pos, TRUE);
6415   gint end_pos = gtk_entry_move_forward_word (entry, priv->current_pos, TRUE);
6416
6417   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6418 }
6419
6420 static void
6421 gtk_entry_select_line (GtkEntry *entry)
6422 {
6423   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6424 }
6425
6426 static gint
6427 truncate_multiline (const gchar *text)
6428 {
6429   gint length;
6430
6431   for (length = 0;
6432        text[length] && text[length] != '\n' && text[length] != '\r';
6433        length++);
6434
6435   return length;
6436 }
6437
6438 static void
6439 paste_received (GtkClipboard *clipboard,
6440                 const gchar  *text,
6441                 gpointer      data)
6442 {
6443   GtkEntry *entry = GTK_ENTRY (data);
6444   GtkEditable *editable = GTK_EDITABLE (entry);
6445   GtkEntryPrivate *priv = entry->priv;
6446
6447   if (priv->button == 2)
6448     {
6449       gint pos, start, end;
6450       pos = priv->insert_pos;
6451       gtk_editable_get_selection_bounds (editable, &start, &end);
6452       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6453         gtk_editable_select_region (editable, pos, pos);
6454     }
6455       
6456   if (text)
6457     {
6458       gint pos, start, end;
6459       gint length = -1;
6460       gboolean popup_completion;
6461       GtkEntryCompletion *completion;
6462
6463       completion = gtk_entry_get_completion (entry);
6464
6465       if (priv->truncate_multiline)
6466         length = truncate_multiline (text);
6467
6468       /* only complete if the selection is at the end */
6469       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6470                           MAX (priv->current_pos, priv->selection_bound));
6471
6472       if (completion)
6473         {
6474           if (gtk_widget_get_mapped (completion->priv->popup_window))
6475             _gtk_entry_completion_popdown (completion);
6476
6477           if (!popup_completion && completion->priv->changed_id > 0)
6478             g_signal_handler_block (entry, completion->priv->changed_id);
6479         }
6480
6481       begin_change (entry);
6482       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6483         gtk_editable_delete_text (editable, start, end);
6484
6485       pos = priv->current_pos;
6486       gtk_editable_insert_text (editable, text, length, &pos);
6487       gtk_editable_set_position (editable, pos);
6488       end_change (entry);
6489
6490       if (completion &&
6491           !popup_completion && completion->priv->changed_id > 0)
6492         g_signal_handler_unblock (entry, completion->priv->changed_id);
6493     }
6494
6495   g_object_unref (entry);
6496 }
6497
6498 static void
6499 gtk_entry_paste (GtkEntry *entry,
6500                  GdkAtom   selection)
6501 {
6502   g_object_ref (entry);
6503   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6504                               paste_received, entry);
6505 }
6506
6507 static void
6508 primary_get_cb (GtkClipboard     *clipboard,
6509                 GtkSelectionData *selection_data,
6510                 guint             info,
6511                 gpointer          data)
6512 {
6513   GtkEntry *entry = GTK_ENTRY (data);
6514   gint start, end;
6515   
6516   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6517     {
6518       gchar *str = gtk_entry_get_display_text (entry, start, end);
6519       gtk_selection_data_set_text (selection_data, str, -1);
6520       g_free (str);
6521     }
6522 }
6523
6524 static void
6525 primary_clear_cb (GtkClipboard *clipboard,
6526                   gpointer      data)
6527 {
6528   GtkEntry *entry = GTK_ENTRY (data);
6529   GtkEntryPrivate *priv = entry->priv;
6530
6531   gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
6532 }
6533
6534 static void
6535 gtk_entry_update_primary_selection (GtkEntry *entry)
6536 {
6537   GtkTargetList *list;
6538   GtkTargetEntry *targets;
6539   GtkClipboard *clipboard;
6540   gint start, end;
6541   gint n_targets;
6542
6543   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6544     return;
6545
6546   list = gtk_target_list_new (NULL, 0);
6547   gtk_target_list_add_text_targets (list, 0);
6548
6549   targets = gtk_target_table_new_from_list (list, &n_targets);
6550
6551   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6552   
6553   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6554     {
6555       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6556                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6557         primary_clear_cb (clipboard, entry);
6558     }
6559   else
6560     {
6561       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6562         gtk_clipboard_clear (clipboard);
6563     }
6564
6565   gtk_target_table_free (targets, n_targets);
6566   gtk_target_list_unref (list);
6567 }
6568
6569 static void
6570 gtk_entry_clear (GtkEntry             *entry,
6571                  GtkEntryIconPosition  icon_pos)
6572 {
6573   GtkEntryPrivate *priv = entry->priv;
6574   EntryIconInfo *icon_info = priv->icons[icon_pos];
6575   GtkImageType storage_type;
6576
6577   if (icon_info && _gtk_icon_helper_get_is_empty (icon_info->icon_helper))
6578     return;
6579
6580   g_object_freeze_notify (G_OBJECT (entry));
6581
6582   /* Explicitly check, as the pointer may become invalidated
6583    * during destruction.
6584    */
6585   if (GDK_IS_WINDOW (icon_info->window))
6586     gdk_window_hide (icon_info->window);
6587
6588   storage_type = _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
6589
6590   switch (storage_type)
6591     {
6592     case GTK_IMAGE_PIXBUF:
6593       g_object_notify (G_OBJECT (entry),
6594                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6595       break;
6596
6597     case GTK_IMAGE_STOCK:
6598       g_object_notify (G_OBJECT (entry),
6599                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6600       break;
6601
6602     case GTK_IMAGE_ICON_NAME:
6603       g_object_notify (G_OBJECT (entry),
6604                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6605       break;
6606
6607     case GTK_IMAGE_GICON:
6608       g_object_notify (G_OBJECT (entry),
6609                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6610       break;
6611
6612     default:
6613       g_assert_not_reached ();
6614       break;
6615     }
6616
6617   _gtk_icon_helper_clear (icon_info->icon_helper);
6618
6619   g_object_notify (G_OBJECT (entry),
6620                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6621
6622   g_object_thaw_notify (G_OBJECT (entry));
6623 }
6624
6625 static GdkPixbuf *
6626 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6627                          GtkEntryIconPosition  icon_pos)
6628 {
6629   GtkEntryPrivate *priv = entry->priv;
6630   EntryIconInfo *icon_info = priv->icons[icon_pos];
6631   GtkStyleContext *context;
6632   GdkPixbuf *pix;
6633
6634   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
6635   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
6636
6637   pix = _gtk_icon_helper_ensure_pixbuf (icon_info->icon_helper,
6638                                         context);
6639
6640   gtk_style_context_restore (context);
6641
6642   return pix;
6643 }
6644
6645 /* Public API
6646  */
6647
6648 /**
6649  * gtk_entry_new:
6650  *
6651  * Creates a new entry.
6652  *
6653  * Return value: a new #GtkEntry.
6654  */
6655 GtkWidget*
6656 gtk_entry_new (void)
6657 {
6658   return g_object_new (GTK_TYPE_ENTRY, NULL);
6659 }
6660
6661 /**
6662  * gtk_entry_new_with_buffer:
6663  * @buffer: The buffer to use for the new #GtkEntry.
6664  *
6665  * Creates a new entry with the specified text buffer.
6666  *
6667  * Return value: a new #GtkEntry
6668  *
6669  * Since: 2.18
6670  */
6671 GtkWidget*
6672 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6673 {
6674   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6675   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6676 }
6677
6678 static GtkEntryBuffer*
6679 get_buffer (GtkEntry *entry)
6680 {
6681   GtkEntryPrivate *priv = entry->priv;
6682
6683   if (priv->buffer == NULL)
6684     {
6685       GtkEntryBuffer *buffer;
6686       buffer = gtk_entry_buffer_new (NULL, 0);
6687       gtk_entry_set_buffer (entry, buffer);
6688       g_object_unref (buffer);
6689     }
6690
6691   return priv->buffer;
6692 }
6693
6694 /**
6695  * gtk_entry_get_buffer:
6696  * @entry: a #GtkEntry
6697  *
6698  * Get the #GtkEntryBuffer object which holds the text for
6699  * this widget.
6700  *
6701  * Since: 2.18
6702  *
6703  * Returns: (transfer none): A #GtkEntryBuffer object.
6704  */
6705 GtkEntryBuffer*
6706 gtk_entry_get_buffer (GtkEntry *entry)
6707 {
6708   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6709
6710   return get_buffer (entry);
6711 }
6712
6713 /**
6714  * gtk_entry_set_buffer:
6715  * @entry: a #GtkEntry
6716  * @buffer: a #GtkEntryBuffer
6717  *
6718  * Set the #GtkEntryBuffer object which holds the text for
6719  * this widget.
6720  *
6721  * Since: 2.18
6722  */
6723 void
6724 gtk_entry_set_buffer (GtkEntry       *entry,
6725                       GtkEntryBuffer *buffer)
6726 {
6727   GtkEntryPrivate *priv;
6728   GObject *obj;
6729
6730   g_return_if_fail (GTK_IS_ENTRY (entry));
6731
6732   priv = entry->priv;
6733
6734   if (buffer)
6735     {
6736       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6737       g_object_ref (buffer);
6738     }
6739
6740   if (priv->buffer)
6741     {
6742       buffer_disconnect_signals (entry);
6743       g_object_unref (priv->buffer);
6744     }
6745
6746   priv->buffer = buffer;
6747
6748   if (priv->buffer)
6749      buffer_connect_signals (entry);
6750
6751   obj = G_OBJECT (entry);
6752   g_object_freeze_notify (obj);
6753   g_object_notify (obj, "buffer");
6754   g_object_notify (obj, "text");
6755   g_object_notify (obj, "text-length");
6756   g_object_notify (obj, "max-length");
6757   g_object_notify (obj, "visibility");
6758   g_object_notify (obj, "invisible-char");
6759   g_object_notify (obj, "invisible-char-set");
6760   g_object_thaw_notify (obj);
6761
6762   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6763   gtk_entry_recompute (entry);
6764 }
6765
6766 /**
6767  * gtk_entry_get_text_area:
6768  * @entry: a #GtkEntry
6769  * @text_area: (out): Return location for the text area.
6770  *
6771  * Gets the area where the entry's text is drawn. This function is
6772  * useful when drawing something to the entry in a draw callback.
6773  *
6774  * If the entry is not realized, @text_area is filled with zeros.
6775  *
6776  * See also gtk_entry_get_icon_area().
6777  *
6778  * Since: 3.0
6779  **/
6780 void
6781 gtk_entry_get_text_area (GtkEntry     *entry,
6782                          GdkRectangle *text_area)
6783 {
6784   GtkEntryPrivate *priv;
6785
6786   g_return_if_fail (GTK_IS_ENTRY (entry));
6787   g_return_if_fail (text_area != NULL);
6788
6789   priv = entry->priv;
6790
6791   if (priv->text_area)
6792     {
6793       GtkAllocation allocation;
6794       gint x, y;
6795
6796       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6797       gdk_window_get_position (priv->text_area, &x, &y);
6798
6799       text_area->x = x - allocation.x;
6800       text_area->y = y - allocation.y;
6801       text_area->width = gdk_window_get_width (priv->text_area);
6802       text_area->height = gdk_window_get_height (priv->text_area);
6803     }
6804   else
6805     {
6806       text_area->x = 0;
6807       text_area->y = 0;
6808       text_area->width = 0;
6809       text_area->height = 0;
6810     }
6811 }
6812
6813 /**
6814  * gtk_entry_set_text:
6815  * @entry: a #GtkEntry
6816  * @text: the new text
6817  *
6818  * Sets the text in the widget to the given
6819  * value, replacing the current contents.
6820  *
6821  * See gtk_entry_buffer_set_text().
6822  */
6823 void
6824 gtk_entry_set_text (GtkEntry    *entry,
6825                     const gchar *text)
6826 {
6827   gint tmp_pos;
6828   GtkEntryCompletion *completion;
6829
6830   g_return_if_fail (GTK_IS_ENTRY (entry));
6831   g_return_if_fail (text != NULL);
6832
6833   /* Actually setting the text will affect the cursor and selection;
6834    * if the contents don't actually change, this will look odd to the user.
6835    */
6836   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6837     return;
6838
6839   completion = gtk_entry_get_completion (entry);
6840   if (completion && completion->priv->changed_id > 0)
6841     g_signal_handler_block (entry, completion->priv->changed_id);
6842
6843   begin_change (entry);
6844   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6845   tmp_pos = 0;
6846   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6847   end_change (entry);
6848
6849   if (completion && completion->priv->changed_id > 0)
6850     g_signal_handler_unblock (entry, completion->priv->changed_id);
6851 }
6852
6853 /**
6854  * gtk_entry_set_visibility:
6855  * @entry: a #GtkEntry
6856  * @visible: %TRUE if the contents of the entry are displayed
6857  *           as plaintext
6858  *
6859  * Sets whether the contents of the entry are visible or not. 
6860  * When visibility is set to %FALSE, characters are displayed 
6861  * as the invisible char, and will also appear that way when 
6862  * the text in the entry widget is copied elsewhere.
6863  *
6864  * By default, GTK+ picks the best invisible character available
6865  * in the current font, but it can be changed with
6866  * gtk_entry_set_invisible_char().
6867  */
6868 void
6869 gtk_entry_set_visibility (GtkEntry *entry,
6870                           gboolean visible)
6871 {
6872   GtkEntryPrivate *priv;
6873
6874   g_return_if_fail (GTK_IS_ENTRY (entry));
6875
6876   priv = entry->priv;
6877
6878   visible = visible != FALSE;
6879
6880   if (priv->visible != visible)
6881     {
6882       priv->visible = visible;
6883
6884       g_object_notify (G_OBJECT (entry), "visibility");
6885       gtk_entry_recompute (entry);
6886     }
6887 }
6888
6889 /**
6890  * gtk_entry_get_visibility:
6891  * @entry: a #GtkEntry
6892  *
6893  * Retrieves whether the text in @entry is visible. See
6894  * gtk_entry_set_visibility().
6895  *
6896  * Return value: %TRUE if the text is currently visible
6897  **/
6898 gboolean
6899 gtk_entry_get_visibility (GtkEntry *entry)
6900 {
6901   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6902
6903   return entry->priv->visible;
6904 }
6905
6906 /**
6907  * gtk_entry_set_invisible_char:
6908  * @entry: a #GtkEntry
6909  * @ch: a Unicode character
6910  * 
6911  * Sets the character to use in place of the actual text when
6912  * gtk_entry_set_visibility() has been called to set text visibility
6913  * to %FALSE. i.e. this is the character used in "password mode" to
6914  * show the user how many characters have been typed. By default, GTK+
6915  * picks the best invisible char available in the current font. If you
6916  * set the invisible char to 0, then the user will get no feedback
6917  * at all; there will be no text on the screen as they type.
6918  **/
6919 void
6920 gtk_entry_set_invisible_char (GtkEntry *entry,
6921                               gunichar  ch)
6922 {
6923   GtkEntryPrivate *priv;
6924
6925   g_return_if_fail (GTK_IS_ENTRY (entry));
6926
6927   priv = entry->priv;
6928
6929   if (!priv->invisible_char_set)
6930     {
6931       priv->invisible_char_set = TRUE;
6932       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6933     }
6934
6935   if (ch == priv->invisible_char)
6936     return;
6937
6938   priv->invisible_char = ch;
6939   g_object_notify (G_OBJECT (entry), "invisible-char");
6940   gtk_entry_recompute (entry);  
6941 }
6942
6943 /**
6944  * gtk_entry_get_invisible_char:
6945  * @entry: a #GtkEntry
6946  *
6947  * Retrieves the character displayed in place of the real characters
6948  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6949  *
6950  * Return value: the current invisible char, or 0, if the entry does not
6951  *               show invisible text at all. 
6952  **/
6953 gunichar
6954 gtk_entry_get_invisible_char (GtkEntry *entry)
6955 {
6956   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6957
6958   return entry->priv->invisible_char;
6959 }
6960
6961 /**
6962  * gtk_entry_unset_invisible_char:
6963  * @entry: a #GtkEntry
6964  *
6965  * Unsets the invisible char previously set with
6966  * gtk_entry_set_invisible_char(). So that the
6967  * default invisible char is used again.
6968  *
6969  * Since: 2.16
6970  **/
6971 void
6972 gtk_entry_unset_invisible_char (GtkEntry *entry)
6973 {
6974   GtkEntryPrivate *priv;
6975   gunichar ch;
6976
6977   g_return_if_fail (GTK_IS_ENTRY (entry));
6978
6979   priv = entry->priv;
6980
6981   if (!priv->invisible_char_set)
6982     return;
6983
6984   priv->invisible_char_set = FALSE;
6985   ch = find_invisible_char (GTK_WIDGET (entry));
6986
6987   if (priv->invisible_char != ch)
6988     {
6989       priv->invisible_char = ch;
6990       g_object_notify (G_OBJECT (entry), "invisible-char");
6991     }
6992
6993   g_object_notify (G_OBJECT (entry), "invisible-char-set");
6994   gtk_entry_recompute (entry);
6995 }
6996
6997 /**
6998  * gtk_entry_set_overwrite_mode:
6999  * @entry: a #GtkEntry
7000  * @overwrite: new value
7001  * 
7002  * Sets whether the text is overwritten when typing in the #GtkEntry.
7003  *
7004  * Since: 2.14
7005  **/
7006 void
7007 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7008                               gboolean  overwrite)
7009 {
7010   GtkEntryPrivate *priv = entry->priv;
7011
7012   g_return_if_fail (GTK_IS_ENTRY (entry));
7013
7014   if (priv->overwrite_mode == overwrite)
7015     return;
7016   
7017   gtk_entry_toggle_overwrite (entry);
7018
7019   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7020 }
7021
7022 /**
7023  * gtk_entry_get_overwrite_mode:
7024  * @entry: a #GtkEntry
7025  * 
7026  * Gets the value set by gtk_entry_set_overwrite_mode().
7027  * 
7028  * Return value: whether the text is overwritten when typing.
7029  *
7030  * Since: 2.14
7031  **/
7032 gboolean
7033 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7034 {
7035   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7036
7037   return entry->priv->overwrite_mode;
7038 }
7039
7040 /**
7041  * gtk_entry_get_text:
7042  * @entry: a #GtkEntry
7043  *
7044  * Retrieves the contents of the entry widget.
7045  * See also gtk_editable_get_chars().
7046  *
7047  * This is equivalent to:
7048  *
7049  * <informalexample><programlisting>
7050  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7051  * </programlisting></informalexample>
7052  *
7053  * Return value: a pointer to the contents of the widget as a
7054  *      string. This string points to internally allocated
7055  *      storage in the widget and must not be freed, modified or
7056  *      stored.
7057  **/
7058 const gchar*
7059 gtk_entry_get_text (GtkEntry *entry)
7060 {
7061   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7062
7063   return gtk_entry_buffer_get_text (get_buffer (entry));
7064 }
7065
7066 /**
7067  * gtk_entry_set_max_length:
7068  * @entry: a #GtkEntry
7069  * @max: the maximum length of the entry, or 0 for no maximum.
7070  *   (other than the maximum length of entries.) The value passed in will
7071  *   be clamped to the range 0-65536.
7072  * 
7073  * Sets the maximum allowed length of the contents of the widget. If
7074  * the current contents are longer than the given length, then they
7075  * will be truncated to fit.
7076  *
7077  * This is equivalent to:
7078  *
7079  * <informalexample><programlisting>
7080  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7081  * </programlisting></informalexample>
7082  **/
7083 void
7084 gtk_entry_set_max_length (GtkEntry     *entry,
7085                           gint          max)
7086 {
7087   g_return_if_fail (GTK_IS_ENTRY (entry));
7088   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7089 }
7090
7091 /**
7092  * gtk_entry_get_max_length:
7093  * @entry: a #GtkEntry
7094  *
7095  * Retrieves the maximum allowed length of the text in
7096  * @entry. See gtk_entry_set_max_length().
7097  *
7098  * This is equivalent to:
7099  *
7100  * <informalexample><programlisting>
7101  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7102  * </programlisting></informalexample>
7103  *
7104  * Return value: the maximum allowed number of characters
7105  *               in #GtkEntry, or 0 if there is no maximum.
7106  **/
7107 gint
7108 gtk_entry_get_max_length (GtkEntry *entry)
7109 {
7110   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7111
7112   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7113 }
7114
7115 /**
7116  * gtk_entry_get_text_length:
7117  * @entry: a #GtkEntry
7118  *
7119  * Retrieves the current length of the text in
7120  * @entry. 
7121  *
7122  * This is equivalent to:
7123  *
7124  * <informalexample><programlisting>
7125  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7126  * </programlisting></informalexample>
7127  *
7128  * Return value: the current number of characters
7129  *               in #GtkEntry, or 0 if there are none.
7130  *
7131  * Since: 2.14
7132  **/
7133 guint16
7134 gtk_entry_get_text_length (GtkEntry *entry)
7135 {
7136   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7137
7138   return gtk_entry_buffer_get_length (get_buffer (entry));
7139 }
7140
7141 /**
7142  * gtk_entry_set_activates_default:
7143  * @entry: a #GtkEntry
7144  * @setting: %TRUE to activate window's default widget on Enter keypress
7145  *
7146  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7147  * widget for the window containing the entry. This usually means that
7148  * the dialog box containing the entry will be closed, since the default
7149  * widget is usually one of the dialog buttons.
7150  *
7151  * (For experts: if @setting is %TRUE, the entry calls
7152  * gtk_window_activate_default() on the window containing the entry, in
7153  * the default handler for the #GtkWidget::activate signal.)
7154  **/
7155 void
7156 gtk_entry_set_activates_default (GtkEntry *entry,
7157                                  gboolean  setting)
7158 {
7159   GtkEntryPrivate *priv;
7160
7161   g_return_if_fail (GTK_IS_ENTRY (entry));
7162
7163   priv = entry->priv;
7164
7165   setting = setting != FALSE;
7166
7167   if (setting != priv->activates_default)
7168     {
7169       priv->activates_default = setting;
7170       g_object_notify (G_OBJECT (entry), "activates-default");
7171     }
7172 }
7173
7174 /**
7175  * gtk_entry_get_activates_default:
7176  * @entry: a #GtkEntry
7177  * 
7178  * Retrieves the value set by gtk_entry_set_activates_default().
7179  * 
7180  * Return value: %TRUE if the entry will activate the default widget
7181  **/
7182 gboolean
7183 gtk_entry_get_activates_default (GtkEntry *entry)
7184 {
7185   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7186
7187   return entry->priv->activates_default;
7188 }
7189
7190 /**
7191  * gtk_entry_set_width_chars:
7192  * @entry: a #GtkEntry
7193  * @n_chars: width in chars
7194  *
7195  * Changes the size request of the entry to be about the right size
7196  * for @n_chars characters. Note that it changes the size
7197  * <emphasis>request</emphasis>, the size can still be affected by
7198  * how you pack the widget into containers. If @n_chars is -1, the
7199  * size reverts to the default entry size.
7200  **/
7201 void
7202 gtk_entry_set_width_chars (GtkEntry *entry,
7203                            gint      n_chars)
7204 {
7205   GtkEntryPrivate *priv;
7206
7207   g_return_if_fail (GTK_IS_ENTRY (entry));
7208
7209   priv = entry->priv;
7210
7211   if (priv->width_chars != n_chars)
7212     {
7213       priv->width_chars = n_chars;
7214       g_object_notify (G_OBJECT (entry), "width-chars");
7215       gtk_widget_queue_resize (GTK_WIDGET (entry));
7216     }
7217 }
7218
7219 /**
7220  * gtk_entry_get_width_chars:
7221  * @entry: a #GtkEntry
7222  * 
7223  * Gets the value set by gtk_entry_set_width_chars().
7224  * 
7225  * Return value: number of chars to request space for, or negative if unset
7226  **/
7227 gint
7228 gtk_entry_get_width_chars (GtkEntry *entry)
7229 {
7230   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7231
7232   return entry->priv->width_chars;
7233 }
7234
7235 /**
7236  * gtk_entry_set_has_frame:
7237  * @entry: a #GtkEntry
7238  * @setting: new value
7239  * 
7240  * Sets whether the entry has a beveled frame around it.
7241  **/
7242 void
7243 gtk_entry_set_has_frame (GtkEntry *entry,
7244                          gboolean  setting)
7245 {
7246   GtkEntryPrivate *priv;
7247
7248   g_return_if_fail (GTK_IS_ENTRY (entry));
7249
7250   priv = entry->priv;
7251
7252   setting = (setting != FALSE);
7253
7254   if (priv->has_frame == setting)
7255     return;
7256
7257   gtk_widget_queue_resize (GTK_WIDGET (entry));
7258   priv->has_frame = setting;
7259   g_object_notify (G_OBJECT (entry), "has-frame");
7260 }
7261
7262 /**
7263  * gtk_entry_get_has_frame:
7264  * @entry: a #GtkEntry
7265  * 
7266  * Gets the value set by gtk_entry_set_has_frame().
7267  * 
7268  * Return value: whether the entry has a beveled frame
7269  **/
7270 gboolean
7271 gtk_entry_get_has_frame (GtkEntry *entry)
7272 {
7273   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7274
7275   return entry->priv->has_frame;
7276 }
7277
7278 /**
7279  * gtk_entry_set_inner_border:
7280  * @entry: a #GtkEntry
7281  * @border: (allow-none): a #GtkBorder, or %NULL
7282  *
7283  * Sets %entry's inner-border property to %border, or clears it if %NULL
7284  * is passed. The inner-border is the area around the entry's text, but
7285  * inside its frame.
7286  *
7287  * If set, this property overrides the inner-border style property.
7288  * Overriding the style-provided border is useful when you want to do
7289  * in-place editing of some text in a canvas or list widget, where
7290  * pixel-exact positioning of the entry is important.
7291  *
7292  * Since: 2.10
7293  **/
7294 void
7295 gtk_entry_set_inner_border (GtkEntry        *entry,
7296                             const GtkBorder *border)
7297 {
7298   g_return_if_fail (GTK_IS_ENTRY (entry));
7299
7300   gtk_widget_queue_resize (GTK_WIDGET (entry));
7301
7302   if (border)
7303     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7304                              gtk_border_copy (border),
7305                              (GDestroyNotify) gtk_border_free);
7306   else
7307     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7308
7309   g_object_notify (G_OBJECT (entry), "inner-border");
7310 }
7311
7312 /**
7313  * gtk_entry_get_inner_border:
7314  * @entry: a #GtkEntry
7315  *
7316  * This function returns the entry's #GtkEntry:inner-border property. See
7317  * gtk_entry_set_inner_border() for more information.
7318  *
7319  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7320  *
7321  * Since: 2.10
7322  **/
7323 const GtkBorder *
7324 gtk_entry_get_inner_border (GtkEntry *entry)
7325 {
7326   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7327
7328   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7329 }
7330
7331 /**
7332  * gtk_entry_get_layout:
7333  * @entry: a #GtkEntry
7334  * 
7335  * Gets the #PangoLayout used to display the entry.
7336  * The layout is useful to e.g. convert text positions to
7337  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7338  * The returned layout is owned by the entry and must not be 
7339  * modified or freed by the caller.
7340  *
7341  * Keep in mind that the layout text may contain a preedit string, so
7342  * gtk_entry_layout_index_to_text_index() and
7343  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7344  * indices in the layout to byte indices in the entry contents.
7345  *
7346  * Return value: (transfer none): the #PangoLayout for this entry
7347  **/
7348 PangoLayout*
7349 gtk_entry_get_layout (GtkEntry *entry)
7350 {
7351   PangoLayout *layout;
7352   
7353   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7354
7355   layout = gtk_entry_ensure_layout (entry, TRUE);
7356
7357   return layout;
7358 }
7359
7360
7361 /**
7362  * gtk_entry_layout_index_to_text_index:
7363  * @entry: a #GtkEntry
7364  * @layout_index: byte index into the entry layout text
7365  * 
7366  * Converts from a position in the entry contents (returned
7367  * by gtk_entry_get_text()) to a position in the
7368  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7369  * with text retrieved via pango_layout_get_text()).
7370  * 
7371  * Return value: byte index into the entry contents
7372  **/
7373 gint
7374 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7375                                       gint      layout_index)
7376 {
7377   GtkEntryPrivate *priv;
7378   PangoLayout *layout;
7379   const gchar *text;
7380   gint cursor_index;
7381   
7382   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7383
7384   priv = entry->priv;
7385
7386   layout = gtk_entry_ensure_layout (entry, TRUE);
7387   text = pango_layout_get_text (layout);
7388   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7389
7390   if (layout_index >= cursor_index && priv->preedit_length)
7391     {
7392       if (layout_index >= cursor_index + priv->preedit_length)
7393         layout_index -= priv->preedit_length;
7394       else
7395         layout_index = cursor_index;
7396     }
7397
7398   return layout_index;
7399 }
7400
7401 /**
7402  * gtk_entry_text_index_to_layout_index:
7403  * @entry: a #GtkEntry
7404  * @text_index: byte index into the entry contents
7405  * 
7406  * Converts from a position in the entry's #PangoLayout (returned by
7407  * gtk_entry_get_layout()) to a position in the entry contents
7408  * (returned by gtk_entry_get_text()).
7409  * 
7410  * Return value: byte index into the entry layout text
7411  **/
7412 gint
7413 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7414                                       gint      text_index)
7415 {
7416   GtkEntryPrivate *priv;
7417   PangoLayout *layout;
7418   const gchar *text;
7419   gint cursor_index;
7420
7421   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7422
7423   priv = entry->priv;
7424
7425   layout = gtk_entry_ensure_layout (entry, TRUE);
7426   text = pango_layout_get_text (layout);
7427   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7428
7429   if (text_index > cursor_index)
7430     text_index += priv->preedit_length;
7431
7432   return text_index;
7433 }
7434
7435 /**
7436  * gtk_entry_get_layout_offsets:
7437  * @entry: a #GtkEntry
7438  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
7439  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
7440  *
7441  *
7442  * Obtains the position of the #PangoLayout used to render text
7443  * in the entry, in widget coordinates. Useful if you want to line
7444  * up the text in an entry with some other text, e.g. when using the
7445  * entry to implement editable cells in a sheet widget.
7446  *
7447  * Also useful to convert mouse events into coordinates inside the
7448  * #PangoLayout, e.g. to take some action if some part of the entry text
7449  * is clicked.
7450  * 
7451  * Note that as the user scrolls around in the entry the offsets will
7452  * change; you'll need to connect to the "notify::scroll-offset"
7453  * signal to track this. Remember when using the #PangoLayout
7454  * functions you need to convert to and from pixels using
7455  * PANGO_PIXELS() or #PANGO_SCALE.
7456  *
7457  * Keep in mind that the layout text may contain a preedit string, so
7458  * gtk_entry_layout_index_to_text_index() and
7459  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7460  * indices in the layout to byte indices in the entry contents.
7461  **/
7462 void
7463 gtk_entry_get_layout_offsets (GtkEntry *entry,
7464                               gint     *x,
7465                               gint     *y)
7466 {
7467   gint text_area_x, text_area_y;
7468   
7469   g_return_if_fail (GTK_IS_ENTRY (entry));
7470
7471   /* this gets coords relative to text area */
7472   get_layout_position (entry, x, y);
7473
7474   /* convert to widget coords */
7475   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7476   
7477   if (x)
7478     *x += text_area_x;
7479
7480   if (y)
7481     *y += text_area_y;
7482 }
7483
7484
7485 /**
7486  * gtk_entry_set_alignment:
7487  * @entry: a #GtkEntry
7488  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7489  *          Reversed for RTL layouts
7490  * 
7491  * Sets the alignment for the contents of the entry. This controls
7492  * the horizontal positioning of the contents when the displayed
7493  * text is shorter than the width of the entry.
7494  *
7495  * Since: 2.4
7496  **/
7497 void
7498 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7499 {
7500   GtkEntryPrivate *priv;
7501
7502   g_return_if_fail (GTK_IS_ENTRY (entry));
7503
7504   priv = entry->priv;
7505
7506   if (xalign < 0.0)
7507     xalign = 0.0;
7508   else if (xalign > 1.0)
7509     xalign = 1.0;
7510
7511   if (xalign != priv->xalign)
7512     {
7513       priv->xalign = xalign;
7514
7515       gtk_entry_recompute (entry);
7516
7517       g_object_notify (G_OBJECT (entry), "xalign");
7518     }
7519 }
7520
7521 /**
7522  * gtk_entry_get_alignment:
7523  * @entry: a #GtkEntry
7524  * 
7525  * Gets the value set by gtk_entry_set_alignment().
7526  * 
7527  * Return value: the alignment
7528  *
7529  * Since: 2.4
7530  **/
7531 gfloat
7532 gtk_entry_get_alignment (GtkEntry *entry)
7533 {
7534   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7535
7536   return entry->priv->xalign;
7537 }
7538
7539 /**
7540  * gtk_entry_set_icon_from_pixbuf:
7541  * @entry: a #GtkEntry
7542  * @icon_pos: Icon position
7543  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7544  *
7545  * Sets the icon shown in the specified position using a pixbuf.
7546  *
7547  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7548  *
7549  * Since: 2.16
7550  */
7551 void
7552 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7553                                 GtkEntryIconPosition  icon_pos,
7554                                 GdkPixbuf            *pixbuf)
7555 {
7556   GtkEntryPrivate *priv;
7557   EntryIconInfo *icon_info;
7558
7559   g_return_if_fail (GTK_IS_ENTRY (entry));
7560   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7561
7562   priv = entry->priv;
7563
7564   if ((icon_info = priv->icons[icon_pos]) == NULL)
7565     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7566
7567   g_object_freeze_notify (G_OBJECT (entry));
7568
7569   if (pixbuf)
7570     g_object_ref (pixbuf);
7571
7572   gtk_entry_clear (entry, icon_pos);
7573
7574   if (pixbuf)
7575     {
7576       _gtk_icon_helper_set_pixbuf (icon_info->icon_helper, pixbuf);
7577
7578       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7579         {
7580           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7581           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7582         }
7583       else
7584         {
7585           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7586           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7587         }
7588
7589       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7590           gdk_window_show_unraised (icon_info->window);
7591
7592       g_object_unref (pixbuf);
7593     }
7594   
7595   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7596     gtk_widget_queue_resize (GTK_WIDGET (entry));
7597
7598   g_object_thaw_notify (G_OBJECT (entry));
7599 }
7600
7601 /**
7602  * gtk_entry_set_icon_from_stock:
7603  * @entry: A #GtkEntry
7604  * @icon_pos: Icon position
7605  * @stock_id: (allow-none): The name of the stock item, or %NULL
7606  *
7607  * Sets the icon shown in the entry at the specified position from
7608  * a stock image.
7609  *
7610  * If @stock_id is %NULL, no icon will be shown in the specified position.
7611  *
7612  * Since: 2.16
7613  */
7614 void
7615 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7616                                GtkEntryIconPosition  icon_pos,
7617                                const gchar          *stock_id)
7618 {
7619   GtkEntryPrivate *priv;
7620   EntryIconInfo *icon_info;
7621   gchar *new_id;
7622
7623   g_return_if_fail (GTK_IS_ENTRY (entry));
7624   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7625
7626   priv = entry->priv;
7627
7628   if ((icon_info = priv->icons[icon_pos]) == NULL)
7629     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7630
7631   g_object_freeze_notify (G_OBJECT (entry));
7632
7633   /* need to dup before clearing */
7634   new_id = g_strdup (stock_id);
7635
7636   gtk_entry_clear (entry, icon_pos);
7637
7638   if (new_id != NULL)
7639     {
7640       _gtk_icon_helper_set_stock_id (icon_info->icon_helper, new_id, GTK_ICON_SIZE_MENU);
7641
7642       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7643         {
7644           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7645           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7646         }
7647       else
7648         {
7649           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7650           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7651         }
7652
7653       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7654           gdk_window_show_unraised (icon_info->window);
7655
7656       g_free (new_id);
7657     }
7658
7659   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7660     gtk_widget_queue_resize (GTK_WIDGET (entry));
7661
7662   g_object_thaw_notify (G_OBJECT (entry));
7663 }
7664
7665 /**
7666  * gtk_entry_set_icon_from_icon_name:
7667  * @entry: A #GtkEntry
7668  * @icon_pos: The position at which to set the icon
7669  * @icon_name: (allow-none): An icon name, or %NULL
7670  *
7671  * Sets the icon shown in the entry at the specified position
7672  * from the current icon theme.
7673  *
7674  * If the icon name isn't known, a "broken image" icon will be displayed
7675  * instead.
7676  *
7677  * If @icon_name is %NULL, no icon will be shown in the specified position.
7678  *
7679  * Since: 2.16
7680  */
7681 void
7682 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7683                                    GtkEntryIconPosition  icon_pos,
7684                                    const gchar          *icon_name)
7685 {
7686   GtkEntryPrivate *priv;
7687   EntryIconInfo *icon_info;
7688   gchar *new_name;
7689
7690   g_return_if_fail (GTK_IS_ENTRY (entry));
7691   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7692
7693   priv = entry->priv;
7694
7695   if ((icon_info = priv->icons[icon_pos]) == NULL)
7696     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7697
7698   g_object_freeze_notify (G_OBJECT (entry));
7699
7700   /* need to dup before clearing */
7701   new_name = g_strdup (icon_name);
7702
7703   gtk_entry_clear (entry, icon_pos);
7704
7705   if (new_name != NULL)
7706     {
7707       _gtk_icon_helper_set_icon_name (icon_info->icon_helper, new_name, GTK_ICON_SIZE_MENU);
7708
7709       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7710         {
7711           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7712           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7713         }
7714       else
7715         {
7716           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7717           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7718         }
7719
7720       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7721           gdk_window_show_unraised (icon_info->window);
7722
7723       g_free (new_name);
7724     }
7725
7726   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7727     gtk_widget_queue_resize (GTK_WIDGET (entry));
7728
7729   g_object_thaw_notify (G_OBJECT (entry));
7730 }
7731
7732 /**
7733  * gtk_entry_set_icon_from_gicon:
7734  * @entry: A #GtkEntry
7735  * @icon_pos: The position at which to set the icon
7736  * @icon: (allow-none): The icon to set, or %NULL
7737  *
7738  * Sets the icon shown in the entry at the specified position
7739  * from the current icon theme.
7740  * If the icon isn't known, a "broken image" icon will be displayed
7741  * instead.
7742  *
7743  * If @icon is %NULL, no icon will be shown in the specified position.
7744  *
7745  * Since: 2.16
7746  */
7747 void
7748 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7749                                GtkEntryIconPosition  icon_pos,
7750                                GIcon                *icon)
7751 {
7752   GtkEntryPrivate *priv;
7753   EntryIconInfo *icon_info;
7754
7755   g_return_if_fail (GTK_IS_ENTRY (entry));
7756   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7757
7758   priv = entry->priv;
7759
7760   if ((icon_info = priv->icons[icon_pos]) == NULL)
7761     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7762
7763   g_object_freeze_notify (G_OBJECT (entry));
7764
7765   /* need to ref before clearing */
7766   if (icon)
7767     g_object_ref (icon);
7768
7769   gtk_entry_clear (entry, icon_pos);
7770
7771   if (icon)
7772     {
7773       _gtk_icon_helper_set_gicon (icon_info->icon_helper, icon, GTK_ICON_SIZE_MENU);
7774
7775       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7776         {
7777           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7778           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7779         }
7780       else
7781         {
7782           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7783           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7784         }
7785
7786       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7787           gdk_window_show_unraised (icon_info->window);
7788
7789       g_object_unref (icon);
7790     }
7791
7792   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7793     gtk_widget_queue_resize (GTK_WIDGET (entry));
7794
7795   g_object_thaw_notify (G_OBJECT (entry));
7796 }
7797
7798 /**
7799  * gtk_entry_set_icon_activatable:
7800  * @entry: A #GtkEntry
7801  * @icon_pos: Icon position
7802  * @activatable: %TRUE if the icon should be activatable
7803  *
7804  * Sets whether the icon is activatable.
7805  *
7806  * Since: 2.16
7807  */
7808 void
7809 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7810                                 GtkEntryIconPosition  icon_pos,
7811                                 gboolean              activatable)
7812 {
7813   GtkEntryPrivate *priv;
7814   EntryIconInfo *icon_info;
7815
7816   g_return_if_fail (GTK_IS_ENTRY (entry));
7817   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7818
7819   priv = entry->priv;
7820
7821   if ((icon_info = priv->icons[icon_pos]) == NULL)
7822     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7823
7824   activatable = activatable != FALSE;
7825
7826   if (icon_info->nonactivatable != !activatable)
7827     {
7828       icon_info->nonactivatable = !activatable;
7829
7830       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7831         update_cursors (GTK_WIDGET (entry));
7832
7833       g_object_notify (G_OBJECT (entry),
7834                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7835     }
7836 }
7837
7838 /**
7839  * gtk_entry_get_icon_activatable:
7840  * @entry: a #GtkEntry
7841  * @icon_pos: Icon position
7842  *
7843  * Returns whether the icon is activatable.
7844  *
7845  * Returns: %TRUE if the icon is activatable.
7846  *
7847  * Since: 2.16
7848  */
7849 gboolean
7850 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7851                                 GtkEntryIconPosition  icon_pos)
7852 {
7853   GtkEntryPrivate *priv;
7854   EntryIconInfo *icon_info;
7855
7856   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7857   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7858
7859   priv = entry->priv;
7860   icon_info = priv->icons[icon_pos];
7861
7862   return (!icon_info || !icon_info->nonactivatable);
7863 }
7864
7865 /**
7866  * gtk_entry_get_icon_pixbuf:
7867  * @entry: A #GtkEntry
7868  * @icon_pos: Icon position
7869  *
7870  * Retrieves the image used for the icon.
7871  *
7872  * Unlike the other methods of setting and getting icon data, this
7873  * method will work regardless of whether the icon was set using a
7874  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7875  *
7876  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7877  *     set for this position.
7878  *
7879  * Since: 2.16
7880  */
7881 GdkPixbuf *
7882 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7883                            GtkEntryIconPosition  icon_pos)
7884 {
7885   GtkEntryPrivate *priv;
7886   EntryIconInfo *icon_info;
7887   GdkPixbuf *pixbuf;
7888
7889   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7890   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7891
7892   priv = entry->priv;
7893
7894   icon_info = priv->icons[icon_pos];
7895
7896   if (!icon_info)
7897     return NULL;
7898
7899   /* HACK: unfortunately this is transfer none, so we need to return
7900    * the icon helper's cache ref directly.
7901    */
7902   pixbuf = gtk_entry_ensure_pixbuf (entry, icon_pos);
7903   g_object_unref (pixbuf);
7904
7905   return pixbuf;
7906 }
7907
7908 /**
7909  * gtk_entry_get_icon_gicon:
7910  * @entry: A #GtkEntry
7911  * @icon_pos: Icon position
7912  *
7913  * Retrieves the #GIcon used for the icon, or %NULL if there is
7914  * no icon or if the icon was set by some other method (e.g., by
7915  * stock, pixbuf, or icon name).
7916  *
7917  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
7918  *     or if the icon is not a #GIcon
7919  *
7920  * Since: 2.16
7921  */
7922 GIcon *
7923 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7924                           GtkEntryIconPosition  icon_pos)
7925 {
7926   GtkEntryPrivate *priv;
7927   EntryIconInfo *icon_info;
7928
7929   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7930   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7931
7932   priv = entry->priv;
7933   icon_info = priv->icons[icon_pos];
7934
7935   if (!icon_info)
7936     return NULL;
7937
7938   return _gtk_icon_helper_peek_gicon (icon_info->icon_helper);
7939 }
7940
7941 /**
7942  * gtk_entry_get_icon_stock:
7943  * @entry: A #GtkEntry
7944  * @icon_pos: Icon position
7945  *
7946  * Retrieves the stock id used for the icon, or %NULL if there is
7947  * no icon or if the icon was set by some other method (e.g., by
7948  * pixbuf, icon name or gicon).
7949  *
7950  * Returns: A stock id, or %NULL if no icon is set or if the icon
7951  *          wasn't set from a stock id
7952  *
7953  * Since: 2.16
7954  */
7955 const gchar *
7956 gtk_entry_get_icon_stock (GtkEntry             *entry,
7957                           GtkEntryIconPosition  icon_pos)
7958 {
7959   GtkEntryPrivate *priv;
7960   EntryIconInfo *icon_info;
7961
7962   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7963   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7964
7965   priv = entry->priv;
7966   icon_info = priv->icons[icon_pos];
7967
7968   if (!icon_info)
7969     return NULL;
7970
7971   return _gtk_icon_helper_get_stock_id (icon_info->icon_helper);
7972 }
7973
7974 /**
7975  * gtk_entry_get_icon_name:
7976  * @entry: A #GtkEntry
7977  * @icon_pos: Icon position
7978  *
7979  * Retrieves the icon name used for the icon, or %NULL if there is
7980  * no icon or if the icon was set by some other method (e.g., by
7981  * pixbuf, stock or gicon).
7982  *
7983  * Returns: An icon name, or %NULL if no icon is set or if the icon
7984  *          wasn't set from an icon name
7985  *
7986  * Since: 2.16
7987  */
7988 const gchar *
7989 gtk_entry_get_icon_name (GtkEntry             *entry,
7990                          GtkEntryIconPosition  icon_pos)
7991 {
7992   GtkEntryPrivate *priv;
7993   EntryIconInfo *icon_info;
7994
7995   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7996   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7997
7998   priv = entry->priv;
7999   icon_info = priv->icons[icon_pos];
8000
8001   if (!icon_info)
8002     return NULL;
8003
8004   return _gtk_icon_helper_get_icon_name (icon_info->icon_helper);
8005 }
8006
8007 /**
8008  * gtk_entry_set_icon_sensitive:
8009  * @entry: A #GtkEntry
8010  * @icon_pos: Icon position
8011  * @sensitive: Specifies whether the icon should appear
8012  *             sensitive or insensitive
8013  *
8014  * Sets the sensitivity for the specified icon.
8015  *
8016  * Since: 2.16
8017  */
8018 void
8019 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8020                               GtkEntryIconPosition  icon_pos,
8021                               gboolean              sensitive)
8022 {
8023   GtkEntryPrivate *priv;
8024   EntryIconInfo *icon_info;
8025
8026   g_return_if_fail (GTK_IS_ENTRY (entry));
8027   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8028
8029   priv = entry->priv;
8030
8031   if ((icon_info = priv->icons[icon_pos]) == NULL)
8032     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8033
8034   if (icon_info->insensitive != !sensitive)
8035     {
8036       icon_info->insensitive = !sensitive;
8037
8038       icon_info->pressed = FALSE;
8039       icon_info->prelight = FALSE;
8040
8041       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8042         update_cursors (GTK_WIDGET (entry));
8043
8044       gtk_widget_queue_draw (GTK_WIDGET (entry));
8045
8046       g_object_notify (G_OBJECT (entry),
8047                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8048     }
8049 }
8050
8051 /**
8052  * gtk_entry_get_icon_sensitive:
8053  * @entry: a #GtkEntry
8054  * @icon_pos: Icon position
8055  *
8056  * Returns whether the icon appears sensitive or insensitive.
8057  *
8058  * Returns: %TRUE if the icon is sensitive.
8059  *
8060  * Since: 2.16
8061  */
8062 gboolean
8063 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8064                               GtkEntryIconPosition  icon_pos)
8065 {
8066   GtkEntryPrivate *priv;
8067   EntryIconInfo *icon_info;
8068
8069   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8070   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8071
8072   priv = entry->priv;
8073
8074   icon_info = priv->icons[icon_pos];
8075
8076   return (!icon_info || !icon_info->insensitive);
8077
8078 }
8079
8080 /**
8081  * gtk_entry_get_icon_storage_type:
8082  * @entry: a #GtkEntry
8083  * @icon_pos: Icon position
8084  *
8085  * Gets the type of representation being used by the icon
8086  * to store image data. If the icon has no image data,
8087  * the return value will be %GTK_IMAGE_EMPTY.
8088  *
8089  * Return value: image representation being used
8090  *
8091  * Since: 2.16
8092  **/
8093 GtkImageType
8094 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8095                                  GtkEntryIconPosition  icon_pos)
8096 {
8097   GtkEntryPrivate *priv;
8098   EntryIconInfo *icon_info;
8099
8100   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8101   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8102
8103   priv = entry->priv;
8104
8105   icon_info = priv->icons[icon_pos];
8106
8107   if (!icon_info)
8108     return GTK_IMAGE_EMPTY;
8109
8110   return _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
8111 }
8112
8113 /**
8114  * gtk_entry_get_icon_at_pos:
8115  * @entry: a #GtkEntry
8116  * @x: the x coordinate of the position to find
8117  * @y: the y coordinate of the position to find
8118  *
8119  * Finds the icon at the given position and return its index. The
8120  * position's coordinates are relative to the @entry's top left corner.
8121  * If @x, @y doesn't lie inside an icon, -1 is returned.
8122  * This function is intended for use in a #GtkWidget::query-tooltip
8123  * signal handler.
8124  *
8125  * Returns: the index of the icon at the given position, or -1
8126  *
8127  * Since: 2.16
8128  */
8129 gint
8130 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8131                            gint      x,
8132                            gint      y)
8133 {
8134   GtkAllocation primary;
8135   GtkAllocation secondary;
8136   gint frame_x, frame_y;
8137
8138   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8139
8140   get_frame_size (entry, FALSE, &frame_x, &frame_y, NULL, NULL);
8141   x -= frame_x;
8142   y -= frame_y;
8143
8144   get_icon_allocations (entry, &primary, &secondary);
8145
8146   if (primary.x <= x && x < primary.x + primary.width &&
8147       primary.y <= y && y < primary.y + primary.height)
8148     return GTK_ENTRY_ICON_PRIMARY;
8149
8150   if (secondary.x <= x && x < secondary.x + secondary.width &&
8151       secondary.y <= y && y < secondary.y + secondary.height)
8152     return GTK_ENTRY_ICON_SECONDARY;
8153
8154   return -1;
8155 }
8156
8157 /**
8158  * gtk_entry_set_icon_drag_source:
8159  * @entry: a #GtkIconEntry
8160  * @icon_pos: icon position
8161  * @target_list: the targets (data formats) in which the data can be provided
8162  * @actions: a bitmask of the allowed drag actions
8163  *
8164  * Sets up the icon at the given position so that GTK+ will start a drag
8165  * operation when the user clicks and drags the icon.
8166  *
8167  * To handle the drag operation, you need to connect to the usual
8168  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8169  * signal, and use gtk_entry_get_current_icon_drag_source() in
8170  * your signal handler to find out if the drag was started from
8171  * an icon.
8172  *
8173  * By default, GTK+ uses the icon as the drag icon. You can use the 
8174  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8175  * have to use g_signal_connect_after() to ensure that your signal handler
8176  * gets executed after the default handler.
8177  *
8178  * Since: 2.16
8179  */
8180 void
8181 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8182                                 GtkEntryIconPosition  icon_pos,
8183                                 GtkTargetList        *target_list,
8184                                 GdkDragAction         actions)
8185 {
8186   GtkEntryPrivate *priv;
8187   EntryIconInfo *icon_info;
8188
8189   g_return_if_fail (GTK_IS_ENTRY (entry));
8190   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8191
8192   priv = entry->priv;
8193
8194   if ((icon_info = priv->icons[icon_pos]) == NULL)
8195     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8196
8197   if (icon_info->target_list)
8198     gtk_target_list_unref (icon_info->target_list);
8199   icon_info->target_list = target_list;
8200   if (icon_info->target_list)
8201     gtk_target_list_ref (icon_info->target_list);
8202
8203   icon_info->actions = actions;
8204 }
8205
8206 /**
8207  * gtk_entry_get_current_icon_drag_source:
8208  * @entry: a #GtkIconEntry
8209  *
8210  * Returns the index of the icon which is the source of the current
8211  * DND operation, or -1.
8212  *
8213  * This function is meant to be used in a #GtkWidget::drag-data-get
8214  * callback.
8215  *
8216  * Returns: index of the icon which is the source of the current
8217  *          DND operation, or -1.
8218  *
8219  * Since: 2.16
8220  */
8221 gint
8222 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8223 {
8224   GtkEntryPrivate *priv;
8225   EntryIconInfo *icon_info = NULL;
8226   gint i;
8227
8228   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8229
8230   priv = entry->priv;
8231
8232   for (i = 0; i < MAX_ICONS; i++)
8233     {
8234       if ((icon_info = priv->icons[i]))
8235         {
8236           if (icon_info->in_drag)
8237             return i;
8238         }
8239     }
8240
8241   return -1;
8242 }
8243
8244 /**
8245  * gtk_entry_get_icon_area:
8246  * @entry: A #GtkEntry
8247  * @icon_pos: Icon position
8248  * @icon_area: (out): Return location for the icon's area
8249  *
8250  * Gets the area where entry's icon at @icon_pos is drawn.
8251  * This function is useful when drawing something to the
8252  * entry in a draw callback.
8253  *
8254  * If the entry is not realized or has no icon at the given position,
8255  * @icon_area is filled with zeros.
8256  *
8257  * See also gtk_entry_get_text_area()
8258  *
8259  * Since: 3.0
8260  */
8261 void
8262 gtk_entry_get_icon_area (GtkEntry             *entry,
8263                          GtkEntryIconPosition  icon_pos,
8264                          GdkRectangle         *icon_area)
8265 {
8266   GtkEntryPrivate *priv;
8267   EntryIconInfo *icon_info;
8268
8269   g_return_if_fail (GTK_IS_ENTRY (entry));
8270   g_return_if_fail (icon_area != NULL);
8271
8272   priv = entry->priv;
8273
8274   icon_info = priv->icons[icon_pos];
8275
8276   if (icon_info)
8277     {
8278       GtkAllocation primary;
8279       GtkAllocation secondary;
8280
8281       get_icon_allocations (entry, &primary, &secondary);
8282
8283       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8284         *icon_area = primary;
8285       else
8286         *icon_area = secondary;
8287     }
8288   else
8289     {
8290       icon_area->x = 0;
8291       icon_area->y = 0;
8292       icon_area->width = 0;
8293       icon_area->height = 0;
8294     }
8295 }
8296
8297 static void
8298 ensure_has_tooltip (GtkEntry *entry)
8299 {
8300   GtkEntryPrivate *priv;
8301   EntryIconInfo *icon_info;
8302   int i;
8303   gboolean has_tooltip = FALSE;
8304
8305   priv = entry->priv;
8306
8307   for (i = 0; i < MAX_ICONS; i++)
8308     {
8309       if ((icon_info = priv->icons[i]) != NULL)
8310         {
8311           if (icon_info->tooltip != NULL)
8312             {
8313               has_tooltip = TRUE;
8314               break;
8315             }
8316         }
8317     }
8318
8319   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8320 }
8321
8322 /**
8323  * gtk_entry_get_icon_tooltip_text:
8324  * @entry: a #GtkEntry
8325  * @icon_pos: the icon position
8326  *
8327  * Gets the contents of the tooltip on the icon at the specified 
8328  * position in @entry.
8329  * 
8330  * Returns: the tooltip text, or %NULL. Free the returned string
8331  *     with g_free() when done.
8332  * 
8333  * Since: 2.16
8334  */
8335 gchar *
8336 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8337                                  GtkEntryIconPosition  icon_pos)
8338 {
8339   GtkEntryPrivate *priv;
8340   EntryIconInfo *icon_info;
8341   gchar *text = NULL;
8342
8343   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8344   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8345
8346   priv = entry->priv;
8347
8348   icon_info = priv->icons[icon_pos];
8349
8350   if (!icon_info)
8351     return NULL;
8352  
8353   if (icon_info->tooltip && 
8354       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8355     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8356
8357   return text;
8358 }
8359
8360 /**
8361  * gtk_entry_set_icon_tooltip_text:
8362  * @entry: a #GtkEntry
8363  * @icon_pos: the icon position
8364  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8365  *
8366  * Sets @tooltip as the contents of the tooltip for the icon
8367  * at the specified position.
8368  *
8369  * Use %NULL for @tooltip to remove an existing tooltip.
8370  *
8371  * See also gtk_widget_set_tooltip_text() and 
8372  * gtk_entry_set_icon_tooltip_markup().
8373  *
8374  * Since: 2.16
8375  */
8376 void
8377 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8378                                  GtkEntryIconPosition  icon_pos,
8379                                  const gchar          *tooltip)
8380 {
8381   GtkEntryPrivate *priv;
8382   EntryIconInfo *icon_info;
8383
8384   g_return_if_fail (GTK_IS_ENTRY (entry));
8385   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8386
8387   priv = entry->priv;
8388
8389   if ((icon_info = priv->icons[icon_pos]) == NULL)
8390     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8391
8392   if (icon_info->tooltip)
8393     g_free (icon_info->tooltip);
8394
8395   /* Treat an empty string as a NULL string,
8396    * because an empty string would be useless for a tooltip:
8397    */
8398   if (tooltip && tooltip[0] == '\0')
8399     tooltip = NULL;
8400
8401   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8402
8403   ensure_has_tooltip (entry);
8404 }
8405
8406 /**
8407  * gtk_entry_get_icon_tooltip_markup:
8408  * @entry: a #GtkEntry
8409  * @icon_pos: the icon position
8410  *
8411  * Gets the contents of the tooltip on the icon at the specified 
8412  * position in @entry.
8413  * 
8414  * Returns: the tooltip text, or %NULL. Free the returned string
8415  *     with g_free() when done.
8416  * 
8417  * Since: 2.16
8418  */
8419 gchar *
8420 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8421                                    GtkEntryIconPosition  icon_pos)
8422 {
8423   GtkEntryPrivate *priv;
8424   EntryIconInfo *icon_info;
8425
8426   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8427   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8428
8429   priv = entry->priv;
8430
8431   icon_info = priv->icons[icon_pos];
8432
8433   if (!icon_info)
8434     return NULL;
8435  
8436   return g_strdup (icon_info->tooltip);
8437 }
8438
8439 /**
8440  * gtk_entry_set_icon_tooltip_markup:
8441  * @entry: a #GtkEntry
8442  * @icon_pos: the icon position
8443  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8444  *
8445  * Sets @tooltip as the contents of the tooltip for the icon at
8446  * the specified position. @tooltip is assumed to be marked up with
8447  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8448  *
8449  * Use %NULL for @tooltip to remove an existing tooltip.
8450  *
8451  * See also gtk_widget_set_tooltip_markup() and 
8452  * gtk_enty_set_icon_tooltip_text().
8453  *
8454  * Since: 2.16
8455  */
8456 void
8457 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8458                                    GtkEntryIconPosition  icon_pos,
8459                                    const gchar          *tooltip)
8460 {
8461   GtkEntryPrivate *priv;
8462   EntryIconInfo *icon_info;
8463
8464   g_return_if_fail (GTK_IS_ENTRY (entry));
8465   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8466
8467   priv = entry->priv;
8468
8469   if ((icon_info = priv->icons[icon_pos]) == NULL)
8470     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8471
8472   if (icon_info->tooltip)
8473     g_free (icon_info->tooltip);
8474
8475   /* Treat an empty string as a NULL string,
8476    * because an empty string would be useless for a tooltip:
8477    */
8478   if (tooltip && tooltip[0] == '\0')
8479     tooltip = NULL;
8480
8481   icon_info->tooltip = g_strdup (tooltip);
8482
8483   ensure_has_tooltip (entry);
8484 }
8485
8486 static gboolean
8487 gtk_entry_query_tooltip (GtkWidget  *widget,
8488                          gint        x,
8489                          gint        y,
8490                          gboolean    keyboard_tip,
8491                          GtkTooltip *tooltip)
8492 {
8493   GtkEntry *entry;
8494   GtkEntryPrivate *priv;
8495   EntryIconInfo *icon_info;
8496   gint icon_pos;
8497
8498   entry = GTK_ENTRY (widget);
8499   priv = entry->priv;
8500
8501   if (!keyboard_tip)
8502     {
8503       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8504       if (icon_pos != -1)
8505         {
8506           if ((icon_info = priv->icons[icon_pos]) != NULL)
8507             {
8508               if (icon_info->tooltip)
8509                 {
8510                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8511                   return TRUE;
8512                 }
8513
8514               return FALSE;
8515             }
8516         }
8517     }
8518
8519   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8520                                                                    x, y,
8521                                                                    keyboard_tip,
8522                                                                    tooltip);
8523 }
8524
8525
8526 /* Quick hack of a popup menu
8527  */
8528 static void
8529 activate_cb (GtkWidget *menuitem,
8530              GtkEntry  *entry)
8531 {
8532   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8533   g_signal_emit_by_name (entry, signal);
8534 }
8535
8536
8537 static gboolean
8538 gtk_entry_mnemonic_activate (GtkWidget *widget,
8539                              gboolean   group_cycling)
8540 {
8541   gtk_widget_grab_focus (widget);
8542   return TRUE;
8543 }
8544
8545 static void
8546 append_action_signal (GtkEntry     *entry,
8547                       GtkWidget    *menu,
8548                       const gchar  *stock_id,
8549                       const gchar  *signal,
8550                       gboolean      sensitive)
8551 {
8552   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8553
8554   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8555   g_signal_connect (menuitem, "activate",
8556                     G_CALLBACK (activate_cb), entry);
8557
8558   gtk_widget_set_sensitive (menuitem, sensitive);
8559   
8560   gtk_widget_show (menuitem);
8561   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8562 }
8563         
8564 static void
8565 popup_menu_detach (GtkWidget *attach_widget,
8566                    GtkMenu   *menu)
8567 {
8568   GtkEntry *entry_attach = GTK_ENTRY (attach_widget);
8569   GtkEntryPrivate *priv_attach = entry_attach->priv;
8570
8571   priv_attach->popup_menu = NULL;
8572 }
8573
8574 static void
8575 popup_position_func (GtkMenu   *menu,
8576                      gint      *x,
8577                      gint      *y,
8578                      gboolean  *push_in,
8579                      gpointer   user_data)
8580 {
8581   GtkEntry *entry = GTK_ENTRY (user_data);
8582   GtkEntryPrivate *priv = entry->priv;
8583   GtkWidget *widget = GTK_WIDGET (entry);
8584   GdkScreen *screen;
8585   GtkRequisition menu_req;
8586   GdkRectangle monitor;
8587   GtkBorder inner_border;
8588   gint monitor_num, strong_x, height;
8589  
8590   g_return_if_fail (gtk_widget_get_realized (widget));
8591
8592   gdk_window_get_origin (priv->text_area, x, y);
8593
8594   screen = gtk_widget_get_screen (widget);
8595   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->text_area);
8596   if (monitor_num < 0)
8597     monitor_num = 0;
8598   gtk_menu_set_monitor (menu, monitor_num);
8599
8600   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
8601   gtk_widget_get_preferred_size (priv->popup_menu,
8602                                  &menu_req, NULL);
8603   height = gdk_window_get_height (priv->text_area);
8604   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8605   _gtk_entry_effective_inner_border (entry, &inner_border);
8606
8607   *x += inner_border.left + strong_x - priv->scroll_offset;
8608   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8609     *x -= menu_req.width;
8610
8611   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8612     *y += height;
8613   else if ((*y - menu_req.height) >= monitor.y)
8614     *y -= menu_req.height;
8615   else if (monitor.y + monitor.height - (*y + height) > *y)
8616     *y += height;
8617   else
8618     *y -= menu_req.height;
8619
8620   *push_in = FALSE;
8621 }
8622
8623 static void
8624 unichar_chosen_func (const char *text,
8625                      gpointer    data)
8626 {
8627   GtkEntry *entry = GTK_ENTRY (data);
8628   GtkEntryPrivate *priv = entry->priv;
8629
8630   if (priv->editable)
8631     gtk_entry_enter_text (entry, text);
8632 }
8633
8634 typedef struct
8635 {
8636   GtkEntry *entry;
8637   gint button;
8638   guint time;
8639   GdkDevice *device;
8640 } PopupInfo;
8641
8642 static void
8643 popup_targets_received (GtkClipboard     *clipboard,
8644                         GtkSelectionData *data,
8645                         gpointer          user_data)
8646 {
8647   PopupInfo *info = user_data;
8648   GtkEntry *entry = info->entry;
8649   GtkEntryPrivate *info_entry_priv = entry->priv;
8650
8651   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8652     {
8653       DisplayMode mode;
8654       gboolean clipboard_contains_text;
8655       GtkWidget *menuitem;
8656       GtkWidget *submenu;
8657       gboolean show_input_method_menu;
8658       gboolean show_unicode_menu;
8659       
8660       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8661       if (info_entry_priv->popup_menu)
8662         gtk_widget_destroy (info_entry_priv->popup_menu);
8663
8664       info_entry_priv->popup_menu = gtk_menu_new ();
8665
8666       gtk_menu_attach_to_widget (GTK_MENU (info_entry_priv->popup_menu),
8667                                  GTK_WIDGET (entry),
8668                                  popup_menu_detach);
8669       
8670       mode = gtk_entry_get_display_mode (entry);
8671       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8672                             info_entry_priv->editable && mode == DISPLAY_NORMAL &&
8673                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8674
8675       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8676                             mode == DISPLAY_NORMAL &&
8677                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8678
8679       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8680                             info_entry_priv->editable && clipboard_contains_text);
8681
8682       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8683       gtk_widget_set_sensitive (menuitem, info_entry_priv->editable && info_entry_priv->current_pos != info_entry_priv->selection_bound);
8684       g_signal_connect_swapped (menuitem, "activate",
8685                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8686       gtk_widget_show (menuitem);
8687       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8688
8689       menuitem = gtk_separator_menu_item_new ();
8690       gtk_widget_show (menuitem);
8691       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8692       
8693       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8694       gtk_widget_set_sensitive (menuitem, gtk_entry_buffer_get_length (info_entry_priv->buffer) > 0);
8695       g_signal_connect_swapped (menuitem, "activate",
8696                                 G_CALLBACK (gtk_entry_select_all), entry);
8697       gtk_widget_show (menuitem);
8698       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8699       
8700       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8701                     "gtk-show-input-method-menu", &show_input_method_menu,
8702                     "gtk-show-unicode-menu", &show_unicode_menu,
8703                     NULL);
8704
8705       if (show_input_method_menu || show_unicode_menu)
8706         {
8707           menuitem = gtk_separator_menu_item_new ();
8708           gtk_widget_show (menuitem);
8709           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8710         }
8711       
8712       if (show_input_method_menu)
8713         {
8714           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8715           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8716           gtk_widget_show (menuitem);
8717           submenu = gtk_menu_new ();
8718           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8719
8720           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8721
8722           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (info_entry_priv->im_context),
8723                                                 GTK_MENU_SHELL (submenu));
8724         }
8725       
8726       if (show_unicode_menu)
8727         {
8728           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8729           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8730           gtk_widget_show (menuitem);
8731           
8732           submenu = gtk_menu_new ();
8733           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8734           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8735
8736           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8737                                                         unichar_chosen_func,
8738                                                         entry);
8739         }
8740       
8741       g_signal_emit (entry,
8742                      signals[POPULATE_POPUP],
8743                      0,
8744                      info_entry_priv->popup_menu);
8745
8746
8747       if (info->device)
8748         gtk_menu_popup_for_device (GTK_MENU (info_entry_priv->popup_menu),
8749                         info->device, NULL, NULL, NULL, NULL, NULL,
8750                         info->button, info->time);
8751       else
8752         {
8753           gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8754                           popup_position_func, entry,
8755                           0, gtk_get_current_event_time ());
8756           gtk_menu_shell_select_first (GTK_MENU_SHELL (info_entry_priv->popup_menu), FALSE);
8757         }
8758     }
8759
8760   g_object_unref (entry);
8761   g_slice_free (PopupInfo, info);
8762 }
8763                         
8764 static void
8765 gtk_entry_do_popup (GtkEntry       *entry,
8766                     GdkEventButton *event)
8767 {
8768   PopupInfo *info = g_slice_new (PopupInfo);
8769
8770   /* In order to know what entries we should make sensitive, we
8771    * ask for the current targets of the clipboard, and when
8772    * we get them, then we actually pop up the menu.
8773    */
8774   info->entry = g_object_ref (entry);
8775   
8776   if (event)
8777     {
8778       info->button = event->button;
8779       info->time = event->time;
8780       info->device = event->device;
8781     }
8782   else
8783     {
8784       info->button = 0;
8785       info->time = gtk_get_current_event_time ();
8786       info->device = NULL;
8787     }
8788
8789   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8790                                   gdk_atom_intern_static_string ("TARGETS"),
8791                                   popup_targets_received,
8792                                   info);
8793 }
8794
8795 static gboolean
8796 gtk_entry_popup_menu (GtkWidget *widget)
8797 {
8798   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8799   return TRUE;
8800 }
8801
8802 static void
8803 gtk_entry_drag_begin (GtkWidget      *widget,
8804                       GdkDragContext *context)
8805 {
8806   GtkEntry *entry = GTK_ENTRY (widget);
8807   GtkEntryPrivate *priv = entry->priv;
8808   gint i;
8809
8810   for (i = 0; i < MAX_ICONS; i++)
8811     {
8812       EntryIconInfo *icon_info = priv->icons[i];
8813       
8814       if (icon_info != NULL)
8815         {
8816           if (icon_info->in_drag) 
8817             {
8818               GdkPixbuf *pix;
8819
8820               pix = _gtk_icon_helper_ensure_pixbuf
8821                 (icon_info->icon_helper,
8822                  gtk_widget_get_style_context (GTK_WIDGET (entry)));
8823               gtk_drag_set_icon_pixbuf (context, pix, -2, -2);
8824
8825               g_object_unref (pix);
8826             }
8827         }
8828     }
8829 }
8830
8831 static void
8832 gtk_entry_drag_end (GtkWidget      *widget,
8833                     GdkDragContext *context)
8834 {
8835   GtkEntry *entry = GTK_ENTRY (widget);
8836   GtkEntryPrivate *priv = entry->priv;
8837   gint i;
8838
8839   for (i = 0; i < MAX_ICONS; i++)
8840     {
8841       EntryIconInfo *icon_info = priv->icons[i];
8842
8843       if (icon_info != NULL)
8844         icon_info->in_drag = 0;
8845     }
8846 }
8847
8848 static void
8849 gtk_entry_drag_leave (GtkWidget        *widget,
8850                       GdkDragContext   *context,
8851                       guint             time)
8852 {
8853   GtkEntry *entry = GTK_ENTRY (widget);
8854   GtkEntryPrivate *priv = entry->priv;
8855
8856   priv->dnd_position = -1;
8857   gtk_widget_queue_draw (widget);
8858 }
8859
8860 static gboolean
8861 gtk_entry_drag_drop  (GtkWidget        *widget,
8862                       GdkDragContext   *context,
8863                       gint              x,
8864                       gint              y,
8865                       guint             time)
8866 {
8867   GtkEntry *entry = GTK_ENTRY (widget);
8868   GtkEntryPrivate *priv = entry->priv;
8869   GdkAtom target = GDK_NONE;
8870
8871   if (priv->editable)
8872     target = gtk_drag_dest_find_target (widget, context, NULL);
8873
8874   if (target != GDK_NONE)
8875     gtk_drag_get_data (widget, context, target, time);
8876   else
8877     gtk_drag_finish (context, FALSE, FALSE, time);
8878   
8879   return TRUE;
8880 }
8881
8882 static gboolean
8883 gtk_entry_drag_motion (GtkWidget        *widget,
8884                        GdkDragContext   *context,
8885                        gint              x,
8886                        gint              y,
8887                        guint             time)
8888 {
8889   GtkEntry *entry = GTK_ENTRY (widget);
8890   GtkEntryPrivate *priv = entry->priv;
8891   GtkStyleContext *style_context;
8892   GtkWidget *source_widget;
8893   GdkDragAction suggested_action;
8894   gint new_position, old_position;
8895   gint sel1, sel2;
8896   GtkBorder padding;
8897
8898   style_context = gtk_widget_get_style_context (widget);
8899   gtk_style_context_get_padding (style_context, 0, &padding);
8900   x -= padding.left;
8901   y -= padding.top;
8902
8903   old_position = priv->dnd_position;
8904   new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8905
8906   if (priv->editable &&
8907       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8908     {
8909       source_widget = gtk_drag_get_source_widget (context);
8910       suggested_action = gdk_drag_context_get_suggested_action (context);
8911
8912       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8913           new_position < sel1 || new_position > sel2)
8914         {
8915           if (source_widget == widget)
8916             {
8917               /* Default to MOVE, unless the user has
8918                * pressed ctrl or alt to affect available actions
8919                */
8920               if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
8921                 suggested_action = GDK_ACTION_MOVE;
8922             }
8923
8924           priv->dnd_position = new_position;
8925         }
8926       else
8927         {
8928           if (source_widget == widget)
8929             suggested_action = 0;       /* Can't drop in selection where drag started */
8930
8931           priv->dnd_position = -1;
8932         }
8933     }
8934   else
8935     {
8936       /* Entry not editable, or no text */
8937       suggested_action = 0;
8938       priv->dnd_position = -1;
8939     }
8940   
8941   gdk_drag_status (context, suggested_action, time);
8942
8943   if (priv->dnd_position != old_position)
8944     gtk_widget_queue_draw (widget);
8945
8946   return TRUE;
8947 }
8948
8949 static void
8950 gtk_entry_drag_data_received (GtkWidget        *widget,
8951                               GdkDragContext   *context,
8952                               gint              x,
8953                               gint              y,
8954                               GtkSelectionData *selection_data,
8955                               guint             info,
8956                               guint             time)
8957 {
8958   GtkEntry *entry = GTK_ENTRY (widget);
8959   GtkEntryPrivate *priv = entry->priv;
8960   GtkEditable *editable = GTK_EDITABLE (widget);
8961   GtkStyleContext *style_context;
8962   GtkBorder padding;
8963   gchar *str;
8964
8965   str = (gchar *) gtk_selection_data_get_text (selection_data);
8966
8967   style_context = gtk_widget_get_style_context (widget);
8968   gtk_style_context_get_padding (style_context, 0, &padding);
8969   x -= padding.left;
8970   y -= padding.top;
8971
8972   if (str && priv->editable)
8973     {
8974       gint new_position;
8975       gint sel1, sel2;
8976       gint length = -1;
8977
8978       if (priv->truncate_multiline)
8979         length = truncate_multiline (str);
8980
8981       new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8982
8983       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8984           new_position < sel1 || new_position > sel2)
8985         {
8986           gtk_editable_insert_text (editable, str, length, &new_position);
8987         }
8988       else
8989         {
8990           /* Replacing selection */
8991           begin_change (entry);
8992           gtk_editable_delete_text (editable, sel1, sel2);
8993           gtk_editable_insert_text (editable, str, length, &sel1);
8994           end_change (entry);
8995         }
8996       
8997       gtk_drag_finish (context, TRUE, gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
8998     }
8999   else
9000     {
9001       /* Drag and drop didn't happen! */
9002       gtk_drag_finish (context, FALSE, FALSE, time);
9003     }
9004
9005   g_free (str);
9006 }
9007
9008 static void
9009 gtk_entry_drag_data_get (GtkWidget        *widget,
9010                          GdkDragContext   *context,
9011                          GtkSelectionData *selection_data,
9012                          guint             info,
9013                          guint             time)
9014 {
9015   GtkEntry *entry = GTK_ENTRY (widget);
9016   GtkEntryPrivate *priv = entry->priv;
9017   GtkEditable *editable = GTK_EDITABLE (widget);
9018   gint sel_start, sel_end;
9019   gint i;
9020
9021   /* If there is an icon drag going on, exit early. */
9022   for (i = 0; i < MAX_ICONS; i++)
9023     {
9024       EntryIconInfo *icon_info = priv->icons[i];
9025
9026       if (icon_info != NULL)
9027         {
9028           if (icon_info->in_drag)
9029             return;
9030         }
9031     }
9032
9033   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9034     {
9035       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9036
9037       gtk_selection_data_set_text (selection_data, str, -1);
9038       
9039       g_free (str);
9040     }
9041
9042 }
9043
9044 static void
9045 gtk_entry_drag_data_delete (GtkWidget      *widget,
9046                             GdkDragContext *context)
9047 {
9048   GtkEntry *entry = GTK_ENTRY (widget);
9049   GtkEntryPrivate *priv = entry->priv;
9050   GtkEditable *editable = GTK_EDITABLE (widget);
9051   gint sel_start, sel_end;
9052   gint i;
9053
9054   /* If there is an icon drag going on, exit early. */
9055   for (i = 0; i < MAX_ICONS; i++)
9056     {
9057       EntryIconInfo *icon_info = priv->icons[i];
9058
9059       if (icon_info != NULL)
9060         {
9061           if (icon_info->in_drag)
9062             return;
9063         }
9064     }
9065
9066   if (priv->editable &&
9067       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9068     gtk_editable_delete_text (editable, sel_start, sel_end);
9069 }
9070
9071 /* We display the cursor when
9072  *
9073  *  - the selection is empty, AND
9074  *  - the widget has focus
9075  */
9076
9077 #define CURSOR_ON_MULTIPLIER 2
9078 #define CURSOR_OFF_MULTIPLIER 1
9079 #define CURSOR_PEND_MULTIPLIER 3
9080 #define CURSOR_DIVIDER 3
9081
9082 static gboolean
9083 cursor_blinks (GtkEntry *entry)
9084 {
9085   GtkEntryPrivate *priv = entry->priv;
9086
9087   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9088       priv->editable &&
9089       priv->selection_bound == priv->current_pos)
9090     {
9091       GtkSettings *settings;
9092       gboolean blink;
9093
9094       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9095       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9096
9097       return blink;
9098     }
9099   else
9100     return FALSE;
9101 }
9102
9103 static gint
9104 get_cursor_time (GtkEntry *entry)
9105 {
9106   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9107   gint time;
9108
9109   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9110
9111   return time;
9112 }
9113
9114 static gint
9115 get_cursor_blink_timeout (GtkEntry *entry)
9116 {
9117   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9118   gint timeout;
9119
9120   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9121
9122   return timeout;
9123 }
9124
9125 static void
9126 show_cursor (GtkEntry *entry)
9127 {
9128   GtkEntryPrivate *priv = entry->priv;
9129   GtkWidget *widget;
9130
9131   if (!priv->cursor_visible)
9132     {
9133       priv->cursor_visible = TRUE;
9134
9135       widget = GTK_WIDGET (entry);
9136       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9137         gtk_widget_queue_draw (widget);
9138     }
9139 }
9140
9141 static void
9142 hide_cursor (GtkEntry *entry)
9143 {
9144   GtkEntryPrivate *priv = entry->priv;
9145   GtkWidget *widget;
9146
9147   if (priv->cursor_visible)
9148     {
9149       priv->cursor_visible = FALSE;
9150
9151       widget = GTK_WIDGET (entry);
9152       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9153         gtk_widget_queue_draw (widget);
9154     }
9155 }
9156
9157 /*
9158  * Blink!
9159  */
9160 static gint
9161 blink_cb (gpointer data)
9162 {
9163   GtkEntry *entry;
9164   GtkEntryPrivate *priv; 
9165   gint blink_timeout;
9166
9167   entry = GTK_ENTRY (data);
9168   priv = entry->priv;
9169  
9170   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9171     {
9172       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9173                  "connect a handler to this signal, it must return\n"
9174                  "FALSE so the entry gets the event as well");
9175
9176       gtk_entry_check_cursor_blink (entry);
9177
9178       return FALSE;
9179     }
9180   
9181   g_assert (priv->selection_bound == priv->current_pos);
9182   
9183   blink_timeout = get_cursor_blink_timeout (entry);
9184   if (priv->blink_time > 1000 * blink_timeout && 
9185       blink_timeout < G_MAXINT/1000) 
9186     {
9187       /* we've blinked enough without the user doing anything, stop blinking */
9188       show_cursor (entry);
9189       priv->blink_timeout = 0;
9190     } 
9191   else if (priv->cursor_visible)
9192     {
9193       hide_cursor (entry);
9194       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9195                                             blink_cb,
9196                                             entry);
9197     }
9198   else
9199     {
9200       show_cursor (entry);
9201       priv->blink_time += get_cursor_time (entry);
9202       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9203                                             blink_cb,
9204                                             entry);
9205     }
9206
9207   /* Remove ourselves */
9208   return FALSE;
9209 }
9210
9211 static void
9212 gtk_entry_check_cursor_blink (GtkEntry *entry)
9213 {
9214   GtkEntryPrivate *priv = entry->priv;
9215
9216   if (cursor_blinks (entry))
9217     {
9218       if (!priv->blink_timeout)
9219         {
9220           show_cursor (entry);
9221           priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9222                                                 blink_cb,
9223                                                 entry);
9224         }
9225     }
9226   else
9227     {
9228       if (priv->blink_timeout)
9229         {
9230           g_source_remove (priv->blink_timeout);
9231           priv->blink_timeout = 0;
9232         }
9233
9234       priv->cursor_visible = TRUE;
9235     }
9236 }
9237
9238 static void
9239 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9240 {
9241   GtkEntryPrivate *priv = entry->priv;
9242
9243   if (cursor_blinks (entry))
9244     {
9245       if (priv->blink_timeout != 0)
9246         g_source_remove (priv->blink_timeout);
9247
9248       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9249                                                      blink_cb,
9250                                                      entry);
9251       show_cursor (entry);
9252     }
9253 }
9254
9255 static void
9256 gtk_entry_reset_blink_time (GtkEntry *entry)
9257 {
9258   GtkEntryPrivate *priv = entry->priv;
9259
9260   priv->blink_time = 0;
9261 }
9262
9263
9264 /* completion */
9265 static gint
9266 gtk_entry_completion_timeout (gpointer data)
9267 {
9268   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9269   GtkEntryPrivate *completion_entry_priv = GTK_ENTRY (completion->priv->entry)->priv;
9270
9271   completion->priv->completion_timeout = 0;
9272
9273   if (completion->priv->filter_model &&
9274       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9275       >= completion->priv->minimum_key_length)
9276     {
9277       gint matches;
9278       gint actions;
9279       GtkTreeSelection *s;
9280       gboolean popup_single;
9281
9282       gtk_entry_completion_complete (completion);
9283       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9284
9285       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9286
9287       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9288
9289       gtk_tree_selection_unselect_all (s);
9290
9291       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9292
9293       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9294       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9295         { 
9296           if (gtk_widget_get_visible (completion->priv->popup_window))
9297             _gtk_entry_completion_resize_popup (completion);
9298           else
9299             _gtk_entry_completion_popup (completion, completion_entry_priv->completion_device);
9300         }
9301       else 
9302         _gtk_entry_completion_popdown (completion);
9303     }
9304   else if (gtk_widget_get_visible (completion->priv->popup_window))
9305     _gtk_entry_completion_popdown (completion);
9306
9307   return FALSE;
9308 }
9309
9310 static inline gboolean
9311 keyval_is_cursor_move (guint keyval)
9312 {
9313   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9314     return TRUE;
9315
9316   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9317     return TRUE;
9318
9319   if (keyval == GDK_KEY_Page_Up)
9320     return TRUE;
9321
9322   if (keyval == GDK_KEY_Page_Down)
9323     return TRUE;
9324
9325   return FALSE;
9326 }
9327
9328 static gboolean
9329 gtk_entry_completion_key_press (GtkWidget   *widget,
9330                                 GdkEventKey *event,
9331                                 gpointer     user_data)
9332 {
9333   gint matches, actions = 0;
9334   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9335
9336   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9337     return FALSE;
9338
9339   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9340
9341   if (completion->priv->actions)
9342     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9343
9344   if (keyval_is_cursor_move (event->keyval))
9345     {
9346       GtkTreePath *path = NULL;
9347       
9348       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9349         {
9350           if (completion->priv->current_selected < 0)
9351             completion->priv->current_selected = matches + actions - 1;
9352           else
9353             completion->priv->current_selected--;
9354         }
9355       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9356         {
9357           if (completion->priv->current_selected < matches + actions - 1)
9358             completion->priv->current_selected++;
9359           else
9360             completion->priv->current_selected = -1;
9361         }
9362       else if (event->keyval == GDK_KEY_Page_Up)
9363         {
9364           if (completion->priv->current_selected < 0)
9365             completion->priv->current_selected = matches + actions - 1;
9366           else if (completion->priv->current_selected == 0)
9367             completion->priv->current_selected = -1;
9368           else if (completion->priv->current_selected < matches) 
9369             {
9370               completion->priv->current_selected -= PAGE_STEP;
9371               if (completion->priv->current_selected < 0)
9372                 completion->priv->current_selected = 0;
9373             }
9374           else 
9375             {
9376               completion->priv->current_selected -= PAGE_STEP;
9377               if (completion->priv->current_selected < matches - 1)
9378                 completion->priv->current_selected = matches - 1;
9379             }
9380         }
9381       else if (event->keyval == GDK_KEY_Page_Down)
9382         {
9383           if (completion->priv->current_selected < 0)
9384             completion->priv->current_selected = 0;
9385           else if (completion->priv->current_selected < matches - 1)
9386             {
9387               completion->priv->current_selected += PAGE_STEP;
9388               if (completion->priv->current_selected > matches - 1)
9389                 completion->priv->current_selected = matches - 1;
9390             }
9391           else if (completion->priv->current_selected == matches + actions - 1)
9392             {
9393               completion->priv->current_selected = -1;
9394             }
9395           else
9396             {
9397               completion->priv->current_selected += PAGE_STEP;
9398               if (completion->priv->current_selected > matches + actions - 1)
9399                 completion->priv->current_selected = matches + actions - 1;
9400             }
9401         }
9402
9403       if (completion->priv->current_selected < 0)
9404         {
9405           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9406           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9407
9408           if (completion->priv->inline_selection &&
9409               completion->priv->completion_prefix)
9410             {
9411               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9412                                   completion->priv->completion_prefix);
9413               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9414             }
9415         }
9416       else if (completion->priv->current_selected < matches)
9417         {
9418           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9419
9420           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9421           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9422                                     path, NULL, FALSE);
9423
9424           if (completion->priv->inline_selection)
9425             {
9426
9427               GtkTreeIter iter;
9428               GtkTreeIter child_iter;
9429               GtkTreeModel *model = NULL;
9430               GtkTreeSelection *sel;
9431               gboolean entry_set;
9432
9433               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9434               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9435                 return FALSE;
9436
9437               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9438               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9439               
9440               if (completion->priv->completion_prefix == NULL)
9441                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9442
9443               g_signal_emit_by_name (completion, "cursor-on-match", model,
9444                                      &child_iter, &entry_set);
9445             }
9446         }
9447       else if (completion->priv->current_selected - matches >= 0)
9448         {
9449           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9450
9451           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9452           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9453                                     path, NULL, FALSE);
9454
9455           if (completion->priv->inline_selection &&
9456               completion->priv->completion_prefix)
9457             {
9458               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9459                                   completion->priv->completion_prefix);
9460               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9461             }
9462         }
9463
9464       gtk_tree_path_free (path);
9465
9466       return TRUE;
9467     }
9468   else if (event->keyval == GDK_KEY_Escape ||
9469            event->keyval == GDK_KEY_Left ||
9470            event->keyval == GDK_KEY_KP_Left ||
9471            event->keyval == GDK_KEY_Right ||
9472            event->keyval == GDK_KEY_KP_Right) 
9473     {
9474       gboolean retval = TRUE;
9475
9476       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9477       _gtk_entry_completion_popdown (completion);
9478
9479       if (completion->priv->current_selected < 0)
9480         {
9481           retval = FALSE;
9482           goto keypress_completion_out;
9483         }
9484       else if (completion->priv->inline_selection)
9485         {
9486           /* Escape rejects the tentative completion */
9487           if (event->keyval == GDK_KEY_Escape)
9488             {
9489               if (completion->priv->completion_prefix)
9490                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9491                                     completion->priv->completion_prefix);
9492               else 
9493                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9494             }
9495
9496           /* Move the cursor to the end for Right/Esc, to the
9497              beginning for Left */
9498           if (event->keyval == GDK_KEY_Right ||
9499               event->keyval == GDK_KEY_KP_Right ||
9500               event->keyval == GDK_KEY_Escape)
9501             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9502           else
9503             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9504         }
9505
9506 keypress_completion_out:
9507       if (completion->priv->inline_selection)
9508         {
9509           g_free (completion->priv->completion_prefix);
9510           completion->priv->completion_prefix = NULL;
9511         }
9512
9513       return retval;
9514     }
9515   else if (event->keyval == GDK_KEY_Tab || 
9516            event->keyval == GDK_KEY_KP_Tab ||
9517            event->keyval == GDK_KEY_ISO_Left_Tab) 
9518     {
9519       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9520       _gtk_entry_completion_popdown (completion);
9521
9522       g_free (completion->priv->completion_prefix);
9523       completion->priv->completion_prefix = NULL;
9524
9525       return FALSE;
9526     }
9527   else if (event->keyval == GDK_KEY_ISO_Enter ||
9528            event->keyval == GDK_KEY_KP_Enter ||
9529            event->keyval == GDK_KEY_Return)
9530     {
9531       GtkTreeIter iter;
9532       GtkTreeModel *model = NULL;
9533       GtkTreeModel *child_model;
9534       GtkTreeIter child_iter;
9535       GtkTreeSelection *sel;
9536       gboolean retval = TRUE;
9537
9538       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9539       _gtk_entry_completion_popdown (completion);
9540
9541       if (completion->priv->current_selected < matches)
9542         {
9543           gboolean entry_set;
9544
9545           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9546           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9547             {
9548               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9549               child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9550               g_signal_handler_block (widget, completion->priv->changed_id);
9551               g_signal_emit_by_name (completion, "match-selected",
9552                                      child_model, &child_iter, &entry_set);
9553               g_signal_handler_unblock (widget, completion->priv->changed_id);
9554
9555               if (!entry_set)
9556                 {
9557                   gchar *str = NULL;
9558
9559                   gtk_tree_model_get (model, &iter,
9560                                       completion->priv->text_column, &str,
9561                                       -1);
9562
9563                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9564
9565                   /* move the cursor to the end */
9566                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9567
9568                   g_free (str);
9569                 }
9570             }
9571           else
9572             retval = FALSE;
9573         }
9574       else if (completion->priv->current_selected - matches >= 0)
9575         {
9576           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9577           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9578             {
9579               GtkTreePath *path;
9580
9581               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9582               g_signal_emit_by_name (completion, "action-activated",
9583                                      gtk_tree_path_get_indices (path)[0]);
9584               gtk_tree_path_free (path);
9585             }
9586           else
9587             retval = FALSE;
9588         }
9589
9590       g_free (completion->priv->completion_prefix);
9591       completion->priv->completion_prefix = NULL;
9592
9593       return retval;
9594     }
9595
9596   return FALSE;
9597 }
9598
9599 static void
9600 gtk_entry_completion_changed (GtkWidget *widget,
9601                               gpointer   user_data)
9602 {
9603   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9604   GtkEntry *entry = GTK_ENTRY (widget);
9605   GtkEntryPrivate *priv = entry->priv;
9606   GdkDevice *device;
9607
9608   /* (re)install completion timeout */
9609   if (completion->priv->completion_timeout)
9610     g_source_remove (completion->priv->completion_timeout);
9611
9612   if (!gtk_entry_get_text (entry))
9613     return;
9614
9615   /* no need to normalize for this test */
9616   if (completion->priv->minimum_key_length > 0 &&
9617       strcmp ("", gtk_entry_get_text (entry)) == 0)
9618     {
9619       if (gtk_widget_get_visible (completion->priv->popup_window))
9620         _gtk_entry_completion_popdown (completion);
9621       return;
9622     }
9623
9624   device = gtk_get_current_event_device ();
9625
9626   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
9627     device = gdk_device_get_associated_device (device);
9628
9629   if (device)
9630     priv->completion_device = device;
9631
9632   completion->priv->completion_timeout =
9633     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9634                    gtk_entry_completion_timeout,
9635                    completion);
9636 }
9637
9638 static gboolean
9639 check_completion_callback (GtkEntryCompletion *completion)
9640 {
9641   completion->priv->check_completion_idle = NULL;
9642   
9643   gtk_entry_completion_complete (completion);
9644   gtk_entry_completion_insert_prefix (completion);
9645
9646   return FALSE;
9647 }
9648
9649 static void
9650 clear_completion_callback (GtkEntry   *entry,
9651                            GParamSpec *pspec)
9652 {
9653   if (pspec->name == I_("cursor-position") ||
9654       pspec->name == I_("selection-bound"))
9655     {
9656       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9657       
9658       completion->priv->has_completion = FALSE;
9659     }
9660 }
9661
9662 static gboolean
9663 accept_completion_callback (GtkEntry *entry)
9664 {
9665   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9666
9667   if (completion->priv->has_completion)
9668     gtk_editable_set_position (GTK_EDITABLE (entry),
9669                                gtk_entry_buffer_get_length (get_buffer (entry)));
9670
9671   return FALSE;
9672 }
9673
9674 static void
9675 completion_insert_text_callback (GtkEntry           *entry,
9676                                  const gchar        *text,
9677                                  gint                length,
9678                                  gint                position,
9679                                  GtkEntryCompletion *completion)
9680 {
9681   /* idle to update the selection based on the file list */
9682   if (completion->priv->check_completion_idle == NULL)
9683     {
9684       completion->priv->check_completion_idle = g_idle_source_new ();
9685       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9686       g_source_set_closure (completion->priv->check_completion_idle,
9687                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9688                                                    G_OBJECT (completion)));
9689       g_source_attach (completion->priv->check_completion_idle, NULL);
9690     }
9691 }
9692
9693 static void
9694 completion_changed (GtkEntryCompletion *completion,
9695                     GParamSpec         *pspec,
9696                     gpointer            data)
9697 {
9698   GtkEntry *entry = GTK_ENTRY (data);
9699
9700   if (pspec->name == I_("popup-completion") ||
9701       pspec->name == I_("inline-completion"))
9702     {
9703       disconnect_completion_signals (entry, completion);
9704       connect_completion_signals (entry, completion);
9705     }
9706 }
9707
9708 static void
9709 disconnect_completion_signals (GtkEntry           *entry,
9710                                GtkEntryCompletion *completion)
9711 {
9712   g_signal_handlers_disconnect_by_func (completion, 
9713                                        G_CALLBACK (completion_changed), entry);
9714   if (completion->priv->changed_id > 0 &&
9715       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9716     {
9717       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9718       completion->priv->changed_id = 0;
9719     }
9720   g_signal_handlers_disconnect_by_func (entry, 
9721                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9722   if (completion->priv->insert_text_id > 0 &&
9723       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9724     {
9725       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9726       completion->priv->insert_text_id = 0;
9727     }
9728   g_signal_handlers_disconnect_by_func (entry, 
9729                                         G_CALLBACK (completion_insert_text_callback), completion);
9730   g_signal_handlers_disconnect_by_func (entry, 
9731                                         G_CALLBACK (clear_completion_callback), completion);
9732   g_signal_handlers_disconnect_by_func (entry, 
9733                                         G_CALLBACK (accept_completion_callback), completion);
9734 }
9735
9736 static void
9737 connect_completion_signals (GtkEntry           *entry,
9738                             GtkEntryCompletion *completion)
9739 {
9740   if (completion->priv->popup_completion)
9741     {
9742       completion->priv->changed_id =
9743         g_signal_connect (entry, "changed",
9744                           G_CALLBACK (gtk_entry_completion_changed), completion);
9745       g_signal_connect (entry, "key-press-event",
9746                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9747     }
9748  
9749   if (completion->priv->inline_completion)
9750     {
9751       completion->priv->insert_text_id =
9752         g_signal_connect (entry, "insert-text",
9753                           G_CALLBACK (completion_insert_text_callback), completion);
9754       g_signal_connect (entry, "notify",
9755                         G_CALLBACK (clear_completion_callback), completion);
9756       g_signal_connect (entry, "activate",
9757                         G_CALLBACK (accept_completion_callback), completion);
9758       g_signal_connect (entry, "focus-out-event",
9759                         G_CALLBACK (accept_completion_callback), completion);
9760     }
9761
9762   g_signal_connect (completion, "notify",
9763                     G_CALLBACK (completion_changed), entry);
9764 }
9765
9766 /**
9767  * gtk_entry_set_completion:
9768  * @entry: A #GtkEntry
9769  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9770  *
9771  * Sets @completion to be the auxiliary completion object to use with @entry.
9772  * All further configuration of the completion mechanism is done on
9773  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9774  * @completion is set to %NULL.
9775  *
9776  * Since: 2.4
9777  */
9778 void
9779 gtk_entry_set_completion (GtkEntry           *entry,
9780                           GtkEntryCompletion *completion)
9781 {
9782   GtkEntryCompletion *old;
9783
9784   g_return_if_fail (GTK_IS_ENTRY (entry));
9785   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9786
9787   old = gtk_entry_get_completion (entry);
9788
9789   if (old == completion)
9790     return;
9791   
9792   if (old)
9793     {
9794       if (old->priv->completion_timeout)
9795         {
9796           g_source_remove (old->priv->completion_timeout);
9797           old->priv->completion_timeout = 0;
9798         }
9799
9800       if (old->priv->check_completion_idle)
9801         {
9802           g_source_destroy (old->priv->check_completion_idle);
9803           old->priv->check_completion_idle = NULL;
9804         }
9805
9806       if (gtk_widget_get_mapped (old->priv->popup_window))
9807         _gtk_entry_completion_popdown (old);
9808
9809       disconnect_completion_signals (entry, old);
9810       old->priv->entry = NULL;
9811
9812       g_object_unref (old);
9813     }
9814
9815   if (!completion)
9816     {
9817       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9818       return;
9819     }
9820
9821   /* hook into the entry */
9822   g_object_ref (completion);
9823
9824   connect_completion_signals (entry, completion);    
9825   completion->priv->entry = GTK_WIDGET (entry);
9826   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9827
9828   g_object_notify (G_OBJECT (entry), "completion");
9829 }
9830
9831 /**
9832  * gtk_entry_get_completion:
9833  * @entry: A #GtkEntry
9834  *
9835  * Returns the auxiliary completion object currently in use by @entry.
9836  *
9837  * Return value: (transfer none): The auxiliary completion object currently
9838  *     in use by @entry.
9839  *
9840  * Since: 2.4
9841  */
9842 GtkEntryCompletion *
9843 gtk_entry_get_completion (GtkEntry *entry)
9844 {
9845   GtkEntryCompletion *completion;
9846
9847   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9848
9849   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9850                                      GTK_ENTRY_COMPLETION_KEY));
9851
9852   return completion;
9853 }
9854
9855 /**
9856  * gtk_entry_set_cursor_hadjustment:
9857  * @entry: a #GtkEntry
9858  * @adjustment: an adjustment which should be adjusted when the cursor 
9859  *              is moved, or %NULL
9860  *
9861  * Hooks up an adjustment to the cursor position in an entry, so that when 
9862  * the cursor is moved, the adjustment is scrolled to show that position. 
9863  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9864  * the adjustment.
9865  *
9866  * The adjustment has to be in pixel units and in the same coordinate system 
9867  * as the entry. 
9868  * 
9869  * Since: 2.12
9870  */
9871 void
9872 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9873                                   GtkAdjustment *adjustment)
9874 {
9875   g_return_if_fail (GTK_IS_ENTRY (entry));
9876   if (adjustment)
9877     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9878
9879   if (adjustment)
9880     g_object_ref (adjustment);
9881
9882   g_object_set_qdata_full (G_OBJECT (entry), 
9883                            quark_cursor_hadjustment,
9884                            adjustment, 
9885                            g_object_unref);
9886 }
9887
9888 /**
9889  * gtk_entry_get_cursor_hadjustment:
9890  * @entry: a #GtkEntry
9891  *
9892  * Retrieves the horizontal cursor adjustment for the entry. 
9893  * See gtk_entry_set_cursor_hadjustment().
9894  *
9895  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9896  *   if none has been set.
9897  *
9898  * Since: 2.12
9899  */
9900 GtkAdjustment*
9901 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9902 {
9903   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9904
9905   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9906 }
9907
9908 /**
9909  * gtk_entry_set_progress_fraction:
9910  * @entry: a #GtkEntry
9911  * @fraction: fraction of the task that's been completed
9912  *
9913  * Causes the entry's progress indicator to "fill in" the given
9914  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9915  * inclusive.
9916  *
9917  * Since: 2.16
9918  */
9919 void
9920 gtk_entry_set_progress_fraction (GtkEntry *entry,
9921                                  gdouble   fraction)
9922 {
9923   GtkWidget       *widget;
9924   GtkEntryPrivate *private;
9925   gdouble          old_fraction;
9926   gint x, y, width, height;
9927   gint old_x, old_y, old_width, old_height;
9928
9929   g_return_if_fail (GTK_IS_ENTRY (entry));
9930
9931   widget = GTK_WIDGET (entry);
9932   private = entry->priv;
9933
9934   if (private->progress_pulse_mode)
9935     old_fraction = -1;
9936   else
9937     old_fraction = private->progress_fraction;
9938
9939   if (gtk_widget_is_drawable (widget))
9940     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9941
9942   fraction = CLAMP (fraction, 0.0, 1.0);
9943
9944   private->progress_fraction = fraction;
9945   private->progress_pulse_mode = FALSE;
9946   private->progress_pulse_current = 0.0;
9947
9948   if (gtk_widget_is_drawable (widget))
9949     {
9950       get_progress_area (widget, &x, &y, &width, &height);
9951
9952       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9953         gtk_widget_queue_draw (widget);
9954     }
9955
9956   if (fraction != old_fraction)
9957     g_object_notify (G_OBJECT (entry), "progress-fraction");
9958 }
9959
9960 /**
9961  * gtk_entry_get_progress_fraction:
9962  * @entry: a #GtkEntry
9963  *
9964  * Returns the current fraction of the task that's been completed.
9965  * See gtk_entry_set_progress_fraction().
9966  *
9967  * Return value: a fraction from 0.0 to 1.0
9968  *
9969  * Since: 2.16
9970  */
9971 gdouble
9972 gtk_entry_get_progress_fraction (GtkEntry *entry)
9973 {
9974   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9975
9976   return entry->priv->progress_fraction;
9977 }
9978
9979 /**
9980  * gtk_entry_set_progress_pulse_step:
9981  * @entry: a #GtkEntry
9982  * @fraction: fraction between 0.0 and 1.0
9983  *
9984  * Sets the fraction of total entry width to move the progress
9985  * bouncing block for each call to gtk_entry_progress_pulse().
9986  *
9987  * Since: 2.16
9988  */
9989 void
9990 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9991                                    gdouble   fraction)
9992 {
9993   GtkEntryPrivate *private;
9994
9995   g_return_if_fail (GTK_IS_ENTRY (entry));
9996
9997   private = entry->priv;
9998
9999   fraction = CLAMP (fraction, 0.0, 1.0);
10000
10001   if (fraction != private->progress_pulse_fraction)
10002     {
10003       private->progress_pulse_fraction = fraction;
10004
10005       gtk_widget_queue_draw (GTK_WIDGET (entry));
10006
10007       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
10008     }
10009 }
10010
10011 /**
10012  * gtk_entry_get_progress_pulse_step:
10013  * @entry: a #GtkEntry
10014  *
10015  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
10016  *
10017  * Return value: a fraction from 0.0 to 1.0
10018  *
10019  * Since: 2.16
10020  */
10021 gdouble
10022 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
10023 {
10024   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10025
10026   return entry->priv->progress_pulse_fraction;
10027 }
10028
10029 /**
10030  * gtk_entry_progress_pulse:
10031  * @entry: a #GtkEntry
10032  *
10033  * Indicates that some progress is made, but you don't know how much.
10034  * Causes the entry's progress indicator to enter "activity mode,"
10035  * where a block bounces back and forth. Each call to
10036  * gtk_entry_progress_pulse() causes the block to move by a little bit
10037  * (the amount of movement per pulse is determined by
10038  * gtk_entry_set_progress_pulse_step()).
10039  *
10040  * Since: 2.16
10041  */
10042 void
10043 gtk_entry_progress_pulse (GtkEntry *entry)
10044 {
10045   GtkEntryPrivate *private;
10046
10047   g_return_if_fail (GTK_IS_ENTRY (entry));
10048
10049   private = entry->priv;
10050
10051   if (private->progress_pulse_mode)
10052     {
10053       if (private->progress_pulse_way_back)
10054         {
10055           private->progress_pulse_current -= private->progress_pulse_fraction;
10056
10057           if (private->progress_pulse_current < 0.0)
10058             {
10059               private->progress_pulse_current = 0.0;
10060               private->progress_pulse_way_back = FALSE;
10061             }
10062         }
10063       else
10064         {
10065           private->progress_pulse_current += private->progress_pulse_fraction;
10066
10067           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10068             {
10069               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10070               private->progress_pulse_way_back = TRUE;
10071             }
10072         }
10073     }
10074   else
10075     {
10076       private->progress_fraction = 0.0;
10077       private->progress_pulse_mode = TRUE;
10078       private->progress_pulse_way_back = FALSE;
10079       private->progress_pulse_current = 0.0;
10080     }
10081
10082   gtk_widget_queue_draw (GTK_WIDGET (entry));
10083 }
10084
10085 /**
10086  * gtk_entry_set_placeholder_text:
10087  * @entry: a #GtkEntry
10088  * @text: a string to be displayed when @entry is empty an unfocused, or %NULL
10089  *
10090  * Sets text to be displayed in @entry when it is empty and unfocused.
10091  * This can be used to give a visual hint of the expected contents of
10092  * the #GtkEntry.
10093  *
10094  * Note that since the placeholder text gets removed when the entry
10095  * received focus, using this feature is a bit problematic if the entry
10096  * is given the initial focus in a window. Sometimes this can be
10097  * worked around by delaying the initial focus setting until the
10098  * first key event arrives.
10099  *
10100  * Since: 3.2
10101  **/
10102 void
10103 gtk_entry_set_placeholder_text (GtkEntry    *entry,
10104                                 const gchar *text)
10105 {
10106   GtkEntryPrivate *priv;
10107
10108   g_return_if_fail (GTK_IS_ENTRY (entry));
10109
10110   priv = entry->priv;
10111
10112   if (g_strcmp0 (priv->placeholder_text, text) == 0)
10113     return;
10114
10115   g_free (priv->placeholder_text);
10116   priv->placeholder_text = g_strdup (text);
10117
10118   gtk_entry_recompute (entry);
10119
10120   g_object_notify (G_OBJECT (entry), "placeholder-text");
10121 }
10122
10123 /**
10124  * gtk_entry_get_placeholder_text:
10125  * @entry: a #GtkEntry
10126  *
10127  * Retrieves the text that will be displayed when @entry is empty and unfocused
10128  *
10129  * Returns: a pointer to the placeholder text as a string. This string points to internally allocated
10130  * storage in the widget and must not be freed, modified or stored.
10131  *
10132  * Since: 3.2
10133  **/
10134 const gchar *
10135 gtk_entry_get_placeholder_text (GtkEntry *entry)
10136 {
10137   GtkEntryPrivate *priv;
10138
10139   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10140
10141   priv = entry->priv;
10142
10143   return priv->placeholder_text;
10144 }
10145
10146 /* Caps Lock warning for password entries */
10147
10148 static void
10149 show_capslock_feedback (GtkEntry    *entry,
10150                         const gchar *text)
10151 {
10152   GtkEntryPrivate *priv = entry->priv;
10153
10154   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10155     {
10156       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10157       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10158       priv->caps_lock_warning_shown = TRUE;
10159     }
10160
10161   if (priv->caps_lock_warning_shown)
10162     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10163   else
10164     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10165 }
10166
10167 static void
10168 remove_capslock_feedback (GtkEntry *entry)
10169 {
10170   GtkEntryPrivate *priv = entry->priv;
10171
10172   if (priv->caps_lock_warning_shown)
10173     {
10174       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10175       priv->caps_lock_warning_shown = FALSE;
10176     } 
10177 }
10178
10179 static void
10180 keymap_state_changed (GdkKeymap *keymap, 
10181                       GtkEntry  *entry)
10182 {
10183   GtkEntryPrivate *priv = entry->priv;
10184   char *text = NULL;
10185
10186   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10187     { 
10188       if (gdk_keymap_get_num_lock_state (keymap)
10189           && gdk_keymap_get_caps_lock_state (keymap))
10190         text = _("Caps Lock and Num Lock are on");
10191       else if (gdk_keymap_get_num_lock_state (keymap))
10192         text = _("Num Lock is on");
10193       else if (gdk_keymap_get_caps_lock_state (keymap))
10194         text = _("Caps Lock is on");
10195     }
10196
10197   if (text)
10198     show_capslock_feedback (entry, text);
10199   else
10200     remove_capslock_feedback (entry);
10201 }
10202
10203 /*
10204  * _gtk_entry_set_is_cell_renderer:
10205  * @entry: a #GtkEntry
10206  * @is_cell_renderer: new value
10207  *
10208  * This is a helper function for GtkComboBox. A GtkEntry in a GtkComboBox
10209  * is supposed to behave like a GtkCellEditable when placed in a combo box.
10210  *
10211  * I.e take up its allocation and get GtkEntry->is_cell_renderer = TRUE.
10212  *
10213  */
10214 void
10215 _gtk_entry_set_is_cell_renderer (GtkEntry *entry,
10216                                  gboolean  is_cell_renderer)
10217 {
10218   entry->priv->is_cell_renderer = is_cell_renderer;
10219 }