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