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