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