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