]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
Merge branch 'master' into treeview-refactor
[~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_get_state (widget);
3423
3424   gtk_paint_flat_box (style, cr,
3425                       state, GTK_SHADOW_NONE,
3426                       widget, "entry_bg",
3427                       x, y, width, height);
3428
3429   if (priv->has_frame)
3430     gtk_paint_shadow (style, cr,
3431                       state, priv->shadow_type,
3432                       widget, "entry", x, y, width, height);
3433
3434   gtk_entry_draw_progress (widget, cr);
3435
3436   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3437     {
3438       x -= priv->focus_width;
3439       y -= priv->focus_width;
3440       width += 2 * priv->focus_width;
3441       height += 2 * priv->focus_width;
3442
3443       gtk_paint_focus (style, cr,
3444                        gtk_widget_get_state (widget),
3445                        widget, "entry",
3446                        0, 0, width, height);
3447     }
3448
3449   cairo_restore (cr);
3450 }
3451
3452 static void
3453 get_progress_area (GtkWidget *widget,
3454                    gint       *x,
3455                    gint       *y,
3456                    gint       *width,
3457                    gint       *height)
3458 {
3459   GtkEntry *entry = GTK_ENTRY (widget);
3460   GtkEntryPrivate *private = entry->priv;
3461   GtkBorder *progress_border;
3462
3463   get_text_area_size (entry, x, y, width, height);
3464
3465   if (!private->interior_focus)
3466     {
3467       *x -= private->focus_width;
3468       *y -= private->focus_width;
3469       *width += 2 * private->focus_width;
3470       *height += 2 * private->focus_width;
3471     }
3472
3473   gtk_widget_style_get (widget, "progress-border", &progress_border, NULL);
3474
3475   if (progress_border)
3476     {
3477       *x += progress_border->left;
3478       *y += progress_border->top;
3479       *width -= progress_border->left + progress_border->right;
3480       *height -= progress_border->top + progress_border->bottom;
3481
3482       gtk_border_free (progress_border);
3483     }
3484
3485   if (private->progress_pulse_mode)
3486     {
3487       gdouble value = private->progress_pulse_current;
3488
3489       *x += (gint) floor(value * (*width));
3490       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3491     }
3492   else if (private->progress_fraction > 0)
3493     {
3494       gdouble value = private->progress_fraction;
3495
3496       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3497         {
3498           gint bar_width;
3499
3500           bar_width = floor(value * (*width) + 0.5);
3501           *x += *width - bar_width;
3502           *width = bar_width;
3503         }
3504       else
3505         {
3506           *width = (gint) floor(value * (*width) + 0.5);
3507         }
3508     }
3509   else
3510     {
3511       *width = 0;
3512       *height = 0;
3513     }
3514 }
3515
3516 static void
3517 gtk_entry_draw_progress (GtkWidget      *widget,
3518                          cairo_t        *cr)
3519 {
3520   gint x, y, width, height;
3521   GtkStateType state;
3522
3523   get_progress_area (widget, &x, &y, &width, &height);
3524
3525   if ((width <= 0) || (height <= 0))
3526     return;
3527
3528   state = GTK_STATE_SELECTED;
3529   if (!gtk_widget_get_sensitive (widget))
3530     state = GTK_STATE_INSENSITIVE;
3531
3532   gtk_paint_box (gtk_widget_get_style (widget), cr,
3533                  state, GTK_SHADOW_OUT,
3534                  widget, "entry-progress",
3535                  x, y,
3536                  width, height);
3537 }
3538
3539 static gint
3540 gtk_entry_draw (GtkWidget *widget,
3541                 cairo_t   *cr)
3542 {
3543   GtkEntry *entry = GTK_ENTRY (widget);
3544   GtkStyle *style;
3545   GtkStateType state;
3546   GtkEntryPrivate *priv = entry->priv;
3547   int i;
3548
3549   style = gtk_widget_get_style (widget);
3550
3551   state = gtk_widget_has_focus (widget) ?
3552     GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
3553
3554   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3555     {
3556       /* Draw entry_bg, shadow, progress and focus */
3557       gtk_entry_draw_frame (widget, cr);
3558
3559       /* Draw text and cursor */
3560       cairo_save (cr);
3561
3562       gtk_cairo_transform_to_window (cr, widget, priv->text_area);
3563
3564       if (priv->dnd_position != -1)
3565         gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3566       
3567       gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3568
3569       /* When no text is being displayed at all, don't show the cursor */
3570       if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3571           gtk_widget_has_focus (widget) &&
3572           priv->selection_bound == priv->current_pos && priv->cursor_visible)
3573         gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3574
3575       cairo_restore (cr);
3576
3577       /* Draw icons */
3578       for (i = 0; i < MAX_ICONS; i++)
3579         {
3580           EntryIconInfo *icon_info = priv->icons[i];
3581
3582           if (icon_info != NULL)
3583             {
3584               cairo_save (cr);
3585
3586               gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3587
3588               draw_icon (widget, cr, i);
3589
3590               cairo_restore (cr);
3591             }
3592         }
3593     }
3594
3595   return FALSE;
3596 }
3597
3598 static gint
3599 gtk_entry_enter_notify (GtkWidget *widget,
3600                         GdkEventCrossing *event)
3601 {
3602   GtkEntry *entry = GTK_ENTRY (widget);
3603   GtkEntryPrivate *priv = entry->priv;
3604   gint i;
3605
3606   for (i = 0; i < MAX_ICONS; i++)
3607     {
3608       EntryIconInfo *icon_info = priv->icons[i];
3609
3610       if (icon_info != NULL && event->window == icon_info->window)
3611         {
3612           if (should_prelight (entry, i))
3613             {
3614               icon_info->prelight = TRUE;
3615               gtk_widget_queue_draw (widget);
3616             }
3617
3618           break;
3619         }
3620     }
3621
3622     return FALSE;
3623 }
3624
3625 static gint
3626 gtk_entry_leave_notify (GtkWidget        *widget,
3627                         GdkEventCrossing *event)
3628 {
3629   GtkEntry *entry = GTK_ENTRY (widget);
3630   GtkEntryPrivate *priv = entry->priv;
3631   gint i;
3632
3633   for (i = 0; i < MAX_ICONS; i++)
3634     {
3635       EntryIconInfo *icon_info = priv->icons[i];
3636
3637       if (icon_info != NULL && event->window == icon_info->window)
3638         {
3639           /* a grab means that we may never see the button release */
3640           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3641             icon_info->pressed = FALSE;
3642
3643           if (should_prelight (entry, i))
3644             {
3645               icon_info->prelight = FALSE;
3646               gtk_widget_queue_draw (widget);
3647             }
3648
3649           break;
3650         }
3651     }
3652
3653   return FALSE;
3654 }
3655
3656 static void
3657 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3658                             gint     **ranges,
3659                             gint      *n_ranges)
3660 {
3661   gint start_char, end_char;
3662
3663   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3664     {
3665       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3666       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3667       const char *text = pango_layout_get_text (layout);
3668       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3669       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3670       gint real_n_ranges, i;
3671
3672       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3673
3674       if (ranges)
3675         {
3676           gint *r = *ranges;
3677           
3678           for (i = 0; i < real_n_ranges; ++i)
3679             {
3680               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3681               r[2 * i] = r[2 * i] / PANGO_SCALE;
3682             }
3683         }
3684       
3685       if (n_ranges)
3686         *n_ranges = real_n_ranges;
3687     }
3688   else
3689     {
3690       if (n_ranges)
3691         *n_ranges = 0;
3692       if (ranges)
3693         *ranges = NULL;
3694     }
3695 }
3696
3697 static gboolean
3698 in_selection (GtkEntry *entry,
3699               gint      x)
3700 {
3701   gint *ranges;
3702   gint n_ranges, i;
3703   gint retval = FALSE;
3704
3705   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3706
3707   for (i = 0; i < n_ranges; ++i)
3708     {
3709       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3710         {
3711           retval = TRUE;
3712           break;
3713         }
3714     }
3715
3716   g_free (ranges);
3717   return retval;
3718 }
3719               
3720 static gint
3721 gtk_entry_button_press (GtkWidget      *widget,
3722                         GdkEventButton *event)
3723 {
3724   GtkEntry *entry = GTK_ENTRY (widget);
3725   GtkEditable *editable = GTK_EDITABLE (widget);
3726   GtkEntryPrivate *priv = entry->priv;
3727   EntryIconInfo *icon_info = NULL;
3728   gint tmp_pos;
3729   gint sel_start, sel_end;
3730   gint i;
3731
3732   for (i = 0; i < MAX_ICONS; i++)
3733     {
3734       icon_info = priv->icons[i];
3735
3736       if (!icon_info || icon_info->insensitive)
3737         continue;
3738
3739       if (event->window == icon_info->window)
3740         {
3741           if (should_prelight (entry, i))
3742             {
3743               icon_info->prelight = FALSE;
3744               gtk_widget_queue_draw (widget);
3745             }
3746
3747           priv->start_x = event->x;
3748           priv->start_y = event->y;
3749           icon_info->pressed = TRUE;
3750
3751           if (!icon_info->nonactivatable)
3752             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3753
3754           return TRUE;
3755         }
3756     }
3757
3758   if (event->window != priv->text_area ||
3759       (priv->button && event->button != priv->button))
3760     return FALSE;
3761
3762   gtk_entry_reset_blink_time (entry);
3763
3764   priv->button = event->button;
3765   
3766   if (!gtk_widget_has_focus (widget))
3767     {
3768       priv->in_click = TRUE;
3769       gtk_widget_grab_focus (widget);
3770       priv->in_click = FALSE;
3771     }
3772   
3773   tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
3774     
3775   if (event->button == 1)
3776     {
3777       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3778       
3779       priv->select_words = FALSE;
3780       priv->select_lines = FALSE;
3781
3782       if (event->state & GDK_SHIFT_MASK)
3783         {
3784           _gtk_entry_reset_im_context (entry);
3785           
3786           if (!have_selection) /* select from the current position to the clicked position */
3787             sel_start = sel_end = priv->current_pos;
3788           
3789           if (tmp_pos > sel_start && tmp_pos < sel_end)
3790             {
3791               /* Truncate current selection, but keep it as big as possible */
3792               if (tmp_pos - sel_start > sel_end - tmp_pos)
3793                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3794               else
3795                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3796             }
3797           else
3798             {
3799               gboolean extend_to_left;
3800               gint start, end;
3801
3802               /* Figure out what click selects and extend current selection */
3803               switch (event->type)
3804                 {
3805                 case GDK_BUTTON_PRESS:
3806                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3807                   break;
3808                   
3809                 case GDK_2BUTTON_PRESS:
3810                   priv->select_words = TRUE;
3811                   gtk_entry_select_word (entry);
3812                   break;
3813                   
3814                 case GDK_3BUTTON_PRESS:
3815                   priv->select_lines = TRUE;
3816                   gtk_entry_select_line (entry);
3817                   break;
3818
3819                 default:
3820                   break;
3821                 }
3822
3823               start = MIN (priv->current_pos, priv->selection_bound);
3824               start = MIN (sel_start, start);
3825               
3826               end = MAX (priv->current_pos, priv->selection_bound);
3827               end = MAX (sel_end, end);
3828
3829               if (tmp_pos == sel_start || tmp_pos == sel_end)
3830                 extend_to_left = (tmp_pos == start);
3831               else
3832                 extend_to_left = (end == sel_end);
3833               
3834               if (extend_to_left)
3835                 gtk_entry_set_positions (entry, start, end);
3836               else
3837                 gtk_entry_set_positions (entry, end, start);
3838             }
3839         }
3840       else /* no shift key */
3841         switch (event->type)
3842         {
3843         case GDK_BUTTON_PRESS:
3844           if (in_selection (entry, event->x + priv->scroll_offset))
3845             {
3846               /* Click inside the selection - we'll either start a drag, or
3847                * clear the selection
3848                */
3849               priv->in_drag = TRUE;
3850               priv->drag_start_x = event->x + priv->scroll_offset;
3851               priv->drag_start_y = event->y;
3852             }
3853           else
3854             gtk_editable_set_position (editable, tmp_pos);
3855           break;
3856  
3857         case GDK_2BUTTON_PRESS:
3858           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3859            * receiving a GDK_2BUTTON_PRESS so we need to reset
3860            * priv->in_drag which may have been set above
3861            */
3862           priv->in_drag = FALSE;
3863           priv->select_words = TRUE;
3864           gtk_entry_select_word (entry);
3865           break;
3866         
3867         case GDK_3BUTTON_PRESS:
3868           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3869            * receiving a GDK_3BUTTON_PRESS so we need to reset
3870            * priv->in_drag which may have been set above
3871            */
3872           priv->in_drag = FALSE;
3873           priv->select_lines = TRUE;
3874           gtk_entry_select_line (entry);
3875           break;
3876
3877         default:
3878           break;
3879         }
3880
3881       return TRUE;
3882     }
3883   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3884     {
3885       if (priv->editable)
3886         {
3887           priv->insert_pos = tmp_pos;
3888           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3889           return TRUE;
3890         }
3891       else
3892         {
3893           gtk_widget_error_bell (widget);
3894         }
3895     }
3896   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3897     {
3898       gtk_entry_do_popup (entry, event);
3899       priv->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3900
3901       return TRUE;
3902     }
3903
3904   return FALSE;
3905 }
3906
3907 static gint
3908 gtk_entry_button_release (GtkWidget      *widget,
3909                           GdkEventButton *event)
3910 {
3911   GtkEntry *entry = GTK_ENTRY (widget);
3912   GtkEntryPrivate *priv = entry->priv;
3913   EntryIconInfo *icon_info = NULL;
3914   gint i;
3915
3916   for (i = 0; i < MAX_ICONS; i++)
3917     {
3918       icon_info = priv->icons[i];
3919
3920       if (!icon_info || icon_info->insensitive)
3921         continue;
3922
3923       if (event->window == icon_info->window)
3924         {
3925           icon_info->pressed = FALSE;
3926
3927           if (should_prelight (entry, i) &&
3928               event->x >= 0 && event->y >= 0 &&
3929               event->x < gdk_window_get_width (icon_info->window) &&
3930               event->y < gdk_window_get_height (icon_info->window))
3931             {
3932               icon_info->prelight = TRUE;
3933               gtk_widget_queue_draw (widget);
3934             }
3935
3936           if (!icon_info->nonactivatable)
3937             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3938
3939           return TRUE;
3940         }
3941     }
3942
3943   if (event->window != priv->text_area || priv->button != event->button)
3944     return FALSE;
3945
3946   if (priv->in_drag)
3947     {
3948       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
3949
3950       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3951
3952       priv->in_drag = 0;
3953     }
3954   
3955   priv->button = 0;
3956   
3957   gtk_entry_update_primary_selection (entry);
3958               
3959   return TRUE;
3960 }
3961
3962 static gchar *
3963 _gtk_entry_get_selected_text (GtkEntry *entry)
3964 {
3965   GtkEditable *editable = GTK_EDITABLE (entry);
3966   gint         start_text, end_text;
3967   gchar       *text = NULL;
3968
3969   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3970     text = gtk_editable_get_chars (editable, start_text, end_text);
3971
3972   return text;
3973 }
3974
3975 static gint
3976 gtk_entry_motion_notify (GtkWidget      *widget,
3977                          GdkEventMotion *event)
3978 {
3979   GtkEntry *entry = GTK_ENTRY (widget);
3980   GtkEntryPrivate *priv = entry->priv;
3981   EntryIconInfo *icon_info = NULL;
3982   GdkDragContext *context;
3983   gint tmp_pos;
3984   gint i;
3985
3986   for (i = 0; i < MAX_ICONS; i++)
3987     {
3988       icon_info = priv->icons[i];
3989
3990       if (!icon_info || icon_info->insensitive)
3991         continue;
3992
3993       if (event->window == icon_info->window)
3994         {
3995           if (icon_info->pressed &&
3996               icon_info->target_list != NULL &&
3997               gtk_drag_check_threshold (widget,
3998                                         priv->start_x,
3999                                         priv->start_y,
4000                                         event->x,
4001                                         event->y))
4002             {
4003               icon_info->in_drag = TRUE;
4004               icon_info->pressed = FALSE;
4005               context = gtk_drag_begin (widget,
4006                                         icon_info->target_list,
4007                                         icon_info->actions,
4008                                         1,
4009                                         (GdkEvent*)event);
4010             }
4011
4012           return TRUE;
4013         }
4014     }
4015
4016   if (priv->mouse_cursor_obscured)
4017     {
4018       GdkCursor *cursor;
4019       
4020       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4021       gdk_window_set_cursor (priv->text_area, cursor);
4022       gdk_cursor_unref (cursor);
4023       priv->mouse_cursor_obscured = FALSE;
4024     }
4025
4026   if (event->window != priv->text_area || priv->button != 1)
4027     return FALSE;
4028
4029   if (priv->select_lines)
4030     return TRUE;
4031
4032   gdk_event_request_motions (event);
4033
4034   if (priv->in_drag)
4035     {
4036       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
4037           gtk_drag_check_threshold (widget,
4038                                     priv->drag_start_x, priv->drag_start_y,
4039                                     event->x + priv->scroll_offset, event->y))
4040         {
4041           GdkDragContext *context;
4042           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
4043           guint actions = priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
4044           gchar *text = NULL;
4045           cairo_surface_t *surface;
4046
4047           gtk_target_list_add_text_targets (target_list, 0);
4048
4049           text = _gtk_entry_get_selected_text (entry);
4050           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
4051
4052           context = gtk_drag_begin (widget, target_list, actions,
4053                                     priv->button, (GdkEvent *)event);
4054           
4055           if (surface)
4056             gtk_drag_set_icon_surface (context, surface);
4057           else
4058             gtk_drag_set_icon_default (context);
4059           
4060           if (surface)
4061             cairo_surface_destroy (surface);
4062           g_free (text);
4063
4064           priv->in_drag = FALSE;
4065           priv->button = 0;
4066           
4067           gtk_target_list_unref (target_list);
4068         }
4069     }
4070   else
4071     {
4072       if (event->y < 0)
4073         tmp_pos = 0;
4074       else if (event->y >= gdk_window_get_height (priv->text_area))
4075         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4076       else
4077         tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
4078
4079       if (priv->select_words)
4080         {
4081           gint min, max;
4082           gint old_min, old_max;
4083           gint pos, bound;
4084           
4085           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4086           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4087
4088           pos = priv->current_pos;
4089           bound = priv->selection_bound;
4090
4091           old_min = MIN(priv->current_pos, priv->selection_bound);
4092           old_max = MAX(priv->current_pos, priv->selection_bound);
4093           
4094           if (min < old_min)
4095             {
4096               pos = min;
4097               bound = old_max;
4098             }
4099           else if (old_max < max) 
4100             {
4101               pos = max;
4102               bound = old_min;
4103             }
4104           else if (pos == old_min) 
4105             {
4106               if (priv->current_pos != min)
4107                 pos = max;
4108             }
4109           else 
4110             {
4111               if (priv->current_pos != max)
4112                 pos = min;
4113             }
4114         
4115           gtk_entry_set_positions (entry, pos, bound);
4116         }
4117       else
4118         gtk_entry_set_positions (entry, tmp_pos, -1);
4119     }
4120       
4121   return TRUE;
4122 }
4123
4124 static void
4125 set_invisible_cursor (GdkWindow *window)
4126 {
4127   GdkDisplay *display;
4128   GdkCursor *cursor;
4129
4130   display = gdk_window_get_display (window);
4131   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4132
4133   gdk_window_set_cursor (window, cursor);
4134
4135   gdk_cursor_unref (cursor);
4136 }
4137
4138 static void
4139 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4140 {
4141   GtkEntryPrivate *priv = entry->priv;
4142
4143   if (priv->mouse_cursor_obscured)
4144     return;
4145
4146   set_invisible_cursor (priv->text_area);
4147
4148   priv->mouse_cursor_obscured = TRUE;
4149 }
4150
4151 static gint
4152 gtk_entry_key_press (GtkWidget   *widget,
4153                      GdkEventKey *event)
4154 {
4155   GtkEntry *entry = GTK_ENTRY (widget);
4156   GtkEntryPrivate *priv = entry->priv;
4157
4158   gtk_entry_reset_blink_time (entry);
4159   gtk_entry_pend_cursor_blink (entry);
4160
4161   if (priv->editable)
4162     {
4163       if (gtk_im_context_filter_keypress (priv->im_context, event))
4164         {
4165           gtk_entry_obscure_mouse_cursor (entry);
4166           priv->need_im_reset = TRUE;
4167           return TRUE;
4168         }
4169     }
4170
4171   if (event->keyval == GDK_KEY_Return || 
4172       event->keyval == GDK_KEY_KP_Enter || 
4173       event->keyval == GDK_KEY_ISO_Enter || 
4174       event->keyval == GDK_KEY_Escape)
4175     {
4176       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4177
4178       if (completion && completion->priv->completion_timeout)
4179         {
4180           g_source_remove (completion->priv->completion_timeout);
4181           completion->priv->completion_timeout = 0;
4182         }
4183
4184       _gtk_entry_reset_im_context (entry);
4185     }
4186
4187   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4188     /* Activate key bindings
4189      */
4190     return TRUE;
4191
4192   if (!priv->editable && event->length)
4193     gtk_widget_error_bell (widget);
4194
4195   return FALSE;
4196 }
4197
4198 static gint
4199 gtk_entry_key_release (GtkWidget   *widget,
4200                        GdkEventKey *event)
4201 {
4202   GtkEntry *entry = GTK_ENTRY (widget);
4203   GtkEntryPrivate *priv = entry->priv;
4204
4205   if (priv->editable)
4206     {
4207       if (gtk_im_context_filter_keypress (priv->im_context, event))
4208         {
4209           priv->need_im_reset = TRUE;
4210           return TRUE;
4211         }
4212     }
4213
4214   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4215 }
4216
4217 static gint
4218 gtk_entry_focus_in (GtkWidget     *widget,
4219                     GdkEventFocus *event)
4220 {
4221   GtkEntry *entry = GTK_ENTRY (widget);
4222   GtkEntryPrivate *priv = entry->priv;
4223   GdkKeymap *keymap;
4224
4225   gtk_widget_queue_draw (widget);
4226
4227   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4228
4229   if (priv->editable)
4230     {
4231       priv->need_im_reset = TRUE;
4232       gtk_im_context_focus_in (priv->im_context);
4233       keymap_state_changed (keymap, entry);
4234       g_signal_connect (keymap, "state-changed", 
4235                         G_CALLBACK (keymap_state_changed), entry);
4236     }
4237
4238   g_signal_connect (keymap, "direction-changed",
4239                     G_CALLBACK (keymap_direction_changed), entry);
4240
4241   gtk_entry_reset_blink_time (entry);
4242   gtk_entry_check_cursor_blink (entry);
4243
4244   return FALSE;
4245 }
4246
4247 static gint
4248 gtk_entry_focus_out (GtkWidget     *widget,
4249                      GdkEventFocus *event)
4250 {
4251   GtkEntry *entry = GTK_ENTRY (widget);
4252   GtkEntryPrivate *priv = entry->priv;
4253   GtkEntryCompletion *completion;
4254   GdkKeymap *keymap;
4255
4256   gtk_widget_queue_draw (widget);
4257
4258   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4259
4260   if (priv->editable)
4261     {
4262       priv->need_im_reset = TRUE;
4263       gtk_im_context_focus_out (priv->im_context);
4264       remove_capslock_feedback (entry);
4265     }
4266
4267   gtk_entry_check_cursor_blink (entry);
4268   
4269   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4270   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4271
4272   completion = gtk_entry_get_completion (entry);
4273   if (completion)
4274     _gtk_entry_completion_popdown (completion);
4275   
4276   return FALSE;
4277 }
4278
4279 static void
4280 gtk_entry_grab_focus (GtkWidget        *widget)
4281 {
4282   GtkEntry *entry = GTK_ENTRY (widget);
4283   GtkEntryPrivate *priv = entry->priv;
4284   gboolean select_on_focus;
4285   
4286   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4287
4288   if (priv->editable && !priv->in_click)
4289     {
4290       g_object_get (gtk_widget_get_settings (widget),
4291                     "gtk-entry-select-on-focus",
4292                     &select_on_focus,
4293                     NULL);
4294   
4295       if (select_on_focus)
4296         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4297     }
4298 }
4299
4300 static void 
4301 gtk_entry_direction_changed (GtkWidget        *widget,
4302                              GtkTextDirection  previous_dir)
4303 {
4304   GtkEntry *entry = GTK_ENTRY (widget);
4305
4306   gtk_entry_recompute (entry);
4307       
4308   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4309 }
4310
4311 static void
4312 gtk_entry_state_changed (GtkWidget      *widget,
4313                          GtkStateType    previous_state)
4314 {
4315   GtkEntry *entry = GTK_ENTRY (widget);
4316   GtkEntryPrivate *priv = entry->priv;
4317   GdkCursor *cursor;
4318   
4319   if (gtk_widget_get_realized (widget))
4320     {
4321       if (gtk_widget_is_sensitive (widget))
4322         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4323       else 
4324         cursor = NULL;
4325
4326       gdk_window_set_cursor (priv->text_area, cursor);
4327
4328       if (cursor)
4329         gdk_cursor_unref (cursor);
4330
4331       priv->mouse_cursor_obscured = FALSE;
4332
4333       update_cursors (widget);
4334     }
4335
4336   if (!gtk_widget_is_sensitive (widget))
4337     {
4338       /* Clear any selection */
4339       gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
4340     }
4341   
4342   gtk_widget_queue_draw (widget);
4343 }
4344
4345 static void
4346 gtk_entry_screen_changed (GtkWidget *widget,
4347                           GdkScreen *old_screen)
4348 {
4349   gtk_entry_recompute (GTK_ENTRY (widget));
4350 }
4351
4352 /* GtkEditable method implementations
4353  */
4354 static void
4355 gtk_entry_insert_text (GtkEditable *editable,
4356                        const gchar *new_text,
4357                        gint         new_text_length,
4358                        gint        *position)
4359 {
4360   g_object_ref (editable);
4361
4362   /*
4363    * The incoming text may a password or other secret. We make sure
4364    * not to copy it into temporary buffers.
4365    */
4366
4367   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4368
4369   g_object_unref (editable);
4370 }
4371
4372 static void
4373 gtk_entry_delete_text (GtkEditable *editable,
4374                        gint         start_pos,
4375                        gint         end_pos)
4376 {
4377   g_object_ref (editable);
4378
4379   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4380
4381   g_object_unref (editable);
4382 }
4383
4384 static gchar *    
4385 gtk_entry_get_chars      (GtkEditable   *editable,
4386                           gint           start_pos,
4387                           gint           end_pos)
4388 {
4389   GtkEntry *entry = GTK_ENTRY (editable);
4390   const gchar *text;
4391   gint text_length;
4392   gint start_index, end_index;
4393
4394   text = gtk_entry_buffer_get_text (get_buffer (entry));
4395   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4396
4397   if (end_pos < 0)
4398     end_pos = text_length;
4399
4400   start_pos = MIN (text_length, start_pos);
4401   end_pos = MIN (text_length, end_pos);
4402
4403   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4404   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4405
4406   return g_strndup (text + start_index, end_index - start_index);
4407 }
4408
4409 static void
4410 gtk_entry_real_set_position (GtkEditable *editable,
4411                              gint         position)
4412 {
4413   GtkEntry *entry = GTK_ENTRY (editable);
4414   GtkEntryPrivate *priv = entry->priv;
4415
4416   guint length;
4417
4418   length = gtk_entry_buffer_get_length (get_buffer (entry));
4419   if (position < 0 || position > length)
4420     position = length;
4421
4422   if (position != priv->current_pos ||
4423       position != priv->selection_bound)
4424     {
4425       _gtk_entry_reset_im_context (entry);
4426       gtk_entry_set_positions (entry, position, position);
4427     }
4428 }
4429
4430 static gint
4431 gtk_entry_get_position (GtkEditable *editable)
4432 {
4433   GtkEntry *entry = GTK_ENTRY (editable);
4434   GtkEntryPrivate *priv = entry->priv;
4435
4436   return priv->current_pos;
4437 }
4438
4439 static void
4440 gtk_entry_set_selection_bounds (GtkEditable *editable,
4441                                 gint         start,
4442                                 gint         end)
4443 {
4444   GtkEntry *entry = GTK_ENTRY (editable);
4445   guint length;
4446
4447   length = gtk_entry_buffer_get_length (get_buffer (entry));
4448   if (start < 0)
4449     start = length;
4450   if (end < 0)
4451     end = length;
4452   
4453   _gtk_entry_reset_im_context (entry);
4454
4455   gtk_entry_set_positions (entry,
4456                            MIN (end, length),
4457                            MIN (start, length));
4458
4459   gtk_entry_update_primary_selection (entry);
4460 }
4461
4462 static gboolean
4463 gtk_entry_get_selection_bounds (GtkEditable *editable,
4464                                 gint        *start,
4465                                 gint        *end)
4466 {
4467   GtkEntry *entry = GTK_ENTRY (editable);
4468   GtkEntryPrivate *priv = entry->priv;
4469
4470   *start = priv->selection_bound;
4471   *end = priv->current_pos;
4472
4473   return (priv->selection_bound != priv->current_pos);
4474 }
4475
4476 static void
4477 icon_theme_changed (GtkEntry *entry)
4478 {
4479   GtkEntryPrivate *priv = entry->priv;
4480   gint i;
4481
4482   for (i = 0; i < MAX_ICONS; i++)
4483     {
4484       EntryIconInfo *icon_info = priv->icons[i];
4485       if (icon_info != NULL) 
4486         {
4487           if (icon_info->storage_type == GTK_IMAGE_ICON_NAME)
4488             gtk_entry_set_icon_from_icon_name (entry, i, icon_info->icon_name);
4489           else if (icon_info->storage_type == GTK_IMAGE_STOCK)
4490             gtk_entry_set_icon_from_stock (entry, i, icon_info->stock_id);
4491           else if (icon_info->storage_type == GTK_IMAGE_GICON)
4492             gtk_entry_set_icon_from_gicon (entry, i, icon_info->gicon);
4493         }
4494     }
4495
4496   gtk_widget_queue_draw (GTK_WIDGET (entry));
4497 }
4498
4499 static void
4500 icon_margin_changed (GtkEntry *entry)
4501 {
4502   GtkEntryPrivate *priv = entry->priv;
4503   GtkBorder border;
4504
4505   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4506
4507   priv->icon_margin = border.left;
4508 }
4509
4510 static void 
4511 gtk_entry_style_set (GtkWidget *widget,
4512                      GtkStyle  *previous_style)
4513 {
4514   GtkEntry *entry = GTK_ENTRY (widget);
4515   GtkEntryPrivate *priv = entry->priv;
4516   gint focus_width;
4517   gboolean interior_focus;
4518
4519   gtk_widget_style_get (widget,
4520                         "focus-line-width", &focus_width,
4521                         "interior-focus", &interior_focus,
4522                         NULL);
4523
4524   priv->focus_width = focus_width;
4525   priv->interior_focus = interior_focus;
4526
4527   if (!priv->invisible_char_set)
4528     priv->invisible_char = find_invisible_char (GTK_WIDGET (entry));
4529
4530   gtk_entry_recompute (entry);
4531
4532   icon_theme_changed (entry);
4533   icon_margin_changed (entry);
4534 }
4535
4536 /* GtkCellEditable method implementations
4537  */
4538 static void
4539 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4540 {
4541   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4542   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4543 }
4544
4545 static gboolean
4546 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4547                                    GdkEventKey *key_event,
4548                                    gpointer     data)
4549 {
4550   GtkEntryPrivate *priv = entry->priv;
4551
4552   if (key_event->keyval == GDK_KEY_Escape)
4553     {
4554       priv->editing_canceled = TRUE;
4555       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4556       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4557
4558       return TRUE;
4559     }
4560
4561   /* override focus */
4562   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4563     {
4564       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4565       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4566
4567       return TRUE;
4568     }
4569
4570   return FALSE;
4571 }
4572
4573 static void
4574 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4575                          GdkEvent        *event)
4576 {
4577   GtkEntry *entry = GTK_ENTRY (cell_editable);
4578   GtkEntryPrivate *priv = entry->priv;
4579
4580   priv->is_cell_renderer = TRUE;
4581
4582   g_signal_connect (cell_editable, "activate",
4583                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4584   g_signal_connect (cell_editable, "key-press-event",
4585                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4586 }
4587
4588 static void
4589 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4590 {
4591   if (password_hint->source_id)
4592     g_source_remove (password_hint->source_id);
4593
4594   g_slice_free (GtkEntryPasswordHint, password_hint);
4595 }
4596
4597
4598 static gboolean
4599 gtk_entry_remove_password_hint (gpointer data)
4600 {
4601   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4602   password_hint->position = -1;
4603
4604   /* Force the string to be redrawn, but now without a visible character */
4605   gtk_entry_recompute (GTK_ENTRY (data));
4606   return FALSE;
4607 }
4608
4609 /* Default signal handlers
4610  */
4611 static void
4612 gtk_entry_real_insert_text (GtkEditable *editable,
4613                             const gchar *new_text,
4614                             gint         new_text_length,
4615                             gint        *position)
4616 {
4617   guint n_inserted;
4618   gint n_chars;
4619
4620   n_chars = g_utf8_strlen (new_text, new_text_length);
4621
4622   /*
4623    * The actual insertion into the buffer. This will end up firing the
4624    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4625    * buffer_notify_text(), buffer_notify_length()
4626    */
4627   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4628
4629   if (n_inserted != n_chars)
4630       gtk_widget_error_bell (GTK_WIDGET (editable));
4631
4632   *position += n_inserted;
4633 }
4634
4635 static void
4636 gtk_entry_real_delete_text (GtkEditable *editable,
4637                             gint         start_pos,
4638                             gint         end_pos)
4639 {
4640   /*
4641    * The actual deletion from the buffer. This will end up firing the
4642    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4643    * buffer_notify_text(), buffer_notify_length()
4644    */
4645
4646   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4647 }
4648
4649 /* GtkEntryBuffer signal handlers
4650  */
4651 static void
4652 buffer_inserted_text (GtkEntryBuffer *buffer,
4653                       guint           position,
4654                       const gchar    *chars,
4655                       guint           n_chars,
4656                       GtkEntry       *entry)
4657 {
4658   GtkEntryPrivate *priv = entry->priv;
4659   guint password_hint_timeout;
4660
4661   if (priv->current_pos > position)
4662     priv->current_pos += n_chars;
4663
4664   if (priv->selection_bound > position)
4665     priv->selection_bound += n_chars;
4666
4667   /* Calculate the password hint if it needs to be displayed. */
4668   if (n_chars == 1 && !priv->visible)
4669     {
4670       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4671                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4672                     NULL);
4673
4674       if (password_hint_timeout > 0)
4675         {
4676           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4677                                                                     quark_password_hint);
4678           if (!password_hint)
4679             {
4680               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4681               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4682                                        (GDestroyNotify)gtk_entry_password_hint_free);
4683             }
4684
4685           password_hint->position = position;
4686           if (password_hint->source_id)
4687             g_source_remove (password_hint->source_id);
4688           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4689                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4690         }
4691     }
4692 }
4693
4694 static void
4695 buffer_deleted_text (GtkEntryBuffer *buffer,
4696                      guint           position,
4697                      guint           n_chars,
4698                      GtkEntry       *entry)
4699 {
4700   GtkEntryPrivate *priv = entry->priv;
4701   guint end_pos = position + n_chars;
4702   gint selection_bound;
4703   guint current_pos;
4704
4705   current_pos = priv->current_pos;
4706   if (current_pos > position)
4707     current_pos -= MIN (current_pos, end_pos) - position;
4708
4709   selection_bound = priv->selection_bound;
4710   if (selection_bound > position)
4711     selection_bound -= MIN (selection_bound, end_pos) - position;
4712
4713   gtk_entry_set_positions (entry, current_pos, selection_bound);
4714
4715   /* We might have deleted the selection */
4716   gtk_entry_update_primary_selection (entry);
4717
4718   /* Disable the password hint if one exists. */
4719   if (!priv->visible)
4720     {
4721       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4722                                                                 quark_password_hint);
4723       if (password_hint)
4724         {
4725           if (password_hint->source_id)
4726             g_source_remove (password_hint->source_id);
4727           password_hint->source_id = 0;
4728           password_hint->position = -1;
4729         }
4730     }
4731 }
4732
4733 static void
4734 buffer_notify_text (GtkEntryBuffer *buffer,
4735                     GParamSpec     *spec,
4736                     GtkEntry       *entry)
4737 {
4738   gtk_entry_recompute (entry);
4739   emit_changed (entry);
4740   g_object_notify (G_OBJECT (entry), "text");
4741 }
4742
4743 static void
4744 buffer_notify_length (GtkEntryBuffer *buffer,
4745                       GParamSpec     *spec,
4746                       GtkEntry       *entry)
4747 {
4748   g_object_notify (G_OBJECT (entry), "text-length");
4749 }
4750
4751 static void
4752 buffer_notify_max_length (GtkEntryBuffer *buffer,
4753                           GParamSpec     *spec,
4754                           GtkEntry       *entry)
4755 {
4756   g_object_notify (G_OBJECT (entry), "max-length");
4757 }
4758
4759 static void
4760 buffer_connect_signals (GtkEntry *entry)
4761 {
4762   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4763   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4764   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4765   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4766   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4767 }
4768
4769 static void
4770 buffer_disconnect_signals (GtkEntry *entry)
4771 {
4772   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4773   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4774   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4775   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4776   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4777 }
4778
4779 /* Compute the X position for an offset that corresponds to the "more important
4780  * cursor position for that offset. We use this when trying to guess to which
4781  * end of the selection we should go to when the user hits the left or
4782  * right arrow key.
4783  */
4784 static gint
4785 get_better_cursor_x (GtkEntry *entry,
4786                      gint      offset)
4787 {
4788   GtkEntryPrivate *priv = entry->priv;
4789   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4790   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4791   gboolean split_cursor;
4792   
4793   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4794   const gchar *text = pango_layout_get_text (layout);
4795   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4796   
4797   PangoRectangle strong_pos, weak_pos;
4798   
4799   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4800                 "gtk-split-cursor", &split_cursor,
4801                 NULL);
4802
4803   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4804
4805   if (split_cursor)
4806     return strong_pos.x / PANGO_SCALE;
4807   else
4808     return (keymap_direction == priv->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4809 }
4810
4811 static void
4812 gtk_entry_move_cursor (GtkEntry       *entry,
4813                        GtkMovementStep step,
4814                        gint            count,
4815                        gboolean        extend_selection)
4816 {
4817   GtkEntryPrivate *priv = entry->priv;
4818   gint new_pos = priv->current_pos;
4819
4820   _gtk_entry_reset_im_context (entry);
4821
4822   if (priv->current_pos != priv->selection_bound && !extend_selection)
4823     {
4824       /* If we have a current selection and aren't extending it, move to the
4825        * start/or end of the selection as appropriate
4826        */
4827       switch (step)
4828         {
4829         case GTK_MOVEMENT_VISUAL_POSITIONS:
4830           {
4831             gint current_x = get_better_cursor_x (entry, priv->current_pos);
4832             gint bound_x = get_better_cursor_x (entry, priv->selection_bound);
4833
4834             if (count <= 0)
4835               new_pos = current_x < bound_x ? priv->current_pos : priv->selection_bound;
4836             else 
4837               new_pos = current_x > bound_x ? priv->current_pos : priv->selection_bound;
4838             break;
4839           }
4840         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4841         case GTK_MOVEMENT_WORDS:
4842           if (count < 0)
4843             new_pos = MIN (priv->current_pos, priv->selection_bound);
4844           else
4845             new_pos = MAX (priv->current_pos, priv->selection_bound);
4846           break;
4847         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4848         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4849         case GTK_MOVEMENT_BUFFER_ENDS:
4850           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4851           break;
4852         case GTK_MOVEMENT_DISPLAY_LINES:
4853         case GTK_MOVEMENT_PARAGRAPHS:
4854         case GTK_MOVEMENT_PAGES:
4855         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4856           break;
4857         }
4858     }
4859   else
4860     {
4861       switch (step)
4862         {
4863         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4864           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4865           break;
4866         case GTK_MOVEMENT_VISUAL_POSITIONS:
4867           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4868           if (priv->current_pos == new_pos)
4869             {
4870               if (!extend_selection)
4871                 {
4872                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4873                                                  count > 0 ?
4874                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4875                     {
4876                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4877
4878                       if (toplevel)
4879                         gtk_widget_child_focus (toplevel,
4880                                                 count > 0 ?
4881                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4882                     }
4883                 }
4884               else
4885                 {
4886                   gtk_widget_error_bell (GTK_WIDGET (entry));
4887                 }
4888             }
4889           break;
4890         case GTK_MOVEMENT_WORDS:
4891           while (count > 0)
4892             {
4893               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4894               count--;
4895             }
4896           while (count < 0)
4897             {
4898               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4899               count++;
4900             }
4901           if (priv->current_pos == new_pos)
4902             gtk_widget_error_bell (GTK_WIDGET (entry));
4903           break;
4904         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4905         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4906         case GTK_MOVEMENT_BUFFER_ENDS:
4907           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4908           if (priv->current_pos == new_pos)
4909             gtk_widget_error_bell (GTK_WIDGET (entry));
4910           break;
4911         case GTK_MOVEMENT_DISPLAY_LINES:
4912         case GTK_MOVEMENT_PARAGRAPHS:
4913         case GTK_MOVEMENT_PAGES:
4914         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4915           break;
4916         }
4917     }
4918
4919   if (extend_selection)
4920     gtk_editable_select_region (GTK_EDITABLE (entry), priv->selection_bound, new_pos);
4921   else
4922     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4923   
4924   gtk_entry_pend_cursor_blink (entry);
4925 }
4926
4927 static void
4928 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4929                             const gchar *str)
4930 {
4931   GtkEntryPrivate *priv = entry->priv;
4932   GtkEditable *editable = GTK_EDITABLE (entry);
4933   gint pos = priv->current_pos;
4934
4935   if (priv->editable)
4936     {
4937       _gtk_entry_reset_im_context (entry);
4938
4939       gtk_editable_insert_text (editable, str, -1, &pos);
4940       gtk_editable_set_position (editable, pos);
4941     }
4942 }
4943
4944 static void
4945 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4946                               GtkDeleteType   type,
4947                               gint            count)
4948 {
4949   GtkEntryPrivate *priv = entry->priv;
4950   GtkEditable *editable = GTK_EDITABLE (entry);
4951   gint start_pos = priv->current_pos;
4952   gint end_pos = priv->current_pos;
4953   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
4954   
4955   _gtk_entry_reset_im_context (entry);
4956
4957   if (!priv->editable)
4958     {
4959       gtk_widget_error_bell (GTK_WIDGET (entry));
4960       return;
4961     }
4962
4963   if (priv->selection_bound != priv->current_pos)
4964     {
4965       gtk_editable_delete_selection (editable);
4966       return;
4967     }
4968   
4969   switch (type)
4970     {
4971     case GTK_DELETE_CHARS:
4972       end_pos = gtk_entry_move_logically (entry, priv->current_pos, count);
4973       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
4974       break;
4975     case GTK_DELETE_WORDS:
4976       if (count < 0)
4977         {
4978           /* Move to end of current word, or if not on a word, end of previous word */
4979           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
4980           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4981         }
4982       else if (count > 0)
4983         {
4984           /* Move to beginning of current word, or if not on a word, begining of next word */
4985           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
4986           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4987         }
4988         
4989       /* Fall through */
4990     case GTK_DELETE_WORD_ENDS:
4991       while (count < 0)
4992         {
4993           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4994           count++;
4995         }
4996       while (count > 0)
4997         {
4998           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4999           count--;
5000         }
5001       gtk_editable_delete_text (editable, start_pos, end_pos);
5002       break;
5003     case GTK_DELETE_DISPLAY_LINE_ENDS:
5004     case GTK_DELETE_PARAGRAPH_ENDS:
5005       if (count < 0)
5006         gtk_editable_delete_text (editable, 0, priv->current_pos);
5007       else
5008         gtk_editable_delete_text (editable, priv->current_pos, -1);
5009       break;
5010     case GTK_DELETE_DISPLAY_LINES:
5011     case GTK_DELETE_PARAGRAPHS:
5012       gtk_editable_delete_text (editable, 0, -1);  
5013       break;
5014     case GTK_DELETE_WHITESPACE:
5015       gtk_entry_delete_whitespace (entry);
5016       break;
5017     }
5018
5019   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
5020     gtk_widget_error_bell (GTK_WIDGET (entry));
5021
5022   gtk_entry_pend_cursor_blink (entry);
5023 }
5024
5025 static void
5026 gtk_entry_backspace (GtkEntry *entry)
5027 {
5028   GtkEntryPrivate *priv = entry->priv;
5029   GtkEditable *editable = GTK_EDITABLE (entry);
5030   gint prev_pos;
5031
5032   _gtk_entry_reset_im_context (entry);
5033
5034   if (!priv->editable)
5035     {
5036       gtk_widget_error_bell (GTK_WIDGET (entry));
5037       return;
5038     }
5039
5040   if (priv->selection_bound != priv->current_pos)
5041     {
5042       gtk_editable_delete_selection (editable);
5043       return;
5044     }
5045
5046   prev_pos = gtk_entry_move_logically (entry, priv->current_pos, -1);
5047
5048   if (prev_pos < priv->current_pos)
5049     {
5050       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5051       PangoLogAttr *log_attrs;
5052       gint n_attrs;
5053
5054       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5055
5056       /* Deleting parts of characters */
5057       if (log_attrs[priv->current_pos].backspace_deletes_character)
5058         {
5059           gchar *cluster_text;
5060           gchar *normalized_text;
5061           glong  len;
5062
5063           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5064                                                      priv->current_pos);
5065           normalized_text = g_utf8_normalize (cluster_text,
5066                                               strlen (cluster_text),
5067                                               G_NORMALIZE_NFD);
5068           len = g_utf8_strlen (normalized_text, -1);
5069
5070           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5071           if (len > 1)
5072             {
5073               gint pos = priv->current_pos;
5074
5075               gtk_editable_insert_text (editable, normalized_text,
5076                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5077                                         &pos);
5078               gtk_editable_set_position (editable, pos);
5079             }
5080
5081           g_free (normalized_text);
5082           g_free (cluster_text);
5083         }
5084       else
5085         {
5086           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5087         }
5088       
5089       g_free (log_attrs);
5090     }
5091   else
5092     {
5093       gtk_widget_error_bell (GTK_WIDGET (entry));
5094     }
5095
5096   gtk_entry_pend_cursor_blink (entry);
5097 }
5098
5099 static void
5100 gtk_entry_copy_clipboard (GtkEntry *entry)
5101 {
5102   GtkEntryPrivate *priv = entry->priv;
5103   GtkEditable *editable = GTK_EDITABLE (entry);
5104   gint start, end;
5105   gchar *str;
5106
5107   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5108     {
5109       if (!priv->visible)
5110         {
5111           gtk_widget_error_bell (GTK_WIDGET (entry));
5112           return;
5113         }
5114
5115       str = gtk_entry_get_display_text (entry, start, end);
5116       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5117                                                         GDK_SELECTION_CLIPBOARD),
5118                               str, -1);
5119       g_free (str);
5120     }
5121 }
5122
5123 static void
5124 gtk_entry_cut_clipboard (GtkEntry *entry)
5125 {
5126   GtkEntryPrivate *priv = entry->priv;
5127   GtkEditable *editable = GTK_EDITABLE (entry);
5128   gint start, end;
5129
5130   if (!priv->visible)
5131     {
5132       gtk_widget_error_bell (GTK_WIDGET (entry));
5133       return;
5134     }
5135
5136   gtk_entry_copy_clipboard (entry);
5137
5138   if (priv->editable)
5139     {
5140       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5141         gtk_editable_delete_text (editable, start, end);
5142     }
5143   else
5144     {
5145       gtk_widget_error_bell (GTK_WIDGET (entry));
5146     }
5147 }
5148
5149 static void
5150 gtk_entry_paste_clipboard (GtkEntry *entry)
5151 {
5152   GtkEntryPrivate *priv = entry->priv;
5153
5154   if (priv->editable)
5155     gtk_entry_paste (entry, GDK_NONE);
5156   else
5157     gtk_widget_error_bell (GTK_WIDGET (entry));
5158 }
5159
5160 static void
5161 gtk_entry_delete_cb (GtkEntry *entry)
5162 {
5163   GtkEntryPrivate *priv = entry->priv;
5164   GtkEditable *editable = GTK_EDITABLE (entry);
5165   gint start, end;
5166
5167   if (priv->editable)
5168     {
5169       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5170         gtk_editable_delete_text (editable, start, end);
5171     }
5172 }
5173
5174 static void
5175 gtk_entry_toggle_overwrite (GtkEntry *entry)
5176 {
5177   GtkEntryPrivate *priv = entry->priv;
5178
5179   priv->overwrite_mode = !priv->overwrite_mode;
5180   gtk_entry_pend_cursor_blink (entry);
5181   gtk_widget_queue_draw (GTK_WIDGET (entry));
5182 }
5183
5184 static void
5185 gtk_entry_select_all (GtkEntry *entry)
5186 {
5187   gtk_entry_select_line (entry);
5188 }
5189
5190 static void
5191 gtk_entry_real_activate (GtkEntry *entry)
5192 {
5193   GtkEntryPrivate *priv = entry->priv;
5194   GtkWindow *window;
5195   GtkWidget *default_widget, *focus_widget;
5196   GtkWidget *toplevel;
5197   GtkWidget *widget;
5198
5199   widget = GTK_WIDGET (entry);
5200
5201   if (priv->activates_default)
5202     {
5203       toplevel = gtk_widget_get_toplevel (widget);
5204       if (GTK_IS_WINDOW (toplevel))
5205         {
5206           window = GTK_WINDOW (toplevel);
5207
5208           if (window)
5209             {
5210               default_widget = gtk_window_get_default_widget (window);
5211               focus_widget = gtk_window_get_focus (window);
5212               if (widget != default_widget &&
5213                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5214                 gtk_window_activate_default (window);
5215             }
5216         }
5217     }
5218 }
5219
5220 static void
5221 keymap_direction_changed (GdkKeymap *keymap,
5222                           GtkEntry  *entry)
5223 {
5224   gtk_entry_recompute (entry);
5225 }
5226
5227 /* IM Context Callbacks
5228  */
5229
5230 static void
5231 gtk_entry_commit_cb (GtkIMContext *context,
5232                      const gchar  *str,
5233                      GtkEntry     *entry)
5234 {
5235   GtkEntryPrivate *priv = entry->priv;
5236
5237   if (priv->editable)
5238     gtk_entry_enter_text (entry, str);
5239 }
5240
5241 static void 
5242 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5243                               GtkEntry     *entry)
5244 {
5245   GtkEntryPrivate *priv = entry->priv;
5246
5247   if (priv->editable)
5248     {
5249       gchar *preedit_string;
5250       gint cursor_pos;
5251
5252       gtk_im_context_get_preedit_string (priv->im_context,
5253                                          &preedit_string, NULL,
5254                                          &cursor_pos);
5255       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5256       priv->preedit_length = strlen (preedit_string);
5257       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5258       priv->preedit_cursor = cursor_pos;
5259       g_free (preedit_string);
5260     
5261       gtk_entry_recompute (entry);
5262     }
5263 }
5264
5265 static gboolean
5266 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5267                                    GtkEntry     *entry)
5268 {
5269   GtkEntryPrivate *priv = entry->priv;
5270   gchar *text;
5271
5272   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5273   text = gtk_entry_get_display_text (entry, 0, -1);
5274   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5275                                   g_utf8_offset_to_pointer (text, priv->current_pos) - text);
5276   g_free (text);
5277
5278   return TRUE;
5279 }
5280
5281 static gboolean
5282 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5283                                  gint          offset,
5284                                  gint          n_chars,
5285                                  GtkEntry     *entry)
5286 {
5287   GtkEntryPrivate *priv = entry->priv;
5288
5289   if (priv->editable)
5290     gtk_editable_delete_text (GTK_EDITABLE (entry),
5291                               priv->current_pos + offset,
5292                               priv->current_pos + offset + n_chars);
5293
5294   return TRUE;
5295 }
5296
5297 /* Internal functions
5298  */
5299
5300 /* Used for im_commit_cb and inserting Unicode chars */
5301 static void
5302 gtk_entry_enter_text (GtkEntry       *entry,
5303                       const gchar    *str)
5304 {
5305   GtkEntryPrivate *priv = entry->priv;
5306   GtkEditable *editable = GTK_EDITABLE (entry);
5307   gint tmp_pos;
5308   gboolean old_need_im_reset;
5309
5310   old_need_im_reset = priv->need_im_reset;
5311   priv->need_im_reset = FALSE;
5312
5313   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5314     gtk_editable_delete_selection (editable);
5315   else
5316     {
5317       if (priv->overwrite_mode)
5318         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5319     }
5320
5321   tmp_pos = priv->current_pos;
5322   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5323   gtk_editable_set_position (editable, tmp_pos);
5324
5325   priv->need_im_reset = old_need_im_reset;
5326 }
5327
5328 /* All changes to priv->current_pos and priv->selection_bound
5329  * should go through this function.
5330  */
5331 static void
5332 gtk_entry_set_positions (GtkEntry *entry,
5333                          gint      current_pos,
5334                          gint      selection_bound)
5335 {
5336   GtkEntryPrivate *priv = entry->priv;
5337   gboolean changed = FALSE;
5338
5339   g_object_freeze_notify (G_OBJECT (entry));
5340   
5341   if (current_pos != -1 &&
5342       priv->current_pos != current_pos)
5343     {
5344       priv->current_pos = current_pos;
5345       changed = TRUE;
5346
5347       g_object_notify (G_OBJECT (entry), "cursor-position");
5348     }
5349
5350   if (selection_bound != -1 &&
5351       priv->selection_bound != selection_bound)
5352     {
5353       priv->selection_bound = selection_bound;
5354       changed = TRUE;
5355       
5356       g_object_notify (G_OBJECT (entry), "selection-bound");
5357     }
5358
5359   g_object_thaw_notify (G_OBJECT (entry));
5360
5361   if (changed) 
5362     {
5363       gtk_entry_move_adjustments (entry);
5364       gtk_entry_recompute (entry);
5365     }
5366 }
5367
5368 static void
5369 gtk_entry_reset_layout (GtkEntry *entry)
5370 {
5371   GtkEntryPrivate *priv = entry->priv;
5372
5373   if (priv->cached_layout)
5374     {
5375       g_object_unref (priv->cached_layout);
5376       priv->cached_layout = NULL;
5377     }
5378 }
5379
5380 static void
5381 update_im_cursor_location (GtkEntry *entry)
5382 {
5383   GtkEntryPrivate *priv = entry->priv;
5384   GdkRectangle area;
5385   gint strong_x;
5386   gint strong_xoffset;
5387   gint area_width, area_height;
5388
5389   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5390   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5391
5392   strong_xoffset = strong_x - priv->scroll_offset;
5393   if (strong_xoffset < 0)
5394     {
5395       strong_xoffset = 0;
5396     }
5397   else if (strong_xoffset > area_width)
5398     {
5399       strong_xoffset = area_width;
5400     }
5401   area.x = strong_xoffset;
5402   area.y = 0;
5403   area.width = 0;
5404   area.height = area_height;
5405
5406   gtk_im_context_set_cursor_location (priv->im_context, &area);
5407 }
5408
5409 static gboolean
5410 recompute_idle_func (gpointer data)
5411 {
5412   GtkEntry *entry = GTK_ENTRY (data);
5413   GtkEntryPrivate *priv = entry->priv;
5414
5415   priv->recompute_idle = 0;
5416
5417   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5418     {
5419       gtk_entry_adjust_scroll (entry);
5420       gtk_entry_queue_draw (entry);
5421       
5422       update_im_cursor_location (entry);
5423     }
5424
5425   return FALSE;
5426 }
5427
5428 static void
5429 gtk_entry_recompute (GtkEntry *entry)
5430 {
5431   GtkEntryPrivate *priv = entry->priv;
5432
5433   gtk_entry_reset_layout (entry);
5434   gtk_entry_check_cursor_blink (entry);
5435
5436   if (!priv->recompute_idle)
5437     {
5438       priv->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5439                                                recompute_idle_func, entry, NULL); 
5440     }
5441 }
5442
5443 static PangoLayout *
5444 gtk_entry_create_layout (GtkEntry *entry,
5445                          gboolean  include_preedit)
5446 {
5447   GtkEntryPrivate *priv = entry->priv;
5448   GtkWidget *widget = GTK_WIDGET (entry);
5449   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5450   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5451
5452   gchar *preedit_string = NULL;
5453   gint preedit_length = 0;
5454   PangoAttrList *preedit_attrs = NULL;
5455
5456   gchar *display;
5457   guint n_bytes;
5458
5459   pango_layout_set_single_paragraph_mode (layout, TRUE);
5460
5461   display = gtk_entry_get_display_text (entry, 0, -1);
5462   n_bytes = strlen (display);
5463
5464   if (include_preedit)
5465     {
5466       gtk_im_context_get_preedit_string (priv->im_context,
5467                                          &preedit_string, &preedit_attrs, NULL);
5468       preedit_length = priv->preedit_length;
5469     }
5470
5471   if (preedit_length)
5472     {
5473       GString *tmp_string = g_string_new (display);
5474       gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
5475
5476       g_string_insert (tmp_string, cursor_index, preedit_string);
5477       
5478       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5479       
5480       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5481                               cursor_index, preedit_length);
5482       
5483       g_string_free (tmp_string, TRUE);
5484     }
5485   else
5486     {
5487       PangoDirection pango_dir;
5488       
5489       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5490         pango_dir = pango_find_base_dir (display, n_bytes);
5491       else
5492         pango_dir = PANGO_DIRECTION_NEUTRAL;
5493
5494       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5495         {
5496           if (gtk_widget_has_focus (widget))
5497             {
5498               GdkDisplay *display = gtk_widget_get_display (widget);
5499               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5500               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5501                 pango_dir = PANGO_DIRECTION_RTL;
5502               else
5503                 pango_dir = PANGO_DIRECTION_LTR;
5504             }
5505           else
5506             {
5507               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5508                 pango_dir = PANGO_DIRECTION_RTL;
5509               else
5510                 pango_dir = PANGO_DIRECTION_LTR;
5511             }
5512         }
5513
5514       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5515                                   pango_dir);
5516
5517       priv->resolved_dir = pango_dir;
5518
5519       pango_layout_set_text (layout, display, n_bytes);
5520     }
5521       
5522   pango_layout_set_attributes (layout, tmp_attrs);
5523
5524   g_free (preedit_string);
5525   g_free (display);
5526
5527   if (preedit_attrs)
5528     pango_attr_list_unref (preedit_attrs);
5529       
5530   pango_attr_list_unref (tmp_attrs);
5531
5532   return layout;
5533 }
5534
5535 static PangoLayout *
5536 gtk_entry_ensure_layout (GtkEntry *entry,
5537                          gboolean  include_preedit)
5538 {
5539   GtkEntryPrivate *priv = entry->priv;
5540
5541   if (priv->preedit_length > 0 &&
5542       !include_preedit != !priv->cache_includes_preedit)
5543     gtk_entry_reset_layout (entry);
5544
5545   if (!priv->cached_layout)
5546     {
5547       priv->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5548       priv->cache_includes_preedit = include_preedit;
5549     }
5550
5551   return priv->cached_layout;
5552 }
5553
5554 static void
5555 get_layout_position (GtkEntry *entry,
5556                      gint     *x,
5557                      gint     *y)
5558 {
5559   GtkEntryPrivate *priv = entry->priv;
5560   PangoLayout *layout;
5561   PangoRectangle logical_rect;
5562   gint area_width, area_height;
5563   GtkBorder inner_border;
5564   gint y_pos;
5565   PangoLayoutLine *line;
5566   
5567   layout = gtk_entry_ensure_layout (entry, TRUE);
5568
5569   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5570   _gtk_entry_effective_inner_border (entry, &inner_border);
5571
5572   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5573
5574   line = pango_layout_get_lines_readonly (layout)->data;
5575   pango_layout_line_get_extents (line, NULL, &logical_rect);
5576   
5577   /* Align primarily for locale's ascent/descent */
5578   y_pos = ((area_height - priv->ascent - priv->descent) / 2 +
5579            priv->ascent + logical_rect.y);
5580
5581   /* Now see if we need to adjust to fit in actual drawn string */
5582   if (logical_rect.height > area_height)
5583     y_pos = (area_height - logical_rect.height) / 2;
5584   else if (y_pos < 0)
5585     y_pos = 0;
5586   else if (y_pos + logical_rect.height > area_height)
5587     y_pos = area_height - logical_rect.height;
5588   
5589   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5590
5591   if (x)
5592     *x = inner_border.left - priv->scroll_offset;
5593
5594   if (y)
5595     *y = y_pos;
5596 }
5597
5598 static void
5599 draw_text_with_color (GtkEntry *entry, cairo_t *cr, GdkColor *default_color)
5600 {
5601   GtkEntryPrivate *priv = entry->priv;
5602   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5603   GtkWidget *widget;
5604   gint x, y;
5605   gint start_pos, end_pos;
5606
5607   widget = GTK_WIDGET (entry);
5608
5609   cairo_save (cr);
5610
5611   get_layout_position (entry, &x, &y);
5612
5613   cairo_move_to (cr, x, y);
5614   gdk_cairo_set_source_color (cr, default_color);
5615   pango_cairo_show_layout (cr, layout);
5616
5617   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5618     {
5619       gint *ranges;
5620       gint n_ranges, i;
5621       PangoRectangle logical_rect;
5622       GdkColor *selection_color, *text_color;
5623       GtkBorder inner_border;
5624       GtkStyle *style;
5625
5626       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5627       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5628
5629       style = gtk_widget_get_style (widget);
5630
5631       if (gtk_widget_has_focus (widget))
5632         {
5633           selection_color = &style->base [GTK_STATE_SELECTED];
5634           text_color = &style->text [GTK_STATE_SELECTED];
5635         }
5636       else
5637         {
5638           selection_color = &style->base [GTK_STATE_ACTIVE];
5639           text_color = &style->text [GTK_STATE_ACTIVE];
5640         }
5641
5642       _gtk_entry_effective_inner_border (entry, &inner_border);
5643
5644       for (i = 0; i < n_ranges; ++i)
5645         cairo_rectangle (cr,
5646                          inner_border.left - priv->scroll_offset + ranges[2 * i],
5647                          y,
5648                          ranges[2 * i + 1],
5649                          logical_rect.height);
5650
5651       cairo_clip (cr);
5652           
5653       gdk_cairo_set_source_color (cr, selection_color);
5654       cairo_paint (cr);
5655
5656       cairo_move_to (cr, x, y);
5657       gdk_cairo_set_source_color (cr, text_color);
5658       pango_cairo_show_layout (cr, layout);
5659   
5660       g_free (ranges);
5661     }
5662   cairo_restore (cr);
5663 }
5664
5665 static void
5666 gtk_entry_draw_text (GtkEntry *entry,
5667                      cairo_t  *cr)
5668 {
5669   GtkEntryPrivate *priv = entry->priv;
5670   GtkWidget *widget = GTK_WIDGET (entry);
5671   GtkStateType state;
5672   GtkStyle *style;
5673   GdkColor text_color, bar_text_color;
5674   gint pos_x, pos_y;
5675   gint width, height;
5676   gint progress_x, progress_y, progress_width, progress_height;
5677   gint clip_width, clip_height;
5678
5679   /* Nothing to display at all */
5680   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5681     return;
5682   
5683   state = GTK_STATE_SELECTED;
5684   if (!gtk_widget_get_sensitive (widget))
5685     state = GTK_STATE_INSENSITIVE;
5686   style = gtk_widget_get_style (widget);
5687   text_color = style->text[gtk_widget_get_state (widget)];
5688   bar_text_color = style->fg[state];
5689
5690   get_progress_area (widget,
5691                      &progress_x, &progress_y,
5692                      &progress_width, &progress_height);
5693
5694   clip_width = gdk_window_get_width (priv->text_area);
5695   clip_height = gdk_window_get_height (priv->text_area);
5696   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5697   cairo_clip (cr);
5698
5699   /* If the color is the same, or the progress area has a zero
5700    * size, then we only need to draw once. */
5701   if ((text_color.pixel == bar_text_color.pixel) ||
5702       ((progress_width == 0) || (progress_height == 0)))
5703     {
5704       draw_text_with_color (entry, cr, &text_color);
5705     }
5706   else
5707     {
5708       width = gdk_window_get_width (priv->text_area);
5709       height = gdk_window_get_height (priv->text_area);
5710
5711       cairo_save (cr);
5712
5713       cairo_rectangle (cr, 0, 0, width, height);
5714       cairo_clip (cr);
5715       cairo_save (cr);
5716
5717       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5718       cairo_rectangle (cr, 0, 0, width, height);
5719
5720       gdk_window_get_position (priv->text_area, &pos_x, &pos_y);
5721       progress_x -= pos_x;
5722       progress_y -= pos_y;
5723
5724       cairo_rectangle (cr, progress_x, progress_y,
5725                        progress_width, progress_height);
5726       cairo_clip (cr);
5727       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5728   
5729       draw_text_with_color (entry, cr, &text_color);
5730       cairo_restore (cr);
5731
5732       cairo_rectangle (cr, progress_x, progress_y,
5733                        progress_width, progress_height);
5734       cairo_clip (cr);
5735
5736       draw_text_with_color (entry, cr, &bar_text_color);
5737
5738       cairo_restore (cr);
5739     }
5740 }
5741
5742 static void
5743 draw_insertion_cursor (GtkEntry      *entry,
5744                        cairo_t       *cr,
5745                        GdkRectangle  *cursor_location,
5746                        gboolean       is_primary,
5747                        PangoDirection direction,
5748                        gboolean       draw_arrow)
5749 {
5750   GtkWidget *widget = GTK_WIDGET (entry);
5751   GtkTextDirection text_dir;
5752
5753   if (direction == PANGO_DIRECTION_LTR)
5754     text_dir = GTK_TEXT_DIR_LTR;
5755   else
5756     text_dir = GTK_TEXT_DIR_RTL;
5757
5758   gtk_draw_insertion_cursor (widget, cr,
5759                              cursor_location,
5760                              is_primary, text_dir, draw_arrow);
5761 }
5762
5763 static void
5764 gtk_entry_draw_cursor (GtkEntry  *entry,
5765                        cairo_t   *cr,
5766                        CursorType type)
5767 {
5768   GtkEntryPrivate *priv = entry->priv;
5769   GtkWidget *widget = GTK_WIDGET (entry);
5770   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5771   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5772   GdkRectangle cursor_location;
5773   gboolean split_cursor;
5774   PangoRectangle cursor_rect;
5775   GtkBorder inner_border;
5776   gint xoffset;
5777   gint text_area_height;
5778   gint cursor_index;
5779   gboolean block;
5780   gboolean block_at_line_end;
5781   PangoLayout *layout;
5782   const char *text;
5783
5784   _gtk_entry_effective_inner_border (entry, &inner_border);
5785
5786   xoffset = inner_border.left - priv->scroll_offset;
5787
5788   text_area_height = gdk_window_get_height (priv->text_area);
5789
5790   layout = gtk_entry_ensure_layout (entry, TRUE);
5791   text = pango_layout_get_text (layout);
5792   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
5793   if (!priv->overwrite_mode)
5794     block = FALSE;
5795   else
5796     block = _gtk_text_util_get_block_cursor_location (layout,
5797                                                       cursor_index, &cursor_rect, &block_at_line_end);
5798
5799   if (!block)
5800     {
5801       gint strong_x, weak_x;
5802       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
5803       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
5804       gint x1 = 0;
5805       gint x2 = 0;
5806
5807       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
5808
5809       g_object_get (gtk_widget_get_settings (widget),
5810                     "gtk-split-cursor", &split_cursor,
5811                     NULL);
5812
5813       dir1 = priv->resolved_dir;
5814   
5815       if (split_cursor)
5816         {
5817           x1 = strong_x;
5818
5819           if (weak_x != strong_x)
5820             {
5821               dir2 = (priv->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
5822               x2 = weak_x;
5823             }
5824         }
5825       else
5826         {
5827           if (keymap_direction == priv->resolved_dir)
5828             x1 = strong_x;
5829           else
5830             x1 = weak_x;
5831         }
5832
5833       cursor_location.x = xoffset + x1;
5834       cursor_location.y = inner_border.top;
5835       cursor_location.width = 0;
5836       cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
5837
5838       draw_insertion_cursor (entry, cr,
5839                              &cursor_location, TRUE, dir1,
5840                              dir2 != PANGO_DIRECTION_NEUTRAL);
5841   
5842       if (dir2 != PANGO_DIRECTION_NEUTRAL)
5843         {
5844           cursor_location.x = xoffset + x2;
5845           draw_insertion_cursor (entry, cr,
5846                                  &cursor_location, FALSE, dir2,
5847                                  TRUE);
5848         }
5849     }
5850   else /* overwrite_mode */
5851     {
5852       GdkColor cursor_color;
5853       GdkRectangle rect;
5854       gint x, y;
5855
5856       cairo_save (cr);
5857
5858       get_layout_position (entry, &x, &y);
5859
5860       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5861       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5862       rect.width = PANGO_PIXELS (cursor_rect.width);
5863       rect.height = PANGO_PIXELS (cursor_rect.height);
5864
5865       _gtk_widget_get_cursor_color (widget, &cursor_color);
5866       gdk_cairo_set_source_color (cr, &cursor_color);
5867       gdk_cairo_rectangle (cr, &rect);
5868       cairo_fill (cr);
5869
5870       if (!block_at_line_end)
5871         {
5872           gdk_cairo_rectangle (cr, &rect);
5873           cairo_clip (cr);
5874           cairo_move_to (cr, x, y);
5875           gdk_cairo_set_source_color (cr, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
5876           pango_cairo_show_layout (cr, layout);
5877         }
5878
5879       cairo_restore (cr);
5880     }
5881 }
5882
5883 static void
5884 gtk_entry_queue_draw (GtkEntry *entry)
5885 {
5886   if (gtk_widget_is_drawable (GTK_WIDGET (entry)))
5887     gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (entry)),
5888                                 NULL, FALSE);
5889 }
5890
5891 void
5892 _gtk_entry_reset_im_context (GtkEntry *entry)
5893 {
5894   GtkEntryPrivate *priv = entry->priv;
5895
5896   if (priv->need_im_reset)
5897     {
5898       priv->need_im_reset = FALSE;
5899       gtk_im_context_reset (priv->im_context);
5900     }
5901 }
5902
5903 /**
5904  * gtk_entry_reset_im_context:
5905  * @entry: a #GtkEntry
5906  *
5907  * Reset the input method context of the entry if needed.
5908  *
5909  * This can be necessary in the case where modifying the buffer
5910  * would confuse on-going input method behavior.
5911  *
5912  * Since: 2.22
5913  */
5914 void
5915 gtk_entry_reset_im_context (GtkEntry *entry)
5916 {
5917   g_return_if_fail (GTK_IS_ENTRY (entry));
5918
5919   _gtk_entry_reset_im_context (entry);
5920 }
5921
5922 /**
5923  * gtk_entry_im_context_filter_keypress:
5924  * @entry: a #GtkEntry
5925  * @event: the key event
5926  *
5927  * Allow the #GtkEntry input method to internally handle key press
5928  * and release events. If this function returns %TRUE, then no further
5929  * processing should be done for this key event. See
5930  * gtk_im_context_filter_keypress().
5931  *
5932  * Note that you are expected to call this function from your handler
5933  * when overriding key event handling. This is needed in the case when
5934  * you need to insert your own key handling between the input method
5935  * and the default key event handling of the #GtkEntry.
5936  * See gtk_text_view_reset_im_context() for an example of use.
5937  *
5938  * Return value: %TRUE if the input method handled the key event.
5939  *
5940  * Since: 2.22
5941  */
5942 gboolean
5943 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
5944                                       GdkEventKey *event)
5945 {
5946   GtkEntryPrivate *priv;
5947
5948   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
5949
5950   priv = entry->priv;
5951
5952   return gtk_im_context_filter_keypress (priv->im_context, event);
5953 }
5954
5955 GtkIMContext*
5956 _gtk_entry_get_im_context (GtkEntry *entry)
5957 {
5958   return entry->priv->im_context;
5959 }
5960
5961 static gint
5962 gtk_entry_find_position (GtkEntry *entry,
5963                          gint      x)
5964 {
5965   GtkEntryPrivate *priv = entry->priv;
5966   PangoLayout *layout;
5967   PangoLayoutLine *line;
5968   gint index;
5969   gint pos;
5970   gint trailing;
5971   const gchar *text;
5972   gint cursor_index;
5973   
5974   layout = gtk_entry_ensure_layout (entry, TRUE);
5975   text = pango_layout_get_text (layout);
5976   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
5977
5978   line = pango_layout_get_lines_readonly (layout)->data;
5979   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
5980
5981   if (index >= cursor_index && priv->preedit_length)
5982     {
5983       if (index >= cursor_index + priv->preedit_length)
5984         index -= priv->preedit_length;
5985       else
5986         {
5987           index = cursor_index;
5988           trailing = 0;
5989         }
5990     }
5991
5992   pos = g_utf8_pointer_to_offset (text, text + index);
5993   pos += trailing;
5994
5995   return pos;
5996 }
5997
5998 static void
5999 gtk_entry_get_cursor_locations (GtkEntry   *entry,
6000                                 CursorType  type,
6001                                 gint       *strong_x,
6002                                 gint       *weak_x)
6003 {
6004   GtkEntryPrivate *priv = entry->priv;
6005   DisplayMode mode = gtk_entry_get_display_mode (entry);
6006
6007   /* Nothing to display at all, so no cursor is relevant */
6008   if (mode == DISPLAY_BLANK)
6009     {
6010       if (strong_x)
6011         *strong_x = 0;
6012       
6013       if (weak_x)
6014         *weak_x = 0;
6015     }
6016   else
6017     {
6018       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
6019       const gchar *text = pango_layout_get_text (layout);
6020       PangoRectangle strong_pos, weak_pos;
6021       gint index;
6022   
6023       if (type == CURSOR_STANDARD)
6024         {
6025           index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
6026         }
6027       else /* type == CURSOR_DND */
6028         {
6029           index = g_utf8_offset_to_pointer (text, priv->dnd_position) - text;
6030
6031           if (priv->dnd_position > priv->current_pos)
6032             {
6033               if (mode == DISPLAY_NORMAL)
6034                 index += priv->preedit_length;
6035               else
6036                 {
6037                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
6038                   index += preedit_len_chars * g_unichar_to_utf8 (priv->invisible_char, NULL);
6039                 }
6040             }
6041         }
6042       
6043       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
6044       
6045       if (strong_x)
6046         *strong_x = strong_pos.x / PANGO_SCALE;
6047       
6048       if (weak_x)
6049         *weak_x = weak_pos.x / PANGO_SCALE;
6050     }
6051 }
6052
6053 static void
6054 gtk_entry_adjust_scroll (GtkEntry *entry)
6055 {
6056   GtkEntryPrivate *priv = entry->priv;
6057   gint min_offset, max_offset;
6058   gint text_area_width, text_width;
6059   GtkBorder inner_border;
6060   gint strong_x, weak_x;
6061   gint strong_xoffset, weak_xoffset;
6062   gfloat xalign;
6063   PangoLayout *layout;
6064   PangoLayoutLine *line;
6065   PangoRectangle logical_rect;
6066
6067   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6068     return;
6069
6070   _gtk_entry_effective_inner_border (entry, &inner_border);
6071
6072   text_area_width = gdk_window_get_width (priv->text_area);
6073   text_area_width -= inner_border.left + inner_border.right;
6074   if (text_area_width < 0)
6075     text_area_width = 0;
6076
6077   layout = gtk_entry_ensure_layout (entry, TRUE);
6078   line = pango_layout_get_lines_readonly (layout)->data;
6079
6080   pango_layout_line_get_extents (line, NULL, &logical_rect);
6081
6082   /* Display as much text as we can */
6083
6084   if (priv->resolved_dir == PANGO_DIRECTION_LTR)
6085       xalign = priv->xalign;
6086   else
6087       xalign = 1.0 - priv->xalign;
6088
6089   text_width = PANGO_PIXELS(logical_rect.width);
6090
6091   if (text_width > text_area_width)
6092     {
6093       min_offset = 0;
6094       max_offset = text_width - text_area_width;
6095     }
6096   else
6097     {
6098       min_offset = (text_width - text_area_width) * xalign;
6099       max_offset = min_offset;
6100     }
6101
6102   priv->scroll_offset = CLAMP (priv->scroll_offset, min_offset, max_offset);
6103
6104   /* And make sure cursors are on screen. Note that the cursor is
6105    * actually drawn one pixel into the INNER_BORDER space on
6106    * the right, when the scroll is at the utmost right. This
6107    * looks better to to me than confining the cursor inside the
6108    * border entirely, though it means that the cursor gets one
6109    * pixel closer to the edge of the widget on the right than
6110    * on the left. This might need changing if one changed
6111    * INNER_BORDER from 2 to 1, as one would do on a
6112    * small-screen-real-estate display.
6113    *
6114    * We always make sure that the strong cursor is on screen, and
6115    * put the weak cursor on screen if possible.
6116    */
6117
6118   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
6119   
6120   strong_xoffset = strong_x - priv->scroll_offset;
6121
6122   if (strong_xoffset < 0)
6123     {
6124       priv->scroll_offset += strong_xoffset;
6125       strong_xoffset = 0;
6126     }
6127   else if (strong_xoffset > text_area_width)
6128     {
6129       priv->scroll_offset += strong_xoffset - text_area_width;
6130       strong_xoffset = text_area_width;
6131     }
6132
6133   weak_xoffset = weak_x - priv->scroll_offset;
6134
6135   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
6136     {
6137       priv->scroll_offset += weak_xoffset;
6138     }
6139   else if (weak_xoffset > text_area_width &&
6140            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
6141     {
6142       priv->scroll_offset += weak_xoffset - text_area_width;
6143     }
6144
6145   g_object_notify (G_OBJECT (entry), "scroll-offset");
6146 }
6147
6148 static void
6149 gtk_entry_move_adjustments (GtkEntry *entry)
6150 {
6151   GtkWidget *widget = GTK_WIDGET (entry);
6152   GtkAllocation allocation;
6153   GtkAdjustment *adjustment;
6154   PangoContext *context;
6155   PangoFontMetrics *metrics;
6156   gint x, layout_x, border_x, border_y;
6157   gint char_width;
6158
6159   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6160   if (!adjustment)
6161     return;
6162
6163   gtk_widget_get_allocation (widget, &allocation);
6164
6165   /* Cursor position, layout offset, border width, and widget allocation */
6166   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6167   get_layout_position (entry, &layout_x, NULL);
6168   _gtk_entry_get_borders (entry, &border_x, &border_y);
6169   x += allocation.x + layout_x + border_x;
6170
6171   /* Approximate width of a char, so user can see what is ahead/behind */
6172   context = gtk_widget_get_pango_context (widget);
6173   metrics = pango_context_get_metrics (context, 
6174                                        gtk_widget_get_style (widget)->font_desc,
6175                                        pango_context_get_language (context));
6176   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6177
6178   /* Scroll it */
6179   gtk_adjustment_clamp_page (adjustment, 
6180                              x - (char_width + 1),   /* one char + one pixel before */
6181                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6182 }
6183
6184 static gint
6185 gtk_entry_move_visually (GtkEntry *entry,
6186                          gint      start,
6187                          gint      count)
6188 {
6189   GtkEntryPrivate *priv = entry->priv;
6190   gint index;
6191   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6192   const gchar *text;
6193
6194   text = pango_layout_get_text (layout);
6195   
6196   index = g_utf8_offset_to_pointer (text, start) - text;
6197
6198   while (count != 0)
6199     {
6200       int new_index, new_trailing;
6201       gboolean split_cursor;
6202       gboolean strong;
6203
6204       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6205                     "gtk-split-cursor", &split_cursor,
6206                     NULL);
6207
6208       if (split_cursor)
6209         strong = TRUE;
6210       else
6211         {
6212           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6213           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6214
6215           strong = keymap_direction == priv->resolved_dir;
6216         }
6217       
6218       if (count > 0)
6219         {
6220           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6221           count--;
6222         }
6223       else
6224         {
6225           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6226           count++;
6227         }
6228
6229       if (new_index < 0)
6230         index = 0;
6231       else if (new_index != G_MAXINT)
6232         index = new_index;
6233       
6234       while (new_trailing--)
6235         index = g_utf8_next_char (text + index) - text;
6236     }
6237   
6238   return g_utf8_pointer_to_offset (text, text + index);
6239 }
6240
6241 static gint
6242 gtk_entry_move_logically (GtkEntry *entry,
6243                           gint      start,
6244                           gint      count)
6245 {
6246   gint new_pos = start;
6247   guint length;
6248
6249   length = gtk_entry_buffer_get_length (get_buffer (entry));
6250
6251   /* Prevent any leak of information */
6252   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6253     {
6254       new_pos = CLAMP (start + count, 0, length);
6255     }
6256   else
6257     {
6258       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6259       PangoLogAttr *log_attrs;
6260       gint n_attrs;
6261
6262       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6263
6264       while (count > 0 && new_pos < length)
6265         {
6266           do
6267             new_pos++;
6268           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6269           
6270           count--;
6271         }
6272       while (count < 0 && new_pos > 0)
6273         {
6274           do
6275             new_pos--;
6276           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6277           
6278           count++;
6279         }
6280       
6281       g_free (log_attrs);
6282     }
6283
6284   return new_pos;
6285 }
6286
6287 static gint
6288 gtk_entry_move_forward_word (GtkEntry *entry,
6289                              gint      start,
6290                              gboolean  allow_whitespace)
6291 {
6292   gint new_pos = start;
6293   guint length;
6294
6295   length = gtk_entry_buffer_get_length (get_buffer (entry));
6296
6297   /* Prevent any leak of information */
6298   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6299     {
6300       new_pos = length;
6301     }
6302   else if (new_pos < length)
6303     {
6304       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6305       PangoLogAttr *log_attrs;
6306       gint n_attrs;
6307
6308       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6309       
6310       /* Find the next word boundary */
6311       new_pos++;
6312       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6313                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6314         new_pos++;
6315
6316       g_free (log_attrs);
6317     }
6318
6319   return new_pos;
6320 }
6321
6322
6323 static gint
6324 gtk_entry_move_backward_word (GtkEntry *entry,
6325                               gint      start,
6326                               gboolean  allow_whitespace)
6327 {
6328   gint new_pos = start;
6329
6330   /* Prevent any leak of information */
6331   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6332     {
6333       new_pos = 0;
6334     }
6335   else if (start > 0)
6336     {
6337       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6338       PangoLogAttr *log_attrs;
6339       gint n_attrs;
6340
6341       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6342
6343       new_pos = start - 1;
6344
6345       /* Find the previous word boundary */
6346       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6347                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6348         new_pos--;
6349
6350       g_free (log_attrs);
6351     }
6352
6353   return new_pos;
6354 }
6355
6356 static void
6357 gtk_entry_delete_whitespace (GtkEntry *entry)
6358 {
6359   GtkEntryPrivate *priv = entry->priv;
6360   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6361   PangoLogAttr *log_attrs;
6362   gint n_attrs;
6363   gint start, end;
6364
6365   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6366
6367   start = end = priv->current_pos;
6368   
6369   while (start > 0 && log_attrs[start-1].is_white)
6370     start--;
6371
6372   while (end < n_attrs && log_attrs[end].is_white)
6373     end++;
6374
6375   g_free (log_attrs);
6376
6377   if (start != end)
6378     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6379 }
6380
6381
6382 static void
6383 gtk_entry_select_word (GtkEntry *entry)
6384 {
6385   GtkEntryPrivate *priv = entry->priv;
6386   gint start_pos = gtk_entry_move_backward_word (entry, priv->current_pos, TRUE);
6387   gint end_pos = gtk_entry_move_forward_word (entry, priv->current_pos, TRUE);
6388
6389   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6390 }
6391
6392 static void
6393 gtk_entry_select_line (GtkEntry *entry)
6394 {
6395   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6396 }
6397
6398 static gint
6399 truncate_multiline (const gchar *text)
6400 {
6401   gint length;
6402
6403   for (length = 0;
6404        text[length] && text[length] != '\n' && text[length] != '\r';
6405        length++);
6406
6407   return length;
6408 }
6409
6410 static void
6411 paste_received (GtkClipboard *clipboard,
6412                 const gchar  *text,
6413                 gpointer      data)
6414 {
6415   GtkEntry *entry = GTK_ENTRY (data);
6416   GtkEditable *editable = GTK_EDITABLE (entry);
6417   GtkEntryPrivate *priv = entry->priv;
6418
6419   if (priv->button == 2)
6420     {
6421       gint pos, start, end;
6422       pos = priv->insert_pos;
6423       gtk_editable_get_selection_bounds (editable, &start, &end);
6424       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6425         gtk_editable_select_region (editable, pos, pos);
6426     }
6427       
6428   if (text)
6429     {
6430       gint pos, start, end;
6431       gint length = -1;
6432       gboolean popup_completion;
6433       GtkEntryCompletion *completion;
6434
6435       completion = gtk_entry_get_completion (entry);
6436
6437       if (priv->truncate_multiline)
6438         length = truncate_multiline (text);
6439
6440       /* only complete if the selection is at the end */
6441       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6442                           MAX (priv->current_pos, priv->selection_bound));
6443
6444       if (completion)
6445         {
6446           if (gtk_widget_get_mapped (completion->priv->popup_window))
6447             _gtk_entry_completion_popdown (completion);
6448
6449           if (!popup_completion && completion->priv->changed_id > 0)
6450             g_signal_handler_block (entry, completion->priv->changed_id);
6451         }
6452
6453       begin_change (entry);
6454       g_object_freeze_notify (G_OBJECT (entry));
6455       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6456         gtk_editable_delete_text (editable, start, end);
6457
6458       pos = priv->current_pos;
6459       gtk_editable_insert_text (editable, text, length, &pos);
6460       gtk_editable_set_position (editable, pos);
6461       g_object_thaw_notify (G_OBJECT (entry));
6462       end_change (entry);
6463
6464       if (completion &&
6465           !popup_completion && completion->priv->changed_id > 0)
6466         g_signal_handler_unblock (entry, completion->priv->changed_id);
6467     }
6468
6469   g_object_unref (entry);
6470 }
6471
6472 static void
6473 gtk_entry_paste (GtkEntry *entry,
6474                  GdkAtom   selection)
6475 {
6476   g_object_ref (entry);
6477   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6478                               paste_received, entry);
6479 }
6480
6481 static void
6482 primary_get_cb (GtkClipboard     *clipboard,
6483                 GtkSelectionData *selection_data,
6484                 guint             info,
6485                 gpointer          data)
6486 {
6487   GtkEntry *entry = GTK_ENTRY (data);
6488   gint start, end;
6489   
6490   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6491     {
6492       gchar *str = gtk_entry_get_display_text (entry, start, end);
6493       gtk_selection_data_set_text (selection_data, str, -1);
6494       g_free (str);
6495     }
6496 }
6497
6498 static void
6499 primary_clear_cb (GtkClipboard *clipboard,
6500                   gpointer      data)
6501 {
6502   GtkEntry *entry = GTK_ENTRY (data);
6503   GtkEntryPrivate *priv = entry->priv;
6504
6505   gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
6506 }
6507
6508 static void
6509 gtk_entry_update_primary_selection (GtkEntry *entry)
6510 {
6511   GtkTargetList *list;
6512   GtkTargetEntry *targets;
6513   GtkClipboard *clipboard;
6514   gint start, end;
6515   gint n_targets;
6516
6517   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6518     return;
6519
6520   list = gtk_target_list_new (NULL, 0);
6521   gtk_target_list_add_text_targets (list, 0);
6522
6523   targets = gtk_target_table_new_from_list (list, &n_targets);
6524
6525   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6526   
6527   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6528     {
6529       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6530                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6531         primary_clear_cb (clipboard, entry);
6532     }
6533   else
6534     {
6535       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6536         gtk_clipboard_clear (clipboard);
6537     }
6538
6539   gtk_target_table_free (targets, n_targets);
6540   gtk_target_list_unref (list);
6541 }
6542
6543 static void
6544 gtk_entry_clear (GtkEntry             *entry,
6545                  GtkEntryIconPosition  icon_pos)
6546 {
6547   GtkEntryPrivate *priv = entry->priv;
6548   EntryIconInfo *icon_info = priv->icons[icon_pos];
6549
6550   if (!icon_info || icon_info->storage_type == GTK_IMAGE_EMPTY)
6551     return;
6552
6553   g_object_freeze_notify (G_OBJECT (entry));
6554
6555   /* Explicitly check, as the pointer may become invalidated
6556    * during destruction.
6557    */
6558   if (GDK_IS_WINDOW (icon_info->window))
6559     gdk_window_hide (icon_info->window);
6560
6561   if (icon_info->pixbuf)
6562     {
6563       g_object_unref (icon_info->pixbuf);
6564       icon_info->pixbuf = NULL;
6565     }
6566
6567   switch (icon_info->storage_type)
6568     {
6569     case GTK_IMAGE_PIXBUF:
6570       g_object_notify (G_OBJECT (entry),
6571                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6572       break;
6573
6574     case GTK_IMAGE_STOCK:
6575       g_free (icon_info->stock_id);
6576       icon_info->stock_id = NULL;
6577       g_object_notify (G_OBJECT (entry),
6578                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6579       break;
6580
6581     case GTK_IMAGE_ICON_NAME:
6582       g_free (icon_info->icon_name);
6583       icon_info->icon_name = NULL;
6584       g_object_notify (G_OBJECT (entry),
6585                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6586       break;
6587
6588     case GTK_IMAGE_GICON:
6589       if (icon_info->gicon)
6590         {
6591           g_object_unref (icon_info->gicon);
6592           icon_info->gicon = NULL;
6593         }
6594       g_object_notify (G_OBJECT (entry),
6595                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6596       break;
6597
6598     default:
6599       g_assert_not_reached ();
6600       break;
6601     }
6602
6603   icon_info->storage_type = GTK_IMAGE_EMPTY;
6604   g_object_notify (G_OBJECT (entry),
6605                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6606
6607   g_object_thaw_notify (G_OBJECT (entry));
6608 }
6609
6610 static void
6611 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6612                          GtkEntryIconPosition  icon_pos)
6613 {
6614   GtkEntryPrivate *priv = entry->priv;
6615   EntryIconInfo *icon_info = priv->icons[icon_pos];
6616   GtkIconInfo *info;
6617   GtkIconTheme *icon_theme;
6618   GtkSettings *settings;
6619   GtkStateFlags state;
6620   GtkWidget *widget;
6621   GdkScreen *screen;
6622   gint width, height;
6623
6624   if (!icon_info || icon_info->pixbuf)
6625     return;
6626
6627   widget = GTK_WIDGET (entry);
6628
6629   switch (icon_info->storage_type)
6630     {
6631     case GTK_IMAGE_EMPTY:
6632     case GTK_IMAGE_PIXBUF:
6633       break;
6634     case GTK_IMAGE_STOCK:
6635       state = gtk_widget_get_state_flags (widget);
6636       gtk_widget_set_state_flags (widget, 0, TRUE);
6637       icon_info->pixbuf = gtk_widget_render_icon (widget,
6638                                                   icon_info->stock_id,
6639                                                   GTK_ICON_SIZE_MENU,
6640                                                   NULL);
6641       if (!icon_info->pixbuf)
6642         icon_info->pixbuf = gtk_widget_render_icon (widget,
6643                                                     GTK_STOCK_MISSING_IMAGE,
6644                                                     GTK_ICON_SIZE_MENU,
6645                                                     NULL);
6646       gtk_widget_set_state_flags (widget, state, TRUE);
6647       break;
6648
6649     case GTK_IMAGE_ICON_NAME:
6650       screen = gtk_widget_get_screen (widget);
6651       if (screen)
6652         {
6653           icon_theme = gtk_icon_theme_get_for_screen (screen);
6654           settings = gtk_settings_get_for_screen (screen);
6655           
6656           gtk_icon_size_lookup_for_settings (settings,
6657                                              GTK_ICON_SIZE_MENU,
6658                                              &width, &height);
6659
6660           icon_info->pixbuf = gtk_icon_theme_load_icon (icon_theme,
6661                                                         icon_info->icon_name,
6662                                                         MIN (width, height),
6663                                                         0, NULL);
6664
6665           if (icon_info->pixbuf == NULL)
6666             {
6667               state = gtk_widget_get_state_flags (widget);
6668               gtk_widget_set_state_flags (widget, 0, TRUE);
6669               icon_info->pixbuf = gtk_widget_render_icon (widget,
6670                                                           GTK_STOCK_MISSING_IMAGE,
6671                                                           GTK_ICON_SIZE_MENU,
6672                                                           NULL);
6673               gtk_widget_set_state_flags (widget, state, TRUE);
6674             }
6675         }
6676       break;
6677
6678     case GTK_IMAGE_GICON:
6679       screen = gtk_widget_get_screen (widget);
6680       if (screen)
6681         {
6682           icon_theme = gtk_icon_theme_get_for_screen (screen);
6683           settings = gtk_settings_get_for_screen (screen);
6684
6685           gtk_icon_size_lookup_for_settings (settings,
6686                                              GTK_ICON_SIZE_MENU,
6687                                              &width, &height);
6688
6689           info = gtk_icon_theme_lookup_by_gicon (icon_theme,
6690                                                  icon_info->gicon,
6691                                                  MIN (width, height), 
6692                                                  GTK_ICON_LOOKUP_USE_BUILTIN);
6693           if (info)
6694             {
6695               icon_info->pixbuf = gtk_icon_info_load_icon (info, NULL);
6696               gtk_icon_info_free (info);
6697             }
6698
6699           if (icon_info->pixbuf == NULL)
6700             {
6701               state = gtk_widget_get_state_flags (widget);
6702               gtk_widget_set_state_flags (widget, 0, TRUE);
6703               icon_info->pixbuf = gtk_widget_render_icon (widget,
6704                                                           GTK_STOCK_MISSING_IMAGE,
6705                                                           GTK_ICON_SIZE_MENU,
6706                                                           NULL);
6707               gtk_widget_set_state_flags (widget, state, TRUE);
6708             }
6709         }
6710       break;
6711
6712     default:
6713       g_assert_not_reached ();
6714       break;
6715     }
6716     
6717   if (icon_info->pixbuf != NULL && icon_info->window != NULL)
6718     gdk_window_show_unraised (icon_info->window);
6719 }
6720
6721
6722 /* Public API
6723  */
6724
6725 /**
6726  * gtk_entry_new:
6727  *
6728  * Creates a new entry.
6729  *
6730  * Return value: a new #GtkEntry.
6731  */
6732 GtkWidget*
6733 gtk_entry_new (void)
6734 {
6735   return g_object_new (GTK_TYPE_ENTRY, NULL);
6736 }
6737
6738 /**
6739  * gtk_entry_new_with_buffer:
6740  * @buffer: The buffer to use for the new #GtkEntry.
6741  *
6742  * Creates a new entry with the specified text buffer.
6743  *
6744  * Return value: a new #GtkEntry
6745  *
6746  * Since: 2.18
6747  */
6748 GtkWidget*
6749 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6750 {
6751   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6752   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6753 }
6754
6755 static GtkEntryBuffer*
6756 get_buffer (GtkEntry *entry)
6757 {
6758   GtkEntryPrivate *priv = entry->priv;
6759
6760   if (priv->buffer == NULL)
6761     {
6762       GtkEntryBuffer *buffer;
6763       buffer = gtk_entry_buffer_new (NULL, 0);
6764       gtk_entry_set_buffer (entry, buffer);
6765       g_object_unref (buffer);
6766     }
6767
6768   return priv->buffer;
6769 }
6770
6771 /**
6772  * gtk_entry_get_buffer:
6773  * @entry: a #GtkEntry
6774  *
6775  * Get the #GtkEntryBuffer object which holds the text for
6776  * this widget.
6777  *
6778  * Since: 2.18
6779  *
6780  * Returns: (transfer none): A #GtkEntryBuffer object.
6781  */
6782 GtkEntryBuffer*
6783 gtk_entry_get_buffer (GtkEntry *entry)
6784 {
6785   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6786
6787   return get_buffer (entry);
6788 }
6789
6790 /**
6791  * gtk_entry_set_buffer:
6792  * @entry: a #GtkEntry
6793  * @buffer: a #GtkEntryBuffer
6794  *
6795  * Set the #GtkEntryBuffer object which holds the text for
6796  * this widget.
6797  *
6798  * Since: 2.18
6799  */
6800 void
6801 gtk_entry_set_buffer (GtkEntry       *entry,
6802                       GtkEntryBuffer *buffer)
6803 {
6804   GtkEntryPrivate *priv;
6805   GObject *obj;
6806
6807   g_return_if_fail (GTK_IS_ENTRY (entry));
6808
6809   priv = entry->priv;
6810
6811   if (buffer)
6812     {
6813       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6814       g_object_ref (buffer);
6815     }
6816
6817   if (priv->buffer)
6818     {
6819       buffer_disconnect_signals (entry);
6820       g_object_unref (priv->buffer);
6821     }
6822
6823   priv->buffer = buffer;
6824
6825   if (priv->buffer)
6826      buffer_connect_signals (entry);
6827
6828   obj = G_OBJECT (entry);
6829   g_object_freeze_notify (obj);
6830   g_object_notify (obj, "buffer");
6831   g_object_notify (obj, "text");
6832   g_object_notify (obj, "text-length");
6833   g_object_notify (obj, "max-length");
6834   g_object_notify (obj, "visibility");
6835   g_object_notify (obj, "invisible-char");
6836   g_object_notify (obj, "invisible-char-set");
6837   g_object_thaw_notify (obj);
6838
6839   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6840   gtk_entry_recompute (entry);
6841 }
6842
6843 /**
6844  * gtk_entry_get_text_area:
6845  * @entry: a #GtkEntry
6846  * @text_area: Return location for the text area.
6847  *
6848  * Gets the area where the entry's text is drawn. This function is
6849  * useful when drawing something to the entry in a draw callback.
6850  *
6851  * If the entry is not realized, @text_area is filled with zeros.
6852  *
6853  * See also gtk_entry_get_icon_area().
6854  *
6855  * Since: 3.0
6856  **/
6857 void
6858 gtk_entry_get_text_area (GtkEntry     *entry,
6859                          GdkRectangle *text_area)
6860 {
6861   GtkEntryPrivate *priv;
6862
6863   g_return_if_fail (GTK_IS_ENTRY (entry));
6864   g_return_if_fail (text_area != NULL);
6865
6866   priv = entry->priv;
6867
6868   if (priv->text_area)
6869     {
6870       GtkAllocation allocation;
6871       gint x, y;
6872
6873       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6874       gdk_window_get_position (priv->text_area, &x, &y);
6875
6876       text_area->x = x - allocation.x;
6877       text_area->y = y - allocation.y;
6878       text_area->width = gdk_window_get_width (priv->text_area);
6879       text_area->height = gdk_window_get_height (priv->text_area);
6880     }
6881   else
6882     {
6883       text_area->x = 0;
6884       text_area->y = 0;
6885       text_area->width = 0;
6886       text_area->height = 0;
6887     }
6888 }
6889
6890 /**
6891  * gtk_entry_set_text:
6892  * @entry: a #GtkEntry
6893  * @text: the new text
6894  *
6895  * Sets the text in the widget to the given
6896  * value, replacing the current contents.
6897  *
6898  * See gtk_entry_buffer_set_text().
6899  */
6900 void
6901 gtk_entry_set_text (GtkEntry    *entry,
6902                     const gchar *text)
6903 {
6904   gint tmp_pos;
6905   GtkEntryCompletion *completion;
6906
6907   g_return_if_fail (GTK_IS_ENTRY (entry));
6908   g_return_if_fail (text != NULL);
6909
6910   /* Actually setting the text will affect the cursor and selection;
6911    * if the contents don't actually change, this will look odd to the user.
6912    */
6913   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6914     return;
6915
6916   completion = gtk_entry_get_completion (entry);
6917   if (completion && completion->priv->changed_id > 0)
6918     g_signal_handler_block (entry, completion->priv->changed_id);
6919
6920   begin_change (entry);
6921   g_object_freeze_notify (G_OBJECT (entry));
6922   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6923   tmp_pos = 0;
6924   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6925   g_object_thaw_notify (G_OBJECT (entry));
6926   end_change (entry);
6927
6928   if (completion && completion->priv->changed_id > 0)
6929     g_signal_handler_unblock (entry, completion->priv->changed_id);
6930 }
6931
6932 /**
6933  * gtk_entry_set_visibility:
6934  * @entry: a #GtkEntry
6935  * @visible: %TRUE if the contents of the entry are displayed
6936  *           as plaintext
6937  *
6938  * Sets whether the contents of the entry are visible or not. 
6939  * When visibility is set to %FALSE, characters are displayed 
6940  * as the invisible char, and will also appear that way when 
6941  * the text in the entry widget is copied elsewhere.
6942  *
6943  * By default, GTK+ picks the best invisible character available
6944  * in the current font, but it can be changed with
6945  * gtk_entry_set_invisible_char().
6946  */
6947 void
6948 gtk_entry_set_visibility (GtkEntry *entry,
6949                           gboolean visible)
6950 {
6951   GtkEntryPrivate *priv;
6952
6953   g_return_if_fail (GTK_IS_ENTRY (entry));
6954
6955   priv = entry->priv;
6956
6957   visible = visible != FALSE;
6958
6959   if (priv->visible != visible)
6960     {
6961       priv->visible = visible;
6962
6963       g_object_notify (G_OBJECT (entry), "visibility");
6964       gtk_entry_recompute (entry);
6965     }
6966 }
6967
6968 /**
6969  * gtk_entry_get_visibility:
6970  * @entry: a #GtkEntry
6971  *
6972  * Retrieves whether the text in @entry is visible. See
6973  * gtk_entry_set_visibility().
6974  *
6975  * Return value: %TRUE if the text is currently visible
6976  **/
6977 gboolean
6978 gtk_entry_get_visibility (GtkEntry *entry)
6979 {
6980   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6981
6982   return entry->priv->visible;
6983 }
6984
6985 /**
6986  * gtk_entry_set_invisible_char:
6987  * @entry: a #GtkEntry
6988  * @ch: a Unicode character
6989  * 
6990  * Sets the character to use in place of the actual text when
6991  * gtk_entry_set_visibility() has been called to set text visibility
6992  * to %FALSE. i.e. this is the character used in "password mode" to
6993  * show the user how many characters have been typed. By default, GTK+
6994  * picks the best invisible char available in the current font. If you
6995  * set the invisible char to 0, then the user will get no feedback
6996  * at all; there will be no text on the screen as they type.
6997  **/
6998 void
6999 gtk_entry_set_invisible_char (GtkEntry *entry,
7000                               gunichar  ch)
7001 {
7002   GtkEntryPrivate *priv;
7003
7004   g_return_if_fail (GTK_IS_ENTRY (entry));
7005
7006   priv = entry->priv;
7007
7008   if (!priv->invisible_char_set)
7009     {
7010       priv->invisible_char_set = TRUE;
7011       g_object_notify (G_OBJECT (entry), "invisible-char-set");
7012     }
7013
7014   if (ch == priv->invisible_char)
7015     return;
7016
7017   priv->invisible_char = ch;
7018   g_object_notify (G_OBJECT (entry), "invisible-char");
7019   gtk_entry_recompute (entry);  
7020 }
7021
7022 /**
7023  * gtk_entry_get_invisible_char:
7024  * @entry: a #GtkEntry
7025  *
7026  * Retrieves the character displayed in place of the real characters
7027  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
7028  *
7029  * Return value: the current invisible char, or 0, if the entry does not
7030  *               show invisible text at all. 
7031  **/
7032 gunichar
7033 gtk_entry_get_invisible_char (GtkEntry *entry)
7034 {
7035   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7036
7037   return entry->priv->invisible_char;
7038 }
7039
7040 /**
7041  * gtk_entry_unset_invisible_char:
7042  * @entry: a #GtkEntry
7043  *
7044  * Unsets the invisible char previously set with
7045  * gtk_entry_set_invisible_char(). So that the
7046  * default invisible char is used again.
7047  *
7048  * Since: 2.16
7049  **/
7050 void
7051 gtk_entry_unset_invisible_char (GtkEntry *entry)
7052 {
7053   GtkEntryPrivate *priv;
7054   gunichar ch;
7055
7056   g_return_if_fail (GTK_IS_ENTRY (entry));
7057
7058   priv = entry->priv;
7059
7060   if (!priv->invisible_char_set)
7061     return;
7062
7063   priv->invisible_char_set = FALSE;
7064   ch = find_invisible_char (GTK_WIDGET (entry));
7065
7066   if (priv->invisible_char != ch)
7067     {
7068       priv->invisible_char = ch;
7069       g_object_notify (G_OBJECT (entry), "invisible-char");
7070     }
7071
7072   g_object_notify (G_OBJECT (entry), "invisible-char-set");
7073   gtk_entry_recompute (entry);
7074 }
7075
7076 /**
7077  * gtk_entry_set_overwrite_mode:
7078  * @entry: a #GtkEntry
7079  * @overwrite: new value
7080  * 
7081  * Sets whether the text is overwritten when typing in the #GtkEntry.
7082  *
7083  * Since: 2.14
7084  **/
7085 void
7086 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7087                               gboolean  overwrite)
7088 {
7089   GtkEntryPrivate *priv = entry->priv;
7090
7091   g_return_if_fail (GTK_IS_ENTRY (entry));
7092
7093   if (priv->overwrite_mode == overwrite)
7094     return;
7095   
7096   gtk_entry_toggle_overwrite (entry);
7097
7098   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7099 }
7100
7101 /**
7102  * gtk_entry_get_overwrite_mode:
7103  * @entry: a #GtkEntry
7104  * 
7105  * Gets the value set by gtk_entry_set_overwrite_mode().
7106  * 
7107  * Return value: whether the text is overwritten when typing.
7108  *
7109  * Since: 2.14
7110  **/
7111 gboolean
7112 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7113 {
7114   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7115
7116   return entry->priv->overwrite_mode;
7117 }
7118
7119 /**
7120  * gtk_entry_get_text:
7121  * @entry: a #GtkEntry
7122  *
7123  * Retrieves the contents of the entry widget.
7124  * See also gtk_editable_get_chars().
7125  *
7126  * This is equivalent to:
7127  *
7128  * <informalexample><programlisting>
7129  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7130  * </programlisting></informalexample>
7131  *
7132  * Return value: a pointer to the contents of the widget as a
7133  *      string. This string points to internally allocated
7134  *      storage in the widget and must not be freed, modified or
7135  *      stored.
7136  **/
7137 G_CONST_RETURN gchar*
7138 gtk_entry_get_text (GtkEntry *entry)
7139 {
7140   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7141
7142   return gtk_entry_buffer_get_text (get_buffer (entry));
7143 }
7144
7145 /**
7146  * gtk_entry_set_max_length:
7147  * @entry: a #GtkEntry
7148  * @max: the maximum length of the entry, or 0 for no maximum.
7149  *   (other than the maximum length of entries.) The value passed in will
7150  *   be clamped to the range 0-65536.
7151  * 
7152  * Sets the maximum allowed length of the contents of the widget. If
7153  * the current contents are longer than the given length, then they
7154  * will be truncated to fit.
7155  *
7156  * This is equivalent to:
7157  *
7158  * <informalexample><programlisting>
7159  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7160  * </programlisting></informalexample>
7161  **/
7162 void
7163 gtk_entry_set_max_length (GtkEntry     *entry,
7164                           gint          max)
7165 {
7166   g_return_if_fail (GTK_IS_ENTRY (entry));
7167   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7168 }
7169
7170 /**
7171  * gtk_entry_get_max_length:
7172  * @entry: a #GtkEntry
7173  *
7174  * Retrieves the maximum allowed length of the text in
7175  * @entry. See gtk_entry_set_max_length().
7176  *
7177  * This is equivalent to:
7178  *
7179  * <informalexample><programlisting>
7180  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7181  * </programlisting></informalexample>
7182  *
7183  * Return value: the maximum allowed number of characters
7184  *               in #GtkEntry, or 0 if there is no maximum.
7185  **/
7186 gint
7187 gtk_entry_get_max_length (GtkEntry *entry)
7188 {
7189   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7190
7191   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7192 }
7193
7194 /**
7195  * gtk_entry_get_text_length:
7196  * @entry: a #GtkEntry
7197  *
7198  * Retrieves the current length of the text in
7199  * @entry. 
7200  *
7201  * This is equivalent to:
7202  *
7203  * <informalexample><programlisting>
7204  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7205  * </programlisting></informalexample>
7206  *
7207  * Return value: the current number of characters
7208  *               in #GtkEntry, or 0 if there are none.
7209  *
7210  * Since: 2.14
7211  **/
7212 guint16
7213 gtk_entry_get_text_length (GtkEntry *entry)
7214 {
7215   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7216
7217   return gtk_entry_buffer_get_length (get_buffer (entry));
7218 }
7219
7220 /**
7221  * gtk_entry_set_activates_default:
7222  * @entry: a #GtkEntry
7223  * @setting: %TRUE to activate window's default widget on Enter keypress
7224  *
7225  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7226  * widget for the window containing the entry. This usually means that
7227  * the dialog box containing the entry will be closed, since the default
7228  * widget is usually one of the dialog buttons.
7229  *
7230  * (For experts: if @setting is %TRUE, the entry calls
7231  * gtk_window_activate_default() on the window containing the entry, in
7232  * the default handler for the #GtkWidget::activate signal.)
7233  **/
7234 void
7235 gtk_entry_set_activates_default (GtkEntry *entry,
7236                                  gboolean  setting)
7237 {
7238   GtkEntryPrivate *priv;
7239
7240   g_return_if_fail (GTK_IS_ENTRY (entry));
7241
7242   priv = entry->priv;
7243
7244   setting = setting != FALSE;
7245
7246   if (setting != priv->activates_default)
7247     {
7248       priv->activates_default = setting;
7249       g_object_notify (G_OBJECT (entry), "activates-default");
7250     }
7251 }
7252
7253 /**
7254  * gtk_entry_get_activates_default:
7255  * @entry: a #GtkEntry
7256  * 
7257  * Retrieves the value set by gtk_entry_set_activates_default().
7258  * 
7259  * Return value: %TRUE if the entry will activate the default widget
7260  **/
7261 gboolean
7262 gtk_entry_get_activates_default (GtkEntry *entry)
7263 {
7264   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7265
7266   return entry->priv->activates_default;
7267 }
7268
7269 /**
7270  * gtk_entry_set_width_chars:
7271  * @entry: a #GtkEntry
7272  * @n_chars: width in chars
7273  *
7274  * Changes the size request of the entry to be about the right size
7275  * for @n_chars characters. Note that it changes the size
7276  * <emphasis>request</emphasis>, the size can still be affected by
7277  * how you pack the widget into containers. If @n_chars is -1, the
7278  * size reverts to the default entry size.
7279  **/
7280 void
7281 gtk_entry_set_width_chars (GtkEntry *entry,
7282                            gint      n_chars)
7283 {
7284   GtkEntryPrivate *priv;
7285
7286   g_return_if_fail (GTK_IS_ENTRY (entry));
7287
7288   priv = entry->priv;
7289
7290   if (priv->width_chars != n_chars)
7291     {
7292       priv->width_chars = n_chars;
7293       g_object_notify (G_OBJECT (entry), "width-chars");
7294       gtk_widget_queue_resize (GTK_WIDGET (entry));
7295     }
7296 }
7297
7298 /**
7299  * gtk_entry_get_width_chars:
7300  * @entry: a #GtkEntry
7301  * 
7302  * Gets the value set by gtk_entry_set_width_chars().
7303  * 
7304  * Return value: number of chars to request space for, or negative if unset
7305  **/
7306 gint
7307 gtk_entry_get_width_chars (GtkEntry *entry)
7308 {
7309   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7310
7311   return entry->priv->width_chars;
7312 }
7313
7314 /**
7315  * gtk_entry_set_has_frame:
7316  * @entry: a #GtkEntry
7317  * @setting: new value
7318  * 
7319  * Sets whether the entry has a beveled frame around it.
7320  **/
7321 void
7322 gtk_entry_set_has_frame (GtkEntry *entry,
7323                          gboolean  setting)
7324 {
7325   GtkEntryPrivate *priv;
7326
7327   g_return_if_fail (GTK_IS_ENTRY (entry));
7328
7329   priv = entry->priv;
7330
7331   setting = (setting != FALSE);
7332
7333   if (priv->has_frame == setting)
7334     return;
7335
7336   gtk_widget_queue_resize (GTK_WIDGET (entry));
7337   priv->has_frame = setting;
7338   g_object_notify (G_OBJECT (entry), "has-frame");
7339 }
7340
7341 /**
7342  * gtk_entry_get_has_frame:
7343  * @entry: a #GtkEntry
7344  * 
7345  * Gets the value set by gtk_entry_set_has_frame().
7346  * 
7347  * Return value: whether the entry has a beveled frame
7348  **/
7349 gboolean
7350 gtk_entry_get_has_frame (GtkEntry *entry)
7351 {
7352   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7353
7354   return entry->priv->has_frame;
7355 }
7356
7357 /**
7358  * gtk_entry_set_inner_border:
7359  * @entry: a #GtkEntry
7360  * @border: (allow-none): a #GtkBorder, or %NULL
7361  *
7362  * Sets %entry's inner-border property to %border, or clears it if %NULL
7363  * is passed. The inner-border is the area around the entry's text, but
7364  * inside its frame.
7365  *
7366  * If set, this property overrides the inner-border style property.
7367  * Overriding the style-provided border is useful when you want to do
7368  * in-place editing of some text in a canvas or list widget, where
7369  * pixel-exact positioning of the entry is important.
7370  *
7371  * Since: 2.10
7372  **/
7373 void
7374 gtk_entry_set_inner_border (GtkEntry        *entry,
7375                             const GtkBorder *border)
7376 {
7377   g_return_if_fail (GTK_IS_ENTRY (entry));
7378
7379   gtk_widget_queue_resize (GTK_WIDGET (entry));
7380
7381   if (border)
7382     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7383                              gtk_border_copy (border),
7384                              (GDestroyNotify) gtk_border_free);
7385   else
7386     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7387
7388   g_object_notify (G_OBJECT (entry), "inner-border");
7389 }
7390
7391 /**
7392  * gtk_entry_get_inner_border:
7393  * @entry: a #GtkEntry
7394  *
7395  * This function returns the entry's #GtkEntry:inner-border property. See
7396  * gtk_entry_set_inner_border() for more information.
7397  *
7398  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7399  *
7400  * Since: 2.10
7401  **/
7402 G_CONST_RETURN GtkBorder *
7403 gtk_entry_get_inner_border (GtkEntry *entry)
7404 {
7405   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7406
7407   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7408 }
7409
7410 /**
7411  * gtk_entry_get_layout:
7412  * @entry: a #GtkEntry
7413  * 
7414  * Gets the #PangoLayout used to display the entry.
7415  * The layout is useful to e.g. convert text positions to
7416  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7417  * The returned layout is owned by the entry and must not be 
7418  * modified or freed by the caller.
7419  *
7420  * Keep in mind that the layout text may contain a preedit string, so
7421  * gtk_entry_layout_index_to_text_index() and
7422  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7423  * indices in the layout to byte indices in the entry contents.
7424  *
7425  * Return value: (transfer none): the #PangoLayout for this entry
7426  **/
7427 PangoLayout*
7428 gtk_entry_get_layout (GtkEntry *entry)
7429 {
7430   PangoLayout *layout;
7431   
7432   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7433
7434   layout = gtk_entry_ensure_layout (entry, TRUE);
7435
7436   return layout;
7437 }
7438
7439
7440 /**
7441  * gtk_entry_layout_index_to_text_index:
7442  * @entry: a #GtkEntry
7443  * @layout_index: byte index into the entry layout text
7444  * 
7445  * Converts from a position in the entry contents (returned
7446  * by gtk_entry_get_text()) to a position in the
7447  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7448  * with text retrieved via pango_layout_get_text()).
7449  * 
7450  * Return value: byte index into the entry contents
7451  **/
7452 gint
7453 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7454                                       gint      layout_index)
7455 {
7456   GtkEntryPrivate *priv;
7457   PangoLayout *layout;
7458   const gchar *text;
7459   gint cursor_index;
7460   
7461   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7462
7463   priv = entry->priv;
7464
7465   layout = gtk_entry_ensure_layout (entry, TRUE);
7466   text = pango_layout_get_text (layout);
7467   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7468
7469   if (layout_index >= cursor_index && priv->preedit_length)
7470     {
7471       if (layout_index >= cursor_index + priv->preedit_length)
7472         layout_index -= priv->preedit_length;
7473       else
7474         layout_index = cursor_index;
7475     }
7476
7477   return layout_index;
7478 }
7479
7480 /**
7481  * gtk_entry_text_index_to_layout_index:
7482  * @entry: a #GtkEntry
7483  * @text_index: byte index into the entry contents
7484  * 
7485  * Converts from a position in the entry's #PangoLayout (returned by
7486  * gtk_entry_get_layout()) to a position in the entry contents
7487  * (returned by gtk_entry_get_text()).
7488  * 
7489  * Return value: byte index into the entry layout text
7490  **/
7491 gint
7492 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7493                                       gint      text_index)
7494 {
7495   GtkEntryPrivate *priv;
7496   PangoLayout *layout;
7497   const gchar *text;
7498   gint cursor_index;
7499
7500   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7501
7502   priv = entry->priv;
7503
7504   layout = gtk_entry_ensure_layout (entry, TRUE);
7505   text = pango_layout_get_text (layout);
7506   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7507
7508   if (text_index > cursor_index)
7509     text_index += priv->preedit_length;
7510
7511   return text_index;
7512 }
7513
7514 /**
7515  * gtk_entry_get_layout_offsets:
7516  * @entry: a #GtkEntry
7517  * @x: (allow-none): location to store X offset of layout, or %NULL
7518  * @y: (allow-none): location to store Y offset of layout, or %NULL
7519  *
7520  *
7521  * Obtains the position of the #PangoLayout used to render text
7522  * in the entry, in widget coordinates. Useful if you want to line
7523  * up the text in an entry with some other text, e.g. when using the
7524  * entry to implement editable cells in a sheet widget.
7525  *
7526  * Also useful to convert mouse events into coordinates inside the
7527  * #PangoLayout, e.g. to take some action if some part of the entry text
7528  * is clicked.
7529  * 
7530  * Note that as the user scrolls around in the entry the offsets will
7531  * change; you'll need to connect to the "notify::scroll-offset"
7532  * signal to track this. Remember when using the #PangoLayout
7533  * functions you need to convert to and from pixels using
7534  * PANGO_PIXELS() or #PANGO_SCALE.
7535  *
7536  * Keep in mind that the layout text may contain a preedit string, so
7537  * gtk_entry_layout_index_to_text_index() and
7538  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7539  * indices in the layout to byte indices in the entry contents.
7540  **/
7541 void
7542 gtk_entry_get_layout_offsets (GtkEntry *entry,
7543                               gint     *x,
7544                               gint     *y)
7545 {
7546   gint text_area_x, text_area_y;
7547   
7548   g_return_if_fail (GTK_IS_ENTRY (entry));
7549
7550   /* this gets coords relative to text area */
7551   get_layout_position (entry, x, y);
7552
7553   /* convert to widget coords */
7554   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7555   
7556   if (x)
7557     *x += text_area_x;
7558
7559   if (y)
7560     *y += text_area_y;
7561 }
7562
7563
7564 /**
7565  * gtk_entry_set_alignment:
7566  * @entry: a #GtkEntry
7567  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7568  *          Reversed for RTL layouts
7569  * 
7570  * Sets the alignment for the contents of the entry. This controls
7571  * the horizontal positioning of the contents when the displayed
7572  * text is shorter than the width of the entry.
7573  *
7574  * Since: 2.4
7575  **/
7576 void
7577 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7578 {
7579   GtkEntryPrivate *priv;
7580
7581   g_return_if_fail (GTK_IS_ENTRY (entry));
7582
7583   priv = entry->priv;
7584
7585   if (xalign < 0.0)
7586     xalign = 0.0;
7587   else if (xalign > 1.0)
7588     xalign = 1.0;
7589
7590   if (xalign != priv->xalign)
7591     {
7592       priv->xalign = xalign;
7593
7594       gtk_entry_recompute (entry);
7595
7596       g_object_notify (G_OBJECT (entry), "xalign");
7597     }
7598 }
7599
7600 /**
7601  * gtk_entry_get_alignment:
7602  * @entry: a #GtkEntry
7603  * 
7604  * Gets the value set by gtk_entry_set_alignment().
7605  * 
7606  * Return value: the alignment
7607  *
7608  * Since: 2.4
7609  **/
7610 gfloat
7611 gtk_entry_get_alignment (GtkEntry *entry)
7612 {
7613   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7614
7615   return entry->priv->xalign;
7616 }
7617
7618 /**
7619  * gtk_entry_set_icon_from_pixbuf:
7620  * @entry: a #GtkEntry
7621  * @icon_pos: Icon position
7622  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7623  *
7624  * Sets the icon shown in the specified position using a pixbuf.
7625  *
7626  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7627  *
7628  * Since: 2.16
7629  */
7630 void
7631 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7632                                 GtkEntryIconPosition  icon_pos,
7633                                 GdkPixbuf            *pixbuf)
7634 {
7635   GtkEntryPrivate *priv;
7636   EntryIconInfo *icon_info;
7637
7638   g_return_if_fail (GTK_IS_ENTRY (entry));
7639   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7640
7641   priv = entry->priv;
7642
7643   if ((icon_info = priv->icons[icon_pos]) == NULL)
7644     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7645
7646   g_object_freeze_notify (G_OBJECT (entry));
7647
7648   if (pixbuf)
7649     g_object_ref (pixbuf);
7650
7651   gtk_entry_clear (entry, icon_pos);
7652
7653   if (pixbuf)
7654     {
7655       icon_info->storage_type = GTK_IMAGE_PIXBUF;
7656       icon_info->pixbuf = pixbuf;
7657
7658       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7659         {
7660           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7661           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7662         }
7663       else
7664         {
7665           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7666           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7667         }
7668
7669       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7670           gdk_window_show_unraised (icon_info->window);
7671     }
7672
7673   gtk_entry_ensure_pixbuf (entry, icon_pos);
7674   
7675   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7676     gtk_widget_queue_resize (GTK_WIDGET (entry));
7677
7678   g_object_thaw_notify (G_OBJECT (entry));
7679 }
7680
7681 /**
7682  * gtk_entry_set_icon_from_stock:
7683  * @entry: A #GtkEntry
7684  * @icon_pos: Icon position
7685  * @stock_id: (allow-none): The name of the stock item, or %NULL
7686  *
7687  * Sets the icon shown in the entry at the specified position from
7688  * a stock image.
7689  *
7690  * If @stock_id is %NULL, no icon will be shown in the specified position.
7691  *
7692  * Since: 2.16
7693  */
7694 void
7695 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7696                                GtkEntryIconPosition  icon_pos,
7697                                const gchar          *stock_id)
7698 {
7699   GtkEntryPrivate *priv;
7700   EntryIconInfo *icon_info;
7701   gchar *new_id;
7702
7703   g_return_if_fail (GTK_IS_ENTRY (entry));
7704   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7705
7706   priv = entry->priv;
7707
7708   if ((icon_info = priv->icons[icon_pos]) == NULL)
7709     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7710
7711   g_object_freeze_notify (G_OBJECT (entry));
7712
7713   gtk_widget_ensure_style (GTK_WIDGET (entry));
7714
7715   /* need to dup before clearing */
7716   new_id = g_strdup (stock_id);
7717
7718   gtk_entry_clear (entry, icon_pos);
7719
7720   if (new_id != NULL)
7721     {
7722       icon_info->storage_type = GTK_IMAGE_STOCK;
7723       icon_info->stock_id = new_id;
7724
7725       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7726         {
7727           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7728           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7729         }
7730       else
7731         {
7732           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7733           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7734         }
7735
7736       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7737           gdk_window_show_unraised (icon_info->window);
7738     }
7739
7740   gtk_entry_ensure_pixbuf (entry, icon_pos);
7741
7742   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7743     gtk_widget_queue_resize (GTK_WIDGET (entry));
7744
7745   g_object_thaw_notify (G_OBJECT (entry));
7746 }
7747
7748 /**
7749  * gtk_entry_set_icon_from_icon_name:
7750  * @entry: A #GtkEntry
7751  * @icon_pos: The position at which to set the icon
7752  * @icon_name: (allow-none): An icon name, or %NULL
7753  *
7754  * Sets the icon shown in the entry at the specified position
7755  * from the current icon theme.
7756  *
7757  * If the icon name isn't known, a "broken image" icon will be displayed
7758  * instead.
7759  *
7760  * If @icon_name is %NULL, no icon will be shown in the specified position.
7761  *
7762  * Since: 2.16
7763  */
7764 void
7765 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7766                                    GtkEntryIconPosition  icon_pos,
7767                                    const gchar          *icon_name)
7768 {
7769   GtkEntryPrivate *priv;
7770   EntryIconInfo *icon_info;
7771   gchar *new_name;
7772
7773   g_return_if_fail (GTK_IS_ENTRY (entry));
7774   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7775
7776   priv = entry->priv;
7777
7778   if ((icon_info = priv->icons[icon_pos]) == NULL)
7779     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7780
7781   g_object_freeze_notify (G_OBJECT (entry));
7782
7783   gtk_widget_ensure_style (GTK_WIDGET (entry));
7784
7785   /* need to dup before clearing */
7786   new_name = g_strdup (icon_name);
7787
7788   gtk_entry_clear (entry, icon_pos);
7789
7790   if (new_name != NULL)
7791     {
7792       icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7793       icon_info->icon_name = new_name;
7794
7795       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7796         {
7797           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7798           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7799         }
7800       else
7801         {
7802           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7803           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7804         }
7805
7806       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7807           gdk_window_show_unraised (icon_info->window);
7808     }
7809
7810   gtk_entry_ensure_pixbuf (entry, icon_pos);
7811
7812   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7813     gtk_widget_queue_resize (GTK_WIDGET (entry));
7814
7815   g_object_thaw_notify (G_OBJECT (entry));
7816 }
7817
7818 /**
7819  * gtk_entry_set_icon_from_gicon:
7820  * @entry: A #GtkEntry
7821  * @icon_pos: The position at which to set the icon
7822  * @icon: (allow-none): The icon to set, or %NULL
7823  *
7824  * Sets the icon shown in the entry at the specified position
7825  * from the current icon theme.
7826  * If the icon isn't known, a "broken image" icon will be displayed
7827  * instead.
7828  *
7829  * If @icon is %NULL, no icon will be shown in the specified position.
7830  *
7831  * Since: 2.16
7832  */
7833 void
7834 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7835                                GtkEntryIconPosition  icon_pos,
7836                                GIcon                *icon)
7837 {
7838   GtkEntryPrivate *priv;
7839   EntryIconInfo *icon_info;
7840
7841   g_return_if_fail (GTK_IS_ENTRY (entry));
7842   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7843
7844   priv = entry->priv;
7845
7846   if ((icon_info = priv->icons[icon_pos]) == NULL)
7847     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7848
7849   g_object_freeze_notify (G_OBJECT (entry));
7850
7851   /* need to ref before clearing */
7852   if (icon)
7853     g_object_ref (icon);
7854
7855   gtk_entry_clear (entry, icon_pos);
7856
7857   if (icon)
7858     {
7859       icon_info->storage_type = GTK_IMAGE_GICON;
7860       icon_info->gicon = icon;
7861
7862       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7863         {
7864           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7865           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7866         }
7867       else
7868         {
7869           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7870           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7871         }
7872
7873       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7874           gdk_window_show_unraised (icon_info->window);
7875     }
7876
7877   gtk_entry_ensure_pixbuf (entry, icon_pos);
7878
7879   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7880     gtk_widget_queue_resize (GTK_WIDGET (entry));
7881
7882   g_object_thaw_notify (G_OBJECT (entry));
7883 }
7884
7885 /**
7886  * gtk_entry_set_icon_activatable:
7887  * @entry: A #GtkEntry
7888  * @icon_pos: Icon position
7889  * @activatable: %TRUE if the icon should be activatable
7890  *
7891  * Sets whether the icon is activatable.
7892  *
7893  * Since: 2.16
7894  */
7895 void
7896 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7897                                 GtkEntryIconPosition  icon_pos,
7898                                 gboolean              activatable)
7899 {
7900   GtkEntryPrivate *priv;
7901   EntryIconInfo *icon_info;
7902
7903   g_return_if_fail (GTK_IS_ENTRY (entry));
7904   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7905
7906   priv = entry->priv;
7907
7908   if ((icon_info = priv->icons[icon_pos]) == NULL)
7909     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7910
7911   activatable = activatable != FALSE;
7912
7913   if (icon_info->nonactivatable != !activatable)
7914     {
7915       icon_info->nonactivatable = !activatable;
7916
7917       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7918         update_cursors (GTK_WIDGET (entry));
7919
7920       g_object_notify (G_OBJECT (entry),
7921                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7922     }
7923 }
7924
7925 /**
7926  * gtk_entry_get_icon_activatable:
7927  * @entry: a #GtkEntry
7928  * @icon_pos: Icon position
7929  *
7930  * Returns whether the icon is activatable.
7931  *
7932  * Returns: %TRUE if the icon is activatable.
7933  *
7934  * Since: 2.16
7935  */
7936 gboolean
7937 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7938                                 GtkEntryIconPosition  icon_pos)
7939 {
7940   GtkEntryPrivate *priv;
7941   EntryIconInfo *icon_info;
7942
7943   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7944   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7945
7946   priv = entry->priv;
7947   icon_info = priv->icons[icon_pos];
7948
7949   return (icon_info != NULL && !icon_info->nonactivatable);
7950 }
7951
7952 /**
7953  * gtk_entry_get_icon_pixbuf:
7954  * @entry: A #GtkEntry
7955  * @icon_pos: Icon position
7956  *
7957  * Retrieves the image used for the icon.
7958  *
7959  * Unlike the other methods of setting and getting icon data, this
7960  * method will work regardless of whether the icon was set using a
7961  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7962  *
7963  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7964  *     set for this position.
7965  *
7966  * Since: 2.16
7967  */
7968 GdkPixbuf *
7969 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7970                            GtkEntryIconPosition  icon_pos)
7971 {
7972   GtkEntryPrivate *priv;
7973   EntryIconInfo *icon_info;
7974
7975   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7976   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7977
7978   priv = entry->priv;
7979
7980   icon_info = priv->icons[icon_pos];
7981
7982   if (!icon_info)
7983     return NULL;
7984
7985   gtk_entry_ensure_pixbuf (entry, icon_pos);
7986
7987   return icon_info->pixbuf;
7988 }
7989
7990 /**
7991  * gtk_entry_get_icon_gicon:
7992  * @entry: A #GtkEntry
7993  * @icon_pos: Icon position
7994  *
7995  * Retrieves the #GIcon used for the icon, or %NULL if there is
7996  * no icon or if the icon was set by some other method (e.g., by
7997  * stock, pixbuf, or icon name).
7998  *
7999  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
8000  *     or if the icon is not a #GIcon
8001  *
8002  * Since: 2.16
8003  */
8004 GIcon *
8005 gtk_entry_get_icon_gicon (GtkEntry             *entry,
8006                           GtkEntryIconPosition  icon_pos)
8007 {
8008   GtkEntryPrivate *priv;
8009   EntryIconInfo *icon_info;
8010
8011   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8012   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8013
8014   priv = entry->priv;
8015   icon_info = priv->icons[icon_pos];
8016
8017   if (!icon_info)
8018     return NULL;
8019
8020   return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
8021 }
8022
8023 /**
8024  * gtk_entry_get_icon_stock:
8025  * @entry: A #GtkEntry
8026  * @icon_pos: Icon position
8027  *
8028  * Retrieves the stock id used for the icon, or %NULL if there is
8029  * no icon or if the icon was set by some other method (e.g., by
8030  * pixbuf, icon name or gicon).
8031  *
8032  * Returns: A stock id, or %NULL if no icon is set or if the icon
8033  *          wasn't set from a stock id
8034  *
8035  * Since: 2.16
8036  */
8037 const gchar *
8038 gtk_entry_get_icon_stock (GtkEntry             *entry,
8039                           GtkEntryIconPosition  icon_pos)
8040 {
8041   GtkEntryPrivate *priv;
8042   EntryIconInfo *icon_info;
8043
8044   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8045   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8046
8047   priv = entry->priv;
8048   icon_info = priv->icons[icon_pos];
8049
8050   if (!icon_info)
8051     return NULL;
8052
8053   return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
8054 }
8055
8056 /**
8057  * gtk_entry_get_icon_name:
8058  * @entry: A #GtkEntry
8059  * @icon_pos: Icon position
8060  *
8061  * Retrieves the icon name used for the icon, or %NULL if there is
8062  * no icon or if the icon was set by some other method (e.g., by
8063  * pixbuf, stock or gicon).
8064  *
8065  * Returns: An icon name, or %NULL if no icon is set or if the icon
8066  *          wasn't set from an icon name
8067  *
8068  * Since: 2.16
8069  */
8070 const gchar *
8071 gtk_entry_get_icon_name (GtkEntry             *entry,
8072                          GtkEntryIconPosition  icon_pos)
8073 {
8074   GtkEntryPrivate *priv;
8075   EntryIconInfo *icon_info;
8076
8077   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8078   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8079
8080   priv = entry->priv;
8081   icon_info = priv->icons[icon_pos];
8082
8083   if (!icon_info)
8084     return NULL;
8085
8086   return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
8087 }
8088
8089 /**
8090  * gtk_entry_set_icon_sensitive:
8091  * @entry: A #GtkEntry
8092  * @icon_pos: Icon position
8093  * @sensitive: Specifies whether the icon should appear
8094  *             sensitive or insensitive
8095  *
8096  * Sets the sensitivity for the specified icon.
8097  *
8098  * Since: 2.16
8099  */
8100 void
8101 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8102                               GtkEntryIconPosition  icon_pos,
8103                               gboolean              sensitive)
8104 {
8105   GtkEntryPrivate *priv;
8106   EntryIconInfo *icon_info;
8107
8108   g_return_if_fail (GTK_IS_ENTRY (entry));
8109   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8110
8111   priv = entry->priv;
8112
8113   if ((icon_info = priv->icons[icon_pos]) == NULL)
8114     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8115
8116   if (icon_info->insensitive != !sensitive)
8117     {
8118       icon_info->insensitive = !sensitive;
8119
8120       icon_info->pressed = FALSE;
8121       icon_info->prelight = FALSE;
8122
8123       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8124         update_cursors (GTK_WIDGET (entry));
8125
8126       gtk_widget_queue_draw (GTK_WIDGET (entry));
8127
8128       g_object_notify (G_OBJECT (entry),
8129                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8130     }
8131 }
8132
8133 /**
8134  * gtk_entry_get_icon_sensitive:
8135  * @entry: a #GtkEntry
8136  * @icon_pos: Icon position
8137  *
8138  * Returns whether the icon appears sensitive or insensitive.
8139  *
8140  * Returns: %TRUE if the icon is sensitive.
8141  *
8142  * Since: 2.16
8143  */
8144 gboolean
8145 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8146                               GtkEntryIconPosition  icon_pos)
8147 {
8148   GtkEntryPrivate *priv;
8149   EntryIconInfo *icon_info;
8150
8151   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8152   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8153
8154   priv = entry->priv;
8155
8156   icon_info = priv->icons[icon_pos];
8157
8158   return (!icon_info || !icon_info->insensitive);
8159
8160 }
8161
8162 /**
8163  * gtk_entry_get_icon_storage_type:
8164  * @entry: a #GtkEntry
8165  * @icon_pos: Icon position
8166  *
8167  * Gets the type of representation being used by the icon
8168  * to store image data. If the icon has no image data,
8169  * the return value will be %GTK_IMAGE_EMPTY.
8170  *
8171  * Return value: image representation being used
8172  *
8173  * Since: 2.16
8174  **/
8175 GtkImageType
8176 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8177                                  GtkEntryIconPosition  icon_pos)
8178 {
8179   GtkEntryPrivate *priv;
8180   EntryIconInfo *icon_info;
8181
8182   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8183   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8184
8185   priv = entry->priv;
8186
8187   icon_info = priv->icons[icon_pos];
8188
8189   if (!icon_info)
8190     return GTK_IMAGE_EMPTY;
8191
8192   return icon_info->storage_type;
8193 }
8194
8195 /**
8196  * gtk_entry_get_icon_at_pos:
8197  * @entry: a #GtkEntry
8198  * @x: the x coordinate of the position to find
8199  * @y: the y coordinate of the position to find
8200  *
8201  * Finds the icon at the given position and return its index.
8202  * If @x, @y doesn't lie inside an icon, -1 is returned.
8203  * This function is intended for use in a #GtkWidget::query-tooltip
8204  * signal handler.
8205  *
8206  * Returns: the index of the icon at the given position, or -1
8207  *
8208  * Since: 2.16
8209  */
8210 gint
8211 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8212                            gint      x,
8213                            gint      y)
8214 {
8215   GtkAllocation primary;
8216   GtkAllocation secondary;
8217   gint frame_x, frame_y;
8218
8219   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8220
8221   get_frame_size (entry, &frame_x, &frame_y, NULL, NULL);
8222   x -= frame_x;
8223   y -= frame_y;
8224
8225   get_icon_allocations (entry, &primary, &secondary);
8226
8227   if (primary.x <= x && x < primary.x + primary.width &&
8228       primary.y <= y && y < primary.y + primary.height)
8229     return GTK_ENTRY_ICON_PRIMARY;
8230
8231   if (secondary.x <= x && x < secondary.x + secondary.width &&
8232       secondary.y <= y && y < secondary.y + secondary.height)
8233     return GTK_ENTRY_ICON_SECONDARY;
8234
8235   return -1;
8236 }
8237
8238 /**
8239  * gtk_entry_set_icon_drag_source:
8240  * @entry: a #GtkIconEntry
8241  * @icon_pos: icon position
8242  * @target_list: the targets (data formats) in which the data can be provided
8243  * @actions: a bitmask of the allowed drag actions
8244  *
8245  * Sets up the icon at the given position so that GTK+ will start a drag
8246  * operation when the user clicks and drags the icon.
8247  *
8248  * To handle the drag operation, you need to connect to the usual
8249  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8250  * signal, and use gtk_entry_get_current_icon_drag_source() in
8251  * your signal handler to find out if the drag was started from
8252  * an icon.
8253  *
8254  * By default, GTK+ uses the icon as the drag icon. You can use the 
8255  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8256  * have to use g_signal_connect_after() to ensure that your signal handler
8257  * gets executed after the default handler.
8258  *
8259  * Since: 2.16
8260  */
8261 void
8262 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8263                                 GtkEntryIconPosition  icon_pos,
8264                                 GtkTargetList        *target_list,
8265                                 GdkDragAction         actions)
8266 {
8267   GtkEntryPrivate *priv;
8268   EntryIconInfo *icon_info;
8269
8270   g_return_if_fail (GTK_IS_ENTRY (entry));
8271   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8272
8273   priv = entry->priv;
8274
8275   if ((icon_info = priv->icons[icon_pos]) == NULL)
8276     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8277
8278   if (icon_info->target_list)
8279     gtk_target_list_unref (icon_info->target_list);
8280   icon_info->target_list = target_list;
8281   if (icon_info->target_list)
8282     gtk_target_list_ref (icon_info->target_list);
8283
8284   icon_info->actions = actions;
8285 }
8286
8287 /**
8288  * gtk_entry_get_current_icon_drag_source:
8289  * @entry: a #GtkIconEntry
8290  *
8291  * Returns the index of the icon which is the source of the current
8292  * DND operation, or -1.
8293  *
8294  * This function is meant to be used in a #GtkWidget::drag-data-get
8295  * callback.
8296  *
8297  * Returns: index of the icon which is the source of the current
8298  *          DND operation, or -1.
8299  *
8300  * Since: 2.16
8301  */
8302 gint
8303 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8304 {
8305   GtkEntryPrivate *priv;
8306   EntryIconInfo *icon_info = NULL;
8307   gint i;
8308
8309   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8310
8311   priv = entry->priv;
8312
8313   for (i = 0; i < MAX_ICONS; i++)
8314     {
8315       if ((icon_info = priv->icons[i]))
8316         {
8317           if (icon_info->in_drag)
8318             return i;
8319         }
8320     }
8321
8322   return -1;
8323 }
8324
8325 /**
8326  * gtk_entry_get_icon_area:
8327  * @entry: A #GtkEntry
8328  * @icon_pos: Icon position
8329  * @icon_area: Return location for the icon's area
8330  *
8331  * Gets the area where entry's icon at @icon_pos is drawn.
8332  * This function is useful when drawing something to the
8333  * entry in a draw callback.
8334  *
8335  * If the entry is not realized or has no icon at the given position,
8336  * @icon_area is filled with zeros.
8337  *
8338  * See also gtk_entry_get_text_area()
8339  *
8340  * Since: 3.0
8341  */
8342 void
8343 gtk_entry_get_icon_area (GtkEntry             *entry,
8344                          GtkEntryIconPosition  icon_pos,
8345                          GdkRectangle         *icon_area)
8346 {
8347   GtkEntryPrivate *priv;
8348   EntryIconInfo *icon_info;
8349
8350   g_return_if_fail (GTK_IS_ENTRY (entry));
8351   g_return_if_fail (icon_area != NULL);
8352
8353   priv = entry->priv;
8354
8355   icon_info = priv->icons[icon_pos];
8356
8357   if (icon_info)
8358     {
8359       GtkAllocation primary;
8360       GtkAllocation secondary;
8361
8362       get_icon_allocations (entry, &primary, &secondary);
8363
8364       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8365         *icon_area = primary;
8366       else
8367         *icon_area = secondary;
8368     }
8369   else
8370     {
8371       icon_area->x = 0;
8372       icon_area->y = 0;
8373       icon_area->width = 0;
8374       icon_area->height = 0;
8375     }
8376 }
8377
8378 static void
8379 ensure_has_tooltip (GtkEntry *entry)
8380 {
8381   GtkEntryPrivate *priv;
8382   EntryIconInfo *icon_info;
8383   int i;
8384   gboolean has_tooltip = FALSE;
8385
8386   priv = entry->priv;
8387
8388   for (i = 0; i < MAX_ICONS; i++)
8389     {
8390       if ((icon_info = priv->icons[i]) != NULL)
8391         {
8392           if (icon_info->tooltip != NULL)
8393             {
8394               has_tooltip = TRUE;
8395               break;
8396             }
8397         }
8398     }
8399
8400   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8401 }
8402
8403 /**
8404  * gtk_entry_get_icon_tooltip_text:
8405  * @entry: a #GtkEntry
8406  * @icon_pos: the icon position
8407  *
8408  * Gets the contents of the tooltip on the icon at the specified 
8409  * position in @entry.
8410  * 
8411  * Returns: the tooltip text, or %NULL. Free the returned string
8412  *     with g_free() when done.
8413  * 
8414  * Since: 2.16
8415  */
8416 gchar *
8417 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8418                                  GtkEntryIconPosition  icon_pos)
8419 {
8420   GtkEntryPrivate *priv;
8421   EntryIconInfo *icon_info;
8422   gchar *text = NULL;
8423
8424   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8425   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8426
8427   priv = entry->priv;
8428
8429   icon_info = priv->icons[icon_pos];
8430
8431   if (!icon_info)
8432     return NULL;
8433  
8434   if (icon_info->tooltip && 
8435       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8436     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8437
8438   return text;
8439 }
8440
8441 /**
8442  * gtk_entry_set_icon_tooltip_text:
8443  * @entry: a #GtkEntry
8444  * @icon_pos: the icon position
8445  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8446  *
8447  * Sets @tooltip as the contents of the tooltip for the icon
8448  * at the specified position.
8449  *
8450  * Use %NULL for @tooltip to remove an existing tooltip.
8451  *
8452  * See also gtk_widget_set_tooltip_text() and 
8453  * gtk_entry_set_icon_tooltip_markup().
8454  *
8455  * Since: 2.16
8456  */
8457 void
8458 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8459                                  GtkEntryIconPosition  icon_pos,
8460                                  const gchar          *tooltip)
8461 {
8462   GtkEntryPrivate *priv;
8463   EntryIconInfo *icon_info;
8464
8465   g_return_if_fail (GTK_IS_ENTRY (entry));
8466   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8467
8468   priv = entry->priv;
8469
8470   if (!(icon_info = priv->icons[icon_pos]))
8471     icon_info = priv->icons[icon_pos];
8472
8473   if (icon_info->tooltip)
8474     g_free (icon_info->tooltip);
8475
8476   /* Treat an empty string as a NULL string,
8477    * because an empty string would be useless for a tooltip:
8478    */
8479   if (tooltip && tooltip[0] == '\0')
8480     tooltip = NULL;
8481
8482   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8483
8484   ensure_has_tooltip (entry);
8485 }
8486
8487 /**
8488  * gtk_entry_get_icon_tooltip_markup:
8489  * @entry: a #GtkEntry
8490  * @icon_pos: the icon position
8491  *
8492  * Gets the contents of the tooltip on the icon at the specified 
8493  * position in @entry.
8494  * 
8495  * Returns: the tooltip text, or %NULL. Free the returned string
8496  *     with g_free() when done.
8497  * 
8498  * Since: 2.16
8499  */
8500 gchar *
8501 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8502                                    GtkEntryIconPosition  icon_pos)
8503 {
8504   GtkEntryPrivate *priv;
8505   EntryIconInfo *icon_info;
8506
8507   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8508   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8509
8510   priv = entry->priv;
8511
8512   icon_info = priv->icons[icon_pos];
8513
8514   if (!icon_info)
8515     return NULL;
8516  
8517   return g_strdup (icon_info->tooltip);
8518 }
8519
8520 /**
8521  * gtk_entry_set_icon_tooltip_markup:
8522  * @entry: a #GtkEntry
8523  * @icon_pos: the icon position
8524  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8525  *
8526  * Sets @tooltip as the contents of the tooltip for the icon at
8527  * the specified position. @tooltip is assumed to be marked up with
8528  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8529  *
8530  * Use %NULL for @tooltip to remove an existing tooltip.
8531  *
8532  * See also gtk_widget_set_tooltip_markup() and 
8533  * gtk_enty_set_icon_tooltip_text().
8534  *
8535  * Since: 2.16
8536  */
8537 void
8538 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8539                                    GtkEntryIconPosition  icon_pos,
8540                                    const gchar          *tooltip)
8541 {
8542   GtkEntryPrivate *priv;
8543   EntryIconInfo *icon_info;
8544
8545   g_return_if_fail (GTK_IS_ENTRY (entry));
8546   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8547
8548   priv = entry->priv;
8549
8550   if (!(icon_info = priv->icons[icon_pos]))
8551     icon_info = priv->icons[icon_pos];
8552
8553   if (icon_info->tooltip)
8554     g_free (icon_info->tooltip);
8555
8556   /* Treat an empty string as a NULL string,
8557    * because an empty string would be useless for a tooltip:
8558    */
8559   if (tooltip && tooltip[0] == '\0')
8560     tooltip = NULL;
8561
8562   icon_info->tooltip = g_strdup (tooltip);
8563
8564   ensure_has_tooltip (entry);
8565 }
8566
8567 static gboolean
8568 gtk_entry_query_tooltip (GtkWidget  *widget,
8569                          gint        x,
8570                          gint        y,
8571                          gboolean    keyboard_tip,
8572                          GtkTooltip *tooltip)
8573 {
8574   GtkEntry *entry;
8575   GtkEntryPrivate *priv;
8576   EntryIconInfo *icon_info;
8577   gint icon_pos;
8578
8579   entry = GTK_ENTRY (widget);
8580   priv = entry->priv;
8581
8582   if (!keyboard_tip)
8583     {
8584       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8585       if (icon_pos != -1)
8586         {
8587           if ((icon_info = priv->icons[icon_pos]) != NULL)
8588             {
8589               if (icon_info->tooltip)
8590                 {
8591                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8592                   return TRUE;
8593                 }
8594
8595               return FALSE;
8596             }
8597         }
8598     }
8599
8600   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8601                                                                    x, y,
8602                                                                    keyboard_tip,
8603                                                                    tooltip);
8604 }
8605
8606
8607 /* Quick hack of a popup menu
8608  */
8609 static void
8610 activate_cb (GtkWidget *menuitem,
8611              GtkEntry  *entry)
8612 {
8613   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8614   g_signal_emit_by_name (entry, signal);
8615 }
8616
8617
8618 static gboolean
8619 gtk_entry_mnemonic_activate (GtkWidget *widget,
8620                              gboolean   group_cycling)
8621 {
8622   gtk_widget_grab_focus (widget);
8623   return TRUE;
8624 }
8625
8626 static void
8627 append_action_signal (GtkEntry     *entry,
8628                       GtkWidget    *menu,
8629                       const gchar  *stock_id,
8630                       const gchar  *signal,
8631                       gboolean      sensitive)
8632 {
8633   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8634
8635   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8636   g_signal_connect (menuitem, "activate",
8637                     G_CALLBACK (activate_cb), entry);
8638
8639   gtk_widget_set_sensitive (menuitem, sensitive);
8640   
8641   gtk_widget_show (menuitem);
8642   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8643 }
8644         
8645 static void
8646 popup_menu_detach (GtkWidget *attach_widget,
8647                    GtkMenu   *menu)
8648 {
8649   GtkEntry *entry_attach = GTK_ENTRY (attach_widget);
8650   GtkEntryPrivate *priv_attach = entry_attach->priv;
8651
8652   priv_attach->popup_menu = NULL;
8653 }
8654
8655 static void
8656 popup_position_func (GtkMenu   *menu,
8657                      gint      *x,
8658                      gint      *y,
8659                      gboolean  *push_in,
8660                      gpointer   user_data)
8661 {
8662   GtkEntry *entry = GTK_ENTRY (user_data);
8663   GtkEntryPrivate *priv = entry->priv;
8664   GtkWidget *widget = GTK_WIDGET (entry);
8665   GdkScreen *screen;
8666   GtkRequisition menu_req;
8667   GdkRectangle monitor;
8668   GtkBorder inner_border;
8669   gint monitor_num, strong_x, height;
8670  
8671   g_return_if_fail (gtk_widget_get_realized (widget));
8672
8673   gdk_window_get_origin (priv->text_area, x, y);
8674
8675   screen = gtk_widget_get_screen (widget);
8676   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->text_area);
8677   if (monitor_num < 0)
8678     monitor_num = 0;
8679   gtk_menu_set_monitor (menu, monitor_num);
8680
8681   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8682   gtk_widget_get_preferred_size (priv->popup_menu,
8683                                  &menu_req, NULL);
8684   height = gdk_window_get_height (priv->text_area);
8685   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8686   _gtk_entry_effective_inner_border (entry, &inner_border);
8687
8688   *x += inner_border.left + strong_x - priv->scroll_offset;
8689   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8690     *x -= menu_req.width;
8691
8692   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8693     *y += height;
8694   else if ((*y - menu_req.height) >= monitor.y)
8695     *y -= menu_req.height;
8696   else if (monitor.y + monitor.height - (*y + height) > *y)
8697     *y += height;
8698   else
8699     *y -= menu_req.height;
8700
8701   *push_in = FALSE;
8702 }
8703
8704 static void
8705 unichar_chosen_func (const char *text,
8706                      gpointer    data)
8707 {
8708   GtkEntry *entry = GTK_ENTRY (data);
8709   GtkEntryPrivate *priv = entry->priv;
8710
8711   if (priv->editable)
8712     gtk_entry_enter_text (entry, text);
8713 }
8714
8715 typedef struct
8716 {
8717   GtkEntry *entry;
8718   gint button;
8719   guint time;
8720 } PopupInfo;
8721
8722 static void
8723 popup_targets_received (GtkClipboard     *clipboard,
8724                         GtkSelectionData *data,
8725                         gpointer          user_data)
8726 {
8727   PopupInfo *info = user_data;
8728   GtkEntry *entry = info->entry;
8729   GtkEntryPrivate *info_entry_priv = entry->priv;
8730
8731   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8732     {
8733       DisplayMode mode;
8734       gboolean clipboard_contains_text;
8735       GtkWidget *menuitem;
8736       GtkWidget *submenu;
8737       gboolean show_input_method_menu;
8738       gboolean show_unicode_menu;
8739       
8740       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8741       if (info_entry_priv->popup_menu)
8742         gtk_widget_destroy (info_entry_priv->popup_menu);
8743
8744       info_entry_priv->popup_menu = gtk_menu_new ();
8745
8746       gtk_menu_attach_to_widget (GTK_MENU (info_entry_priv->popup_menu),
8747                                  GTK_WIDGET (entry),
8748                                  popup_menu_detach);
8749       
8750       mode = gtk_entry_get_display_mode (entry);
8751       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8752                             info_entry_priv->editable && mode == DISPLAY_NORMAL &&
8753                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8754
8755       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8756                             mode == DISPLAY_NORMAL &&
8757                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8758
8759       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8760                             info_entry_priv->editable && clipboard_contains_text);
8761
8762       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8763       gtk_widget_set_sensitive (menuitem, info_entry_priv->editable && info_entry_priv->current_pos != info_entry_priv->selection_bound);
8764       g_signal_connect_swapped (menuitem, "activate",
8765                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8766       gtk_widget_show (menuitem);
8767       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8768
8769       menuitem = gtk_separator_menu_item_new ();
8770       gtk_widget_show (menuitem);
8771       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8772       
8773       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8774       g_signal_connect_swapped (menuitem, "activate",
8775                                 G_CALLBACK (gtk_entry_select_all), entry);
8776       gtk_widget_show (menuitem);
8777       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8778       
8779       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8780                     "gtk-show-input-method-menu", &show_input_method_menu,
8781                     "gtk-show-unicode-menu", &show_unicode_menu,
8782                     NULL);
8783
8784       if (show_input_method_menu || show_unicode_menu)
8785         {
8786           menuitem = gtk_separator_menu_item_new ();
8787           gtk_widget_show (menuitem);
8788           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8789         }
8790       
8791       if (show_input_method_menu)
8792         {
8793           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8794           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8795           gtk_widget_show (menuitem);
8796           submenu = gtk_menu_new ();
8797           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8798
8799           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8800
8801           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (info_entry_priv->im_context),
8802                                                 GTK_MENU_SHELL (submenu));
8803         }
8804       
8805       if (show_unicode_menu)
8806         {
8807           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8808           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8809           gtk_widget_show (menuitem);
8810           
8811           submenu = gtk_menu_new ();
8812           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8813           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8814
8815           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8816                                                         unichar_chosen_func,
8817                                                         entry);
8818         }
8819       
8820       g_signal_emit (entry,
8821                      signals[POPULATE_POPUP],
8822                      0,
8823                      info_entry_priv->popup_menu);
8824
8825
8826       if (info->button)
8827         gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8828                         NULL, NULL,
8829                         info->button, info->time);
8830       else
8831         {
8832           gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8833                           popup_position_func, entry,
8834                           info->button, info->time);
8835           gtk_menu_shell_select_first (GTK_MENU_SHELL (info_entry_priv->popup_menu), FALSE);
8836         }
8837     }
8838
8839   g_object_unref (entry);
8840   g_slice_free (PopupInfo, info);
8841 }
8842                         
8843 static void
8844 gtk_entry_do_popup (GtkEntry       *entry,
8845                     GdkEventButton *event)
8846 {
8847   PopupInfo *info = g_slice_new (PopupInfo);
8848
8849   /* In order to know what entries we should make sensitive, we
8850    * ask for the current targets of the clipboard, and when
8851    * we get them, then we actually pop up the menu.
8852    */
8853   info->entry = g_object_ref (entry);
8854   
8855   if (event)
8856     {
8857       info->button = event->button;
8858       info->time = event->time;
8859     }
8860   else
8861     {
8862       info->button = 0;
8863       info->time = gtk_get_current_event_time ();
8864     }
8865
8866   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8867                                   gdk_atom_intern_static_string ("TARGETS"),
8868                                   popup_targets_received,
8869                                   info);
8870 }
8871
8872 static gboolean
8873 gtk_entry_popup_menu (GtkWidget *widget)
8874 {
8875   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8876   return TRUE;
8877 }
8878
8879 static void
8880 gtk_entry_drag_begin (GtkWidget      *widget,
8881                       GdkDragContext *context)
8882 {
8883   GtkEntry *entry = GTK_ENTRY (widget);
8884   GtkEntryPrivate *priv = entry->priv;
8885   gint i;
8886
8887   for (i = 0; i < MAX_ICONS; i++)
8888     {
8889       EntryIconInfo *icon_info = priv->icons[i];
8890       
8891       if (icon_info != NULL)
8892         {
8893           if (icon_info->in_drag) 
8894             {
8895               switch (icon_info->storage_type)
8896                 {
8897                 case GTK_IMAGE_STOCK:
8898                   gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
8899                   break;
8900
8901                 case GTK_IMAGE_ICON_NAME:
8902                   gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
8903                   break;
8904
8905                   /* FIXME: No GIcon support for dnd icons */
8906                 case GTK_IMAGE_GICON:
8907                 case GTK_IMAGE_PIXBUF:
8908                   gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
8909                   break;
8910                 default:
8911                   g_assert_not_reached ();
8912                 }
8913             }
8914         }
8915     }
8916 }
8917
8918 static void
8919 gtk_entry_drag_end (GtkWidget      *widget,
8920                     GdkDragContext *context)
8921 {
8922   GtkEntry *entry = GTK_ENTRY (widget);
8923   GtkEntryPrivate *priv = entry->priv;
8924   gint i;
8925
8926   for (i = 0; i < MAX_ICONS; i++)
8927     {
8928       EntryIconInfo *icon_info = priv->icons[i];
8929
8930       if (icon_info != NULL)
8931         icon_info->in_drag = 0;
8932     }
8933 }
8934
8935 static void
8936 gtk_entry_drag_leave (GtkWidget        *widget,
8937                       GdkDragContext   *context,
8938                       guint             time)
8939 {
8940   GtkEntry *entry = GTK_ENTRY (widget);
8941   GtkEntryPrivate *priv = entry->priv;
8942
8943   priv->dnd_position = -1;
8944   gtk_widget_queue_draw (widget);
8945 }
8946
8947 static gboolean
8948 gtk_entry_drag_drop  (GtkWidget        *widget,
8949                       GdkDragContext   *context,
8950                       gint              x,
8951                       gint              y,
8952                       guint             time)
8953 {
8954   GtkEntry *entry = GTK_ENTRY (widget);
8955   GtkEntryPrivate *priv = entry->priv;
8956   GdkAtom target = GDK_NONE;
8957
8958   if (priv->editable)
8959     target = gtk_drag_dest_find_target (widget, context, NULL);
8960
8961   if (target != GDK_NONE)
8962     gtk_drag_get_data (widget, context, target, time);
8963   else
8964     gtk_drag_finish (context, FALSE, FALSE, time);
8965   
8966   return TRUE;
8967 }
8968
8969 static gboolean
8970 gtk_entry_drag_motion (GtkWidget        *widget,
8971                        GdkDragContext   *context,
8972                        gint              x,
8973                        gint              y,
8974                        guint             time)
8975 {
8976   GtkEntry *entry = GTK_ENTRY (widget);
8977   GtkEntryPrivate *priv = entry->priv;
8978   GtkStyle *style;
8979   GtkWidget *source_widget;
8980   GdkDragAction suggested_action;
8981   gint new_position, old_position;
8982   gint sel1, sel2;
8983
8984   style = gtk_widget_get_style (widget);
8985   x -= style->xthickness;
8986   y -= style->ythickness;
8987
8988   old_position = priv->dnd_position;
8989   new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8990
8991   if (priv->editable &&
8992       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8993     {
8994       source_widget = gtk_drag_get_source_widget (context);
8995       suggested_action = context->suggested_action;
8996
8997       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8998           new_position < sel1 || new_position > sel2)
8999         {
9000           if (source_widget == widget)
9001             {
9002               /* Default to MOVE, unless the user has
9003                * pressed ctrl or alt to affect available actions
9004                */
9005               if ((context->actions & GDK_ACTION_MOVE) != 0)
9006                 suggested_action = GDK_ACTION_MOVE;
9007             }
9008
9009           priv->dnd_position = new_position;
9010         }
9011       else
9012         {
9013           if (source_widget == widget)
9014             suggested_action = 0;       /* Can't drop in selection where drag started */
9015
9016           priv->dnd_position = -1;
9017         }
9018     }
9019   else
9020     {
9021       /* Entry not editable, or no text */
9022       suggested_action = 0;
9023       priv->dnd_position = -1;
9024     }
9025   
9026   gdk_drag_status (context, suggested_action, time);
9027
9028   if (priv->dnd_position != old_position)
9029     gtk_widget_queue_draw (widget);
9030
9031   return TRUE;
9032 }
9033
9034 static void
9035 gtk_entry_drag_data_received (GtkWidget        *widget,
9036                               GdkDragContext   *context,
9037                               gint              x,
9038                               gint              y,
9039                               GtkSelectionData *selection_data,
9040                               guint             info,
9041                               guint             time)
9042 {
9043   GtkEntry *entry = GTK_ENTRY (widget);
9044   GtkEntryPrivate *priv = entry->priv;
9045   GtkEditable *editable = GTK_EDITABLE (widget);
9046   GtkStyle *style;
9047   gchar *str;
9048
9049   str = (gchar *) gtk_selection_data_get_text (selection_data);
9050
9051   style = gtk_widget_get_style (widget);
9052   x -= style->xthickness;
9053   y -= style->ythickness;
9054
9055   if (str && priv->editable)
9056     {
9057       gint new_position;
9058       gint sel1, sel2;
9059       gint length = -1;
9060
9061       if (priv->truncate_multiline)
9062         length = truncate_multiline (str);
9063
9064       new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
9065
9066       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
9067           new_position < sel1 || new_position > sel2)
9068         {
9069           gtk_editable_insert_text (editable, str, length, &new_position);
9070         }
9071       else
9072         {
9073           /* Replacing selection */
9074           begin_change (entry);
9075           g_object_freeze_notify (G_OBJECT (entry));
9076           gtk_editable_delete_text (editable, sel1, sel2);
9077           gtk_editable_insert_text (editable, str, length, &sel1);
9078           g_object_thaw_notify (G_OBJECT (entry));
9079           end_change (entry);
9080         }
9081       
9082       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
9083     }
9084   else
9085     {
9086       /* Drag and drop didn't happen! */
9087       gtk_drag_finish (context, FALSE, FALSE, time);
9088     }
9089
9090   g_free (str);
9091 }
9092
9093 static void
9094 gtk_entry_drag_data_get (GtkWidget        *widget,
9095                          GdkDragContext   *context,
9096                          GtkSelectionData *selection_data,
9097                          guint             info,
9098                          guint             time)
9099 {
9100   GtkEntry *entry = GTK_ENTRY (widget);
9101   GtkEntryPrivate *priv = entry->priv;
9102   GtkEditable *editable = GTK_EDITABLE (widget);
9103   gint sel_start, sel_end;
9104   gint i;
9105
9106   /* If there is an icon drag going on, exit early. */
9107   for (i = 0; i < MAX_ICONS; i++)
9108     {
9109       EntryIconInfo *icon_info = priv->icons[i];
9110
9111       if (icon_info != NULL)
9112         {
9113           if (icon_info->in_drag)
9114             return;
9115         }
9116     }
9117
9118   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9119     {
9120       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9121
9122       gtk_selection_data_set_text (selection_data, str, -1);
9123       
9124       g_free (str);
9125     }
9126
9127 }
9128
9129 static void
9130 gtk_entry_drag_data_delete (GtkWidget      *widget,
9131                             GdkDragContext *context)
9132 {
9133   GtkEntry *entry = GTK_ENTRY (widget);
9134   GtkEntryPrivate *priv = entry->priv;
9135   GtkEditable *editable = GTK_EDITABLE (widget);
9136   gint sel_start, sel_end;
9137   gint i;
9138
9139   /* If there is an icon drag going on, exit early. */
9140   for (i = 0; i < MAX_ICONS; i++)
9141     {
9142       EntryIconInfo *icon_info = priv->icons[i];
9143
9144       if (icon_info != NULL)
9145         {
9146           if (icon_info->in_drag)
9147             return;
9148         }
9149     }
9150
9151   if (priv->editable &&
9152       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9153     gtk_editable_delete_text (editable, sel_start, sel_end);
9154 }
9155
9156 /* We display the cursor when
9157  *
9158  *  - the selection is empty, AND
9159  *  - the widget has focus
9160  */
9161
9162 #define CURSOR_ON_MULTIPLIER 2
9163 #define CURSOR_OFF_MULTIPLIER 1
9164 #define CURSOR_PEND_MULTIPLIER 3
9165 #define CURSOR_DIVIDER 3
9166
9167 static gboolean
9168 cursor_blinks (GtkEntry *entry)
9169 {
9170   GtkEntryPrivate *priv = entry->priv;
9171
9172   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9173       priv->editable &&
9174       priv->selection_bound == priv->current_pos)
9175     {
9176       GtkSettings *settings;
9177       gboolean blink;
9178
9179       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9180       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9181
9182       return blink;
9183     }
9184   else
9185     return FALSE;
9186 }
9187
9188 static gint
9189 get_cursor_time (GtkEntry *entry)
9190 {
9191   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9192   gint time;
9193
9194   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9195
9196   return time;
9197 }
9198
9199 static gint
9200 get_cursor_blink_timeout (GtkEntry *entry)
9201 {
9202   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9203   gint timeout;
9204
9205   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9206
9207   return timeout;
9208 }
9209
9210 static void
9211 show_cursor (GtkEntry *entry)
9212 {
9213   GtkEntryPrivate *priv = entry->priv;
9214   GtkWidget *widget;
9215
9216   if (!priv->cursor_visible)
9217     {
9218       priv->cursor_visible = TRUE;
9219
9220       widget = GTK_WIDGET (entry);
9221       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9222         gtk_widget_queue_draw (widget);
9223     }
9224 }
9225
9226 static void
9227 hide_cursor (GtkEntry *entry)
9228 {
9229   GtkEntryPrivate *priv = entry->priv;
9230   GtkWidget *widget;
9231
9232   if (priv->cursor_visible)
9233     {
9234       priv->cursor_visible = FALSE;
9235
9236       widget = GTK_WIDGET (entry);
9237       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9238         gtk_widget_queue_draw (widget);
9239     }
9240 }
9241
9242 /*
9243  * Blink!
9244  */
9245 static gint
9246 blink_cb (gpointer data)
9247 {
9248   GtkEntry *entry;
9249   GtkEntryPrivate *priv; 
9250   gint blink_timeout;
9251
9252   entry = GTK_ENTRY (data);
9253   priv = entry->priv;
9254  
9255   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9256     {
9257       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9258                  "connect a handler to this signal, it must return\n"
9259                  "FALSE so the entry gets the event as well");
9260
9261       gtk_entry_check_cursor_blink (entry);
9262
9263       return FALSE;
9264     }
9265   
9266   g_assert (priv->selection_bound == priv->current_pos);
9267   
9268   blink_timeout = get_cursor_blink_timeout (entry);
9269   if (priv->blink_time > 1000 * blink_timeout && 
9270       blink_timeout < G_MAXINT/1000) 
9271     {
9272       /* we've blinked enough without the user doing anything, stop blinking */
9273       show_cursor (entry);
9274       priv->blink_timeout = 0;
9275     } 
9276   else if (priv->cursor_visible)
9277     {
9278       hide_cursor (entry);
9279       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9280                                             blink_cb,
9281                                             entry);
9282     }
9283   else
9284     {
9285       show_cursor (entry);
9286       priv->blink_time += get_cursor_time (entry);
9287       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9288                                             blink_cb,
9289                                             entry);
9290     }
9291
9292   /* Remove ourselves */
9293   return FALSE;
9294 }
9295
9296 static void
9297 gtk_entry_check_cursor_blink (GtkEntry *entry)
9298 {
9299   GtkEntryPrivate *priv = entry->priv;
9300
9301   if (cursor_blinks (entry))
9302     {
9303       if (!priv->blink_timeout)
9304         {
9305           show_cursor (entry);
9306           priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9307                                                 blink_cb,
9308                                                 entry);
9309         }
9310     }
9311   else
9312     {
9313       if (priv->blink_timeout)
9314         {
9315           g_source_remove (priv->blink_timeout);
9316           priv->blink_timeout = 0;
9317         }
9318
9319       priv->cursor_visible = TRUE;
9320     }
9321 }
9322
9323 static void
9324 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9325 {
9326   GtkEntryPrivate *priv = entry->priv;
9327
9328   if (cursor_blinks (entry))
9329     {
9330       if (priv->blink_timeout != 0)
9331         g_source_remove (priv->blink_timeout);
9332
9333       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9334                                                      blink_cb,
9335                                                      entry);
9336       show_cursor (entry);
9337     }
9338 }
9339
9340 static void
9341 gtk_entry_reset_blink_time (GtkEntry *entry)
9342 {
9343   GtkEntryPrivate *priv = entry->priv;
9344
9345   priv->blink_time = 0;
9346 }
9347
9348
9349 /* completion */
9350 static gint
9351 gtk_entry_completion_timeout (gpointer data)
9352 {
9353   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9354   GtkEntryPrivate *completion_entry_priv = GTK_ENTRY (completion->priv->entry)->priv;
9355
9356   completion->priv->completion_timeout = 0;
9357
9358   if (completion->priv->filter_model &&
9359       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9360       >= completion->priv->minimum_key_length)
9361     {
9362       gint matches;
9363       gint actions;
9364       GtkTreeSelection *s;
9365       gboolean popup_single;
9366
9367       gtk_entry_completion_complete (completion);
9368       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9369
9370       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9371
9372       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9373
9374       gtk_tree_selection_unselect_all (s);
9375
9376       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9377
9378       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9379       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9380         { 
9381           if (gtk_widget_get_visible (completion->priv->popup_window))
9382             _gtk_entry_completion_resize_popup (completion);
9383           else
9384             _gtk_entry_completion_popup (completion, completion_entry_priv->completion_device);
9385         }
9386       else 
9387         _gtk_entry_completion_popdown (completion);
9388     }
9389   else if (gtk_widget_get_visible (completion->priv->popup_window))
9390     _gtk_entry_completion_popdown (completion);
9391
9392   return FALSE;
9393 }
9394
9395 static inline gboolean
9396 keyval_is_cursor_move (guint keyval)
9397 {
9398   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9399     return TRUE;
9400
9401   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9402     return TRUE;
9403
9404   if (keyval == GDK_KEY_Page_Up)
9405     return TRUE;
9406
9407   if (keyval == GDK_KEY_Page_Down)
9408     return TRUE;
9409
9410   return FALSE;
9411 }
9412
9413 static gboolean
9414 gtk_entry_completion_key_press (GtkWidget   *widget,
9415                                 GdkEventKey *event,
9416                                 gpointer     user_data)
9417 {
9418   gint matches, actions = 0;
9419   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9420
9421   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9422     return FALSE;
9423
9424   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9425
9426   if (completion->priv->actions)
9427     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9428
9429   if (keyval_is_cursor_move (event->keyval))
9430     {
9431       GtkTreePath *path = NULL;
9432       
9433       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9434         {
9435           if (completion->priv->current_selected < 0)
9436             completion->priv->current_selected = matches + actions - 1;
9437           else
9438             completion->priv->current_selected--;
9439         }
9440       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9441         {
9442           if (completion->priv->current_selected < matches + actions - 1)
9443             completion->priv->current_selected++;
9444           else
9445             completion->priv->current_selected = -1;
9446         }
9447       else if (event->keyval == GDK_KEY_Page_Up)
9448         {
9449           if (completion->priv->current_selected < 0)
9450             completion->priv->current_selected = matches + actions - 1;
9451           else if (completion->priv->current_selected == 0)
9452             completion->priv->current_selected = -1;
9453           else if (completion->priv->current_selected < matches) 
9454             {
9455               completion->priv->current_selected -= 14;
9456               if (completion->priv->current_selected < 0)
9457                 completion->priv->current_selected = 0;
9458             }
9459           else 
9460             {
9461               completion->priv->current_selected -= 14;
9462               if (completion->priv->current_selected < matches - 1)
9463                 completion->priv->current_selected = matches - 1;
9464             }
9465         }
9466       else if (event->keyval == GDK_KEY_Page_Down)
9467         {
9468           if (completion->priv->current_selected < 0)
9469             completion->priv->current_selected = 0;
9470           else if (completion->priv->current_selected < matches - 1)
9471             {
9472               completion->priv->current_selected += 14;
9473               if (completion->priv->current_selected > matches - 1)
9474                 completion->priv->current_selected = matches - 1;
9475             }
9476           else if (completion->priv->current_selected == matches + actions - 1)
9477             {
9478               completion->priv->current_selected = -1;
9479             }
9480           else
9481             {
9482               completion->priv->current_selected += 14;
9483               if (completion->priv->current_selected > matches + actions - 1)
9484                 completion->priv->current_selected = matches + actions - 1;
9485             }
9486         }
9487
9488       if (completion->priv->current_selected < 0)
9489         {
9490           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9491           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9492
9493           if (completion->priv->inline_selection &&
9494               completion->priv->completion_prefix)
9495             {
9496               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9497                                   completion->priv->completion_prefix);
9498               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9499             }
9500         }
9501       else if (completion->priv->current_selected < matches)
9502         {
9503           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9504
9505           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9506           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9507                                     path, NULL, FALSE);
9508
9509           if (completion->priv->inline_selection)
9510             {
9511
9512               GtkTreeIter iter;
9513               GtkTreeIter child_iter;
9514               GtkTreeModel *model = NULL;
9515               GtkTreeSelection *sel;
9516               gboolean entry_set;
9517
9518               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9519               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9520                 return FALSE;
9521
9522               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9523               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9524               
9525               if (completion->priv->completion_prefix == NULL)
9526                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9527
9528               g_signal_emit_by_name (completion, "cursor-on-match", model,
9529                                      &child_iter, &entry_set);
9530             }
9531         }
9532       else if (completion->priv->current_selected - matches >= 0)
9533         {
9534           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9535
9536           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9537           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9538                                     path, NULL, FALSE);
9539
9540           if (completion->priv->inline_selection &&
9541               completion->priv->completion_prefix)
9542             {
9543               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9544                                   completion->priv->completion_prefix);
9545               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9546             }
9547         }
9548
9549       gtk_tree_path_free (path);
9550
9551       return TRUE;
9552     }
9553   else if (event->keyval == GDK_KEY_Escape ||
9554            event->keyval == GDK_KEY_Left ||
9555            event->keyval == GDK_KEY_KP_Left ||
9556            event->keyval == GDK_KEY_Right ||
9557            event->keyval == GDK_KEY_KP_Right) 
9558     {
9559       gboolean retval = TRUE;
9560
9561       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9562       _gtk_entry_completion_popdown (completion);
9563
9564       if (completion->priv->current_selected < 0)
9565         {
9566           retval = FALSE;
9567           goto keypress_completion_out;
9568         }
9569       else if (completion->priv->inline_selection)
9570         {
9571           /* Escape rejects the tentative completion */
9572           if (event->keyval == GDK_KEY_Escape)
9573             {
9574               if (completion->priv->completion_prefix)
9575                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9576                                     completion->priv->completion_prefix);
9577               else 
9578                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9579             }
9580
9581           /* Move the cursor to the end for Right/Esc, to the
9582              beginning for Left */
9583           if (event->keyval == GDK_KEY_Right ||
9584               event->keyval == GDK_KEY_KP_Right ||
9585               event->keyval == GDK_KEY_Escape)
9586             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9587           else
9588             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9589         }
9590
9591 keypress_completion_out:
9592       if (completion->priv->inline_selection)
9593         {
9594           g_free (completion->priv->completion_prefix);
9595           completion->priv->completion_prefix = NULL;
9596         }
9597
9598       return retval;
9599     }
9600   else if (event->keyval == GDK_KEY_Tab || 
9601            event->keyval == GDK_KEY_KP_Tab ||
9602            event->keyval == GDK_KEY_ISO_Left_Tab) 
9603     {
9604       GtkDirectionType dir = event->keyval == GDK_KEY_ISO_Left_Tab ? 
9605         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
9606       
9607       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9608       _gtk_entry_completion_popdown (completion);
9609
9610       g_free (completion->priv->completion_prefix);
9611       completion->priv->completion_prefix = NULL;
9612
9613       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
9614
9615       return TRUE;
9616     }
9617   else if (event->keyval == GDK_KEY_ISO_Enter ||
9618            event->keyval == GDK_KEY_KP_Enter ||
9619            event->keyval == GDK_KEY_Return)
9620     {
9621       GtkTreeIter iter;
9622       GtkTreeModel *model = NULL;
9623       GtkTreeSelection *sel;
9624       gboolean retval = TRUE;
9625
9626       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9627       _gtk_entry_completion_popdown (completion);
9628
9629       if (completion->priv->current_selected < matches)
9630         {
9631           gboolean entry_set;
9632
9633           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9634           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9635             {
9636               g_signal_handler_block (widget, completion->priv->changed_id);
9637               g_signal_emit_by_name (completion, "match-selected",
9638                                      model, &iter, &entry_set);
9639               g_signal_handler_unblock (widget, completion->priv->changed_id);
9640
9641               if (!entry_set)
9642                 {
9643                   gchar *str = NULL;
9644
9645                   gtk_tree_model_get (model, &iter,
9646                                       completion->priv->text_column, &str,
9647                                       -1);
9648
9649                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9650
9651                   /* move the cursor to the end */
9652                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9653
9654                   g_free (str);
9655                 }
9656             }
9657           else
9658             retval = FALSE;
9659         }
9660       else if (completion->priv->current_selected - matches >= 0)
9661         {
9662           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9663           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9664             {
9665               GtkTreePath *path;
9666
9667               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9668               g_signal_emit_by_name (completion, "action-activated",
9669                                      gtk_tree_path_get_indices (path)[0]);
9670               gtk_tree_path_free (path);
9671             }
9672           else
9673             retval = FALSE;
9674         }
9675
9676       g_free (completion->priv->completion_prefix);
9677       completion->priv->completion_prefix = NULL;
9678
9679       return retval;
9680     }
9681
9682   return FALSE;
9683 }
9684
9685 static void
9686 gtk_entry_completion_changed (GtkWidget *widget,
9687                               gpointer   user_data)
9688 {
9689   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9690   GtkEntry *entry = GTK_ENTRY (widget);
9691   GtkEntryPrivate *priv = entry->priv;
9692   GdkDevice *device;
9693
9694   /* (re)install completion timeout */
9695   if (completion->priv->completion_timeout)
9696     g_source_remove (completion->priv->completion_timeout);
9697
9698   if (!gtk_entry_get_text (entry))
9699     return;
9700
9701   /* no need to normalize for this test */
9702   if (completion->priv->minimum_key_length > 0 &&
9703       strcmp ("", gtk_entry_get_text (entry)) == 0)
9704     {
9705       if (gtk_widget_get_visible (completion->priv->popup_window))
9706         _gtk_entry_completion_popdown (completion);
9707       return;
9708     }
9709
9710   device = gtk_get_current_event_device ();
9711
9712   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
9713     device = gdk_device_get_associated_device (device);
9714
9715   if (device)
9716     priv->completion_device = device;
9717
9718   completion->priv->completion_timeout =
9719     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9720                    gtk_entry_completion_timeout,
9721                    completion);
9722 }
9723
9724 static gboolean
9725 check_completion_callback (GtkEntryCompletion *completion)
9726 {
9727   completion->priv->check_completion_idle = NULL;
9728   
9729   gtk_entry_completion_complete (completion);
9730   gtk_entry_completion_insert_prefix (completion);
9731
9732   return FALSE;
9733 }
9734
9735 static void
9736 clear_completion_callback (GtkEntry   *entry,
9737                            GParamSpec *pspec)
9738 {
9739   if (pspec->name == I_("cursor-position") ||
9740       pspec->name == I_("selection-bound"))
9741     {
9742       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9743       
9744       completion->priv->has_completion = FALSE;
9745     }
9746 }
9747
9748 static gboolean
9749 accept_completion_callback (GtkEntry *entry)
9750 {
9751   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9752
9753   if (completion->priv->has_completion)
9754     gtk_editable_set_position (GTK_EDITABLE (entry),
9755                                gtk_entry_buffer_get_length (get_buffer (entry)));
9756
9757   return FALSE;
9758 }
9759
9760 static void
9761 completion_insert_text_callback (GtkEntry           *entry,
9762                                  const gchar        *text,
9763                                  gint                length,
9764                                  gint                position,
9765                                  GtkEntryCompletion *completion)
9766 {
9767   /* idle to update the selection based on the file list */
9768   if (completion->priv->check_completion_idle == NULL)
9769     {
9770       completion->priv->check_completion_idle = g_idle_source_new ();
9771       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9772       g_source_set_closure (completion->priv->check_completion_idle,
9773                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9774                                                    G_OBJECT (completion)));
9775       g_source_attach (completion->priv->check_completion_idle, NULL);
9776     }
9777 }
9778
9779 static void
9780 completion_changed (GtkEntryCompletion *completion,
9781                     GParamSpec         *pspec,
9782                     gpointer            data)
9783 {
9784   GtkEntry *entry = GTK_ENTRY (data);
9785
9786   if (pspec->name == I_("popup-completion") ||
9787       pspec->name == I_("inline-completion"))
9788     {
9789       disconnect_completion_signals (entry, completion);
9790       connect_completion_signals (entry, completion);
9791     }
9792 }
9793
9794 static void
9795 disconnect_completion_signals (GtkEntry           *entry,
9796                                GtkEntryCompletion *completion)
9797 {
9798   g_signal_handlers_disconnect_by_func (completion, 
9799                                        G_CALLBACK (completion_changed), entry);
9800   if (completion->priv->changed_id > 0 &&
9801       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9802     {
9803       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9804       completion->priv->changed_id = 0;
9805     }
9806   g_signal_handlers_disconnect_by_func (entry, 
9807                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9808   if (completion->priv->insert_text_id > 0 &&
9809       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9810     {
9811       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9812       completion->priv->insert_text_id = 0;
9813     }
9814   g_signal_handlers_disconnect_by_func (entry, 
9815                                         G_CALLBACK (completion_insert_text_callback), completion);
9816   g_signal_handlers_disconnect_by_func (entry, 
9817                                         G_CALLBACK (clear_completion_callback), completion);
9818   g_signal_handlers_disconnect_by_func (entry, 
9819                                         G_CALLBACK (accept_completion_callback), completion);
9820 }
9821
9822 static void
9823 connect_completion_signals (GtkEntry           *entry,
9824                             GtkEntryCompletion *completion)
9825 {
9826   if (completion->priv->popup_completion)
9827     {
9828       completion->priv->changed_id =
9829         g_signal_connect (entry, "changed",
9830                           G_CALLBACK (gtk_entry_completion_changed), completion);
9831       g_signal_connect (entry, "key-press-event",
9832                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9833     }
9834  
9835   if (completion->priv->inline_completion)
9836     {
9837       completion->priv->insert_text_id =
9838         g_signal_connect (entry, "insert-text",
9839                           G_CALLBACK (completion_insert_text_callback), completion);
9840       g_signal_connect (entry, "notify",
9841                         G_CALLBACK (clear_completion_callback), completion);
9842       g_signal_connect (entry, "activate",
9843                         G_CALLBACK (accept_completion_callback), completion);
9844       g_signal_connect (entry, "focus-out-event",
9845                         G_CALLBACK (accept_completion_callback), completion);
9846     }
9847
9848   g_signal_connect (completion, "notify",
9849                     G_CALLBACK (completion_changed), entry);
9850 }
9851
9852 /**
9853  * gtk_entry_set_completion:
9854  * @entry: A #GtkEntry
9855  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9856  *
9857  * Sets @completion to be the auxiliary completion object to use with @entry.
9858  * All further configuration of the completion mechanism is done on
9859  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9860  * @completion is set to %NULL.
9861  *
9862  * Since: 2.4
9863  */
9864 void
9865 gtk_entry_set_completion (GtkEntry           *entry,
9866                           GtkEntryCompletion *completion)
9867 {
9868   GtkEntryCompletion *old;
9869
9870   g_return_if_fail (GTK_IS_ENTRY (entry));
9871   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9872
9873   old = gtk_entry_get_completion (entry);
9874
9875   if (old == completion)
9876     return;
9877   
9878   if (old)
9879     {
9880       if (old->priv->completion_timeout)
9881         {
9882           g_source_remove (old->priv->completion_timeout);
9883           old->priv->completion_timeout = 0;
9884         }
9885
9886       if (gtk_widget_get_mapped (old->priv->popup_window))
9887         _gtk_entry_completion_popdown (old);
9888
9889       disconnect_completion_signals (entry, old);
9890       old->priv->entry = NULL;
9891
9892       g_object_unref (old);
9893     }
9894
9895   if (!completion)
9896     {
9897       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9898       return;
9899     }
9900
9901   /* hook into the entry */
9902   g_object_ref (completion);
9903
9904   connect_completion_signals (entry, completion);    
9905   completion->priv->entry = GTK_WIDGET (entry);
9906   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9907 }
9908
9909 /**
9910  * gtk_entry_get_completion:
9911  * @entry: A #GtkEntry
9912  *
9913  * Returns the auxiliary completion object currently in use by @entry.
9914  *
9915  * Return value: (transfer none): The auxiliary completion object currently
9916  *     in use by @entry.
9917  *
9918  * Since: 2.4
9919  */
9920 GtkEntryCompletion *
9921 gtk_entry_get_completion (GtkEntry *entry)
9922 {
9923   GtkEntryCompletion *completion;
9924
9925   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9926
9927   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9928                                      GTK_ENTRY_COMPLETION_KEY));
9929
9930   return completion;
9931 }
9932
9933 /**
9934  * gtk_entry_set_cursor_hadjustment:
9935  * @entry: a #GtkEntry
9936  * @adjustment: an adjustment which should be adjusted when the cursor 
9937  *              is moved, or %NULL
9938  *
9939  * Hooks up an adjustment to the cursor position in an entry, so that when 
9940  * the cursor is moved, the adjustment is scrolled to show that position. 
9941  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9942  * the adjustment.
9943  *
9944  * The adjustment has to be in pixel units and in the same coordinate system 
9945  * as the entry. 
9946  * 
9947  * Since: 2.12
9948  */
9949 void
9950 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9951                                   GtkAdjustment *adjustment)
9952 {
9953   g_return_if_fail (GTK_IS_ENTRY (entry));
9954   if (adjustment)
9955     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9956
9957   if (adjustment)
9958     g_object_ref (adjustment);
9959
9960   g_object_set_qdata_full (G_OBJECT (entry), 
9961                            quark_cursor_hadjustment,
9962                            adjustment, 
9963                            g_object_unref);
9964 }
9965
9966 /**
9967  * gtk_entry_get_cursor_hadjustment:
9968  * @entry: a #GtkEntry
9969  *
9970  * Retrieves the horizontal cursor adjustment for the entry. 
9971  * See gtk_entry_set_cursor_hadjustment().
9972  *
9973  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9974  *   if none has been set.
9975  *
9976  * Since: 2.12
9977  */
9978 GtkAdjustment*
9979 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9980 {
9981   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9982
9983   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9984 }
9985
9986 /**
9987  * gtk_entry_set_progress_fraction:
9988  * @entry: a #GtkEntry
9989  * @fraction: fraction of the task that's been completed
9990  *
9991  * Causes the entry's progress indicator to "fill in" the given
9992  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9993  * inclusive.
9994  *
9995  * Since: 2.16
9996  */
9997 void
9998 gtk_entry_set_progress_fraction (GtkEntry *entry,
9999                                  gdouble   fraction)
10000 {
10001   GtkWidget       *widget;
10002   GtkEntryPrivate *private;
10003   gdouble          old_fraction;
10004   gint x, y, width, height;
10005   gint old_x, old_y, old_width, old_height;
10006
10007   g_return_if_fail (GTK_IS_ENTRY (entry));
10008
10009   widget = GTK_WIDGET (entry);
10010   private = entry->priv;
10011
10012   if (private->progress_pulse_mode)
10013     old_fraction = -1;
10014   else
10015     old_fraction = private->progress_fraction;
10016
10017   if (gtk_widget_is_drawable (widget))
10018     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
10019
10020   fraction = CLAMP (fraction, 0.0, 1.0);
10021
10022   private->progress_fraction = fraction;
10023   private->progress_pulse_mode = FALSE;
10024   private->progress_pulse_current = 0.0;
10025
10026   if (gtk_widget_is_drawable (widget))
10027     {
10028       get_progress_area (widget, &x, &y, &width, &height);
10029
10030       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
10031         gtk_widget_queue_draw (widget);
10032     }
10033
10034   if (fraction != old_fraction)
10035     g_object_notify (G_OBJECT (entry), "progress-fraction");
10036 }
10037
10038 /**
10039  * gtk_entry_get_progress_fraction:
10040  * @entry: a #GtkEntry
10041  *
10042  * Returns the current fraction of the task that's been completed.
10043  * See gtk_entry_set_progress_fraction().
10044  *
10045  * Return value: a fraction from 0.0 to 1.0
10046  *
10047  * Since: 2.16
10048  */
10049 gdouble
10050 gtk_entry_get_progress_fraction (GtkEntry *entry)
10051 {
10052   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10053
10054   return entry->priv->progress_fraction;
10055 }
10056
10057 /**
10058  * gtk_entry_set_progress_pulse_step:
10059  * @entry: a #GtkEntry
10060  * @fraction: fraction between 0.0 and 1.0
10061  *
10062  * Sets the fraction of total entry width to move the progress
10063  * bouncing block for each call to gtk_entry_progress_pulse().
10064  *
10065  * Since: 2.16
10066  */
10067 void
10068 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
10069                                    gdouble   fraction)
10070 {
10071   GtkEntryPrivate *private;
10072
10073   g_return_if_fail (GTK_IS_ENTRY (entry));
10074
10075   private = entry->priv;
10076
10077   fraction = CLAMP (fraction, 0.0, 1.0);
10078
10079   if (fraction != private->progress_pulse_fraction)
10080     {
10081       private->progress_pulse_fraction = fraction;
10082
10083       gtk_widget_queue_draw (GTK_WIDGET (entry));
10084
10085       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
10086     }
10087 }
10088
10089 /**
10090  * gtk_entry_get_progress_pulse_step:
10091  * @entry: a #GtkEntry
10092  *
10093  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
10094  *
10095  * Return value: a fraction from 0.0 to 1.0
10096  *
10097  * Since: 2.16
10098  */
10099 gdouble
10100 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
10101 {
10102   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10103
10104   return entry->priv->progress_pulse_fraction;
10105 }
10106
10107 /**
10108  * gtk_entry_progress_pulse:
10109  * @entry: a #GtkEntry
10110  *
10111  * Indicates that some progress is made, but you don't know how much.
10112  * Causes the entry's progress indicator to enter "activity mode,"
10113  * where a block bounces back and forth. Each call to
10114  * gtk_entry_progress_pulse() causes the block to move by a little bit
10115  * (the amount of movement per pulse is determined by
10116  * gtk_entry_set_progress_pulse_step()).
10117  *
10118  * Since: 2.16
10119  */
10120 void
10121 gtk_entry_progress_pulse (GtkEntry *entry)
10122 {
10123   GtkEntryPrivate *private;
10124
10125   g_return_if_fail (GTK_IS_ENTRY (entry));
10126
10127   private = entry->priv;
10128
10129   if (private->progress_pulse_mode)
10130     {
10131       if (private->progress_pulse_way_back)
10132         {
10133           private->progress_pulse_current -= private->progress_pulse_fraction;
10134
10135           if (private->progress_pulse_current < 0.0)
10136             {
10137               private->progress_pulse_current = 0.0;
10138               private->progress_pulse_way_back = FALSE;
10139             }
10140         }
10141       else
10142         {
10143           private->progress_pulse_current += private->progress_pulse_fraction;
10144
10145           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10146             {
10147               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10148               private->progress_pulse_way_back = TRUE;
10149             }
10150         }
10151     }
10152   else
10153     {
10154       private->progress_fraction = 0.0;
10155       private->progress_pulse_mode = TRUE;
10156       private->progress_pulse_way_back = FALSE;
10157       private->progress_pulse_current = 0.0;
10158     }
10159
10160   gtk_widget_queue_draw (GTK_WIDGET (entry));
10161 }
10162
10163 /* Caps Lock warning for password entries */
10164
10165 static void
10166 show_capslock_feedback (GtkEntry    *entry,
10167                         const gchar *text)
10168 {
10169   GtkEntryPrivate *priv = entry->priv;
10170
10171   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10172     {
10173       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10174       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10175       priv->caps_lock_warning_shown = TRUE;
10176     }
10177
10178   if (priv->caps_lock_warning_shown)
10179     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10180   else
10181     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10182 }
10183
10184 static void
10185 remove_capslock_feedback (GtkEntry *entry)
10186 {
10187   GtkEntryPrivate *priv = entry->priv;
10188
10189   if (priv->caps_lock_warning_shown)
10190     {
10191       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10192       priv->caps_lock_warning_shown = FALSE;
10193     } 
10194 }
10195
10196 static void
10197 keymap_state_changed (GdkKeymap *keymap, 
10198                       GtkEntry  *entry)
10199 {
10200   GtkEntryPrivate *priv = entry->priv;
10201   char *text = NULL;
10202
10203   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10204     { 
10205       if (gdk_keymap_get_num_lock_state (keymap)
10206           && gdk_keymap_get_caps_lock_state (keymap))
10207         text = _("Caps Lock and Num Lock are on");
10208       else if (gdk_keymap_get_num_lock_state (keymap))
10209         text = _("Num Lock is on");
10210       else if (gdk_keymap_get_caps_lock_state (keymap))
10211         text = _("Caps Lock is on");
10212     }
10213
10214   if (text)
10215     show_capslock_feedback (entry, text);
10216   else
10217     remove_capslock_feedback (entry);
10218 }