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