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