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