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