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