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