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