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