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