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