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