]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
Move docs inline
[~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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <config.h>
29 #include <string.h>
30
31 #include <pango/pango.h>
32
33 #include "gdk/gdkkeysyms.h"
34 #include "gtkbindings.h"
35 #include "gtkcelleditable.h"
36 #include "gtkclipboard.h"
37 #include "gtkdnd.h"
38 #include "gtkentry.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkimcontextsimple.h"
41 #include "gtkimmulticontext.h"
42 #include "gtkintl.h"
43 #include "gtkmain.h"
44 #include "gtkmarshalers.h"
45 #include "gtkmenu.h"
46 #include "gtkmenuitem.h"
47 #include "gtkseparatormenuitem.h"
48 #include "gtkselection.h"
49 #include "gtksettings.h"
50 #include "gtkstock.h"
51 #include "gtktextutil.h"
52 #include "gtkwindow.h"
53 #include "gtktreeview.h"
54 #include "gtktreeselection.h"
55 #include "gtkprivate.h"
56 #include "gtkentryprivate.h"
57 #include "gtkcelllayout.h"
58 #include "gtkalias.h"
59
60 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
61
62 #define MIN_ENTRY_WIDTH  150
63 #define DRAW_TIMEOUT     20
64 #define COMPLETION_TIMEOUT 300
65 #define PASSWORD_HINT_MAX 8
66
67 /* Initial size of buffer, in bytes */
68 #define MIN_SIZE 16
69
70 /* Maximum size of text buffer, in bytes */
71 #define MAX_SIZE G_MAXUSHORT
72
73 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
74 static GQuark          quark_inner_border   = 0;
75 static GQuark          quark_password_hint  = 0;
76
77 typedef struct _GtkEntryPrivate GtkEntryPrivate;
78
79 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
80
81 struct _GtkEntryPrivate 
82 {
83   gfloat xalign;
84   gint insert_pos;
85   guint blink_time;  /* time in msec the cursor has blinked since last user event */
86   guint real_changed : 1;
87   guint change_count : 8;
88
89   gint focus_width;
90   gboolean interior_focus;
91   GtkShadowType shadow_type;
92
93   GtkAdjustment *cursor_hadjustment;
94 };
95
96 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
97
98 struct _GtkEntryPasswordHint
99 {
100   gchar password_hint[PASSWORD_HINT_MAX];
101   guint password_hint_timeout_id;
102   gint  password_hint_length;
103   gint  password_hint_position;
104 };
105
106 enum {
107   ACTIVATE,
108   POPULATE_POPUP,
109   MOVE_CURSOR,
110   INSERT_AT_CURSOR,
111   DELETE_FROM_CURSOR,
112   BACKSPACE,
113   CUT_CLIPBOARD,
114   COPY_CLIPBOARD,
115   PASTE_CLIPBOARD,
116   TOGGLE_OVERWRITE,
117   LAST_SIGNAL
118 };
119
120 enum {
121   PROP_0,
122   PROP_CURSOR_POSITION,
123   PROP_SELECTION_BOUND,
124   PROP_EDITABLE,
125   PROP_MAX_LENGTH,
126   PROP_VISIBILITY,
127   PROP_HAS_FRAME,
128   PROP_INNER_BORDER,
129   PROP_INVISIBLE_CHAR,
130   PROP_ACTIVATES_DEFAULT,
131   PROP_WIDTH_CHARS,
132   PROP_SCROLL_OFFSET,
133   PROP_TEXT,
134   PROP_XALIGN,
135   PROP_TRUNCATE_MULTILINE,
136   PROP_SHADOW_TYPE
137 };
138
139 static guint signals[LAST_SIGNAL] = { 0 };
140
141 typedef enum {
142   CURSOR_STANDARD,
143   CURSOR_DND
144 } CursorType;
145
146 /* GObject, GtkObject methods
147  */
148 static void   gtk_entry_editable_init        (GtkEditableClass     *iface);
149 static void   gtk_entry_cell_editable_init   (GtkCellEditableIface *iface);
150 static void   gtk_entry_set_property         (GObject          *object,
151                                               guint             prop_id,
152                                               const GValue     *value,
153                                               GParamSpec       *pspec);
154 static void   gtk_entry_get_property         (GObject          *object,
155                                               guint             prop_id,
156                                               GValue           *value,
157                                               GParamSpec       *pspec);
158 static void   gtk_entry_finalize             (GObject          *object);
159 static void   gtk_entry_destroy              (GtkObject        *object);
160
161 /* GtkWidget methods
162  */
163 static void   gtk_entry_realize              (GtkWidget        *widget);
164 static void   gtk_entry_unrealize            (GtkWidget        *widget);
165 static void   gtk_entry_size_request         (GtkWidget        *widget,
166                                               GtkRequisition   *requisition);
167 static void   gtk_entry_size_allocate        (GtkWidget        *widget,
168                                               GtkAllocation    *allocation);
169 static void   gtk_entry_draw_frame           (GtkWidget        *widget,
170                                               GdkRectangle     *area);
171 static gint   gtk_entry_expose               (GtkWidget        *widget,
172                                               GdkEventExpose   *event);
173 static gint   gtk_entry_button_press         (GtkWidget        *widget,
174                                               GdkEventButton   *event);
175 static gint   gtk_entry_button_release       (GtkWidget        *widget,
176                                               GdkEventButton   *event);
177 static gint   gtk_entry_motion_notify        (GtkWidget        *widget,
178                                               GdkEventMotion   *event);
179 static gint   gtk_entry_key_press            (GtkWidget        *widget,
180                                               GdkEventKey      *event);
181 static gint   gtk_entry_key_release          (GtkWidget        *widget,
182                                               GdkEventKey      *event);
183 static gint   gtk_entry_focus_in             (GtkWidget        *widget,
184                                               GdkEventFocus    *event);
185 static gint   gtk_entry_focus_out            (GtkWidget        *widget,
186                                               GdkEventFocus    *event);
187 static void   gtk_entry_grab_focus           (GtkWidget        *widget);
188 static void   gtk_entry_style_set            (GtkWidget        *widget,
189                                               GtkStyle         *previous_style);
190 static void   gtk_entry_direction_changed    (GtkWidget        *widget,
191                                               GtkTextDirection  previous_dir);
192 static void   gtk_entry_state_changed        (GtkWidget        *widget,
193                                               GtkStateType      previous_state);
194 static void   gtk_entry_screen_changed       (GtkWidget        *widget,
195                                               GdkScreen        *old_screen);
196
197 static gboolean gtk_entry_drag_drop          (GtkWidget        *widget,
198                                               GdkDragContext   *context,
199                                               gint              x,
200                                               gint              y,
201                                               guint             time);
202 static gboolean gtk_entry_drag_motion        (GtkWidget        *widget,
203                                               GdkDragContext   *context,
204                                               gint              x,
205                                               gint              y,
206                                               guint             time);
207 static void     gtk_entry_drag_leave         (GtkWidget        *widget,
208                                               GdkDragContext   *context,
209                                               guint             time);
210 static void     gtk_entry_drag_data_received (GtkWidget        *widget,
211                                               GdkDragContext   *context,
212                                               gint              x,
213                                               gint              y,
214                                               GtkSelectionData *selection_data,
215                                               guint             info,
216                                               guint             time);
217 static void     gtk_entry_drag_data_get      (GtkWidget        *widget,
218                                               GdkDragContext   *context,
219                                               GtkSelectionData *selection_data,
220                                               guint             info,
221                                               guint             time);
222 static void     gtk_entry_drag_data_delete   (GtkWidget        *widget,
223                                               GdkDragContext   *context);
224
225 /* GtkEditable method implementations
226  */
227 static void     gtk_entry_insert_text          (GtkEditable *editable,
228                                                 const gchar *new_text,
229                                                 gint         new_text_length,
230                                                 gint        *position);
231 static void     gtk_entry_delete_text          (GtkEditable *editable,
232                                                 gint         start_pos,
233                                                 gint         end_pos);
234 static gchar *  gtk_entry_get_chars            (GtkEditable *editable,
235                                                 gint         start_pos,
236                                                 gint         end_pos);
237 static void     gtk_entry_real_set_position    (GtkEditable *editable,
238                                                 gint         position);
239 static gint     gtk_entry_get_position         (GtkEditable *editable);
240 static void     gtk_entry_set_selection_bounds (GtkEditable *editable,
241                                                 gint         start,
242                                                 gint         end);
243 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
244                                                 gint        *start,
245                                                 gint        *end);
246
247 /* GtkCellEditable method implementations
248  */
249 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
250                                      GdkEvent        *event);
251
252 /* Default signal handlers
253  */
254 static void gtk_entry_real_insert_text   (GtkEditable     *editable,
255                                           const gchar     *new_text,
256                                           gint             new_text_length,
257                                           gint            *position);
258 static void gtk_entry_real_delete_text   (GtkEditable     *editable,
259                                           gint             start_pos,
260                                           gint             end_pos);
261 static void gtk_entry_move_cursor        (GtkEntry        *entry,
262                                           GtkMovementStep  step,
263                                           gint             count,
264                                           gboolean         extend_selection);
265 static void gtk_entry_insert_at_cursor   (GtkEntry        *entry,
266                                           const gchar     *str);
267 static void gtk_entry_delete_from_cursor (GtkEntry        *entry,
268                                           GtkDeleteType    type,
269                                           gint             count);
270 static void gtk_entry_backspace          (GtkEntry        *entry);
271 static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
272 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
273 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
274 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
275 static void gtk_entry_select_all         (GtkEntry        *entry);
276 static void gtk_entry_real_activate      (GtkEntry        *entry);
277 static gboolean gtk_entry_popup_menu     (GtkWidget      *widget);
278
279 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
280                                                 GtkEntry  *entry);
281 /* IM Context Callbacks
282  */
283 static void     gtk_entry_commit_cb               (GtkIMContext *context,
284                                                    const gchar  *str,
285                                                    GtkEntry     *entry);
286 static void     gtk_entry_preedit_changed_cb      (GtkIMContext *context,
287                                                    GtkEntry     *entry);
288 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
289                                                    GtkEntry     *entry);
290 static gboolean gtk_entry_delete_surrounding_cb   (GtkIMContext *context,
291                                                    gint          offset,
292                                                    gint          n_chars,
293                                                    GtkEntry     *entry);
294
295 /* Internal routines
296  */
297 static void         gtk_entry_enter_text               (GtkEntry       *entry,
298                                                         const gchar    *str);
299 static void         gtk_entry_set_positions            (GtkEntry       *entry,
300                                                         gint            current_pos,
301                                                         gint            selection_bound);
302 static void         gtk_entry_draw_text                (GtkEntry       *entry);
303 static void         gtk_entry_draw_cursor              (GtkEntry       *entry,
304                                                         CursorType      type);
305 static PangoLayout *gtk_entry_ensure_layout            (GtkEntry       *entry,
306                                                         gboolean        include_preedit);
307 static void         gtk_entry_reset_layout             (GtkEntry       *entry);
308 static void         gtk_entry_queue_draw               (GtkEntry       *entry);
309 static void         gtk_entry_recompute                (GtkEntry       *entry);
310 static gint         gtk_entry_find_position            (GtkEntry       *entry,
311                                                         gint            x);
312 static void         gtk_entry_get_cursor_locations     (GtkEntry       *entry,
313                                                         CursorType      type,
314                                                         gint           *strong_x,
315                                                         gint           *weak_x);
316 static void         gtk_entry_adjust_scroll            (GtkEntry       *entry);
317 static gint         gtk_entry_move_visually            (GtkEntry       *editable,
318                                                         gint            start,
319                                                         gint            count);
320 static gint         gtk_entry_move_logically           (GtkEntry       *entry,
321                                                         gint            start,
322                                                         gint            count);
323 static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
324                                                         gint            start,
325                                                         gboolean        allow_whitespace);
326 static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
327                                                         gint            start,
328                                                         gboolean        allow_whitespace);
329 static void         gtk_entry_delete_whitespace        (GtkEntry       *entry);
330 static void         gtk_entry_select_word              (GtkEntry       *entry);
331 static void         gtk_entry_select_line              (GtkEntry       *entry);
332 static char *       gtk_entry_get_public_chars         (GtkEntry       *entry,
333                                                         gint            start,
334                                                         gint            end);
335 static void         gtk_entry_paste                    (GtkEntry       *entry,
336                                                         GdkAtom         selection);
337 static void         gtk_entry_update_primary_selection (GtkEntry       *entry);
338 static void         gtk_entry_do_popup                 (GtkEntry       *entry,
339                                                         GdkEventButton *event);
340 static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
341                                                         gboolean        group_cycling);
342 static void         gtk_entry_state_changed            (GtkWidget      *widget,
343                                                         GtkStateType    previous_state);
344 static void         gtk_entry_check_cursor_blink       (GtkEntry       *entry);
345 static void         gtk_entry_pend_cursor_blink        (GtkEntry       *entry);
346 static void         gtk_entry_reset_blink_time         (GtkEntry       *entry);
347 static void         get_text_area_size                 (GtkEntry       *entry,
348                                                         gint           *x,
349                                                         gint           *y,
350                                                         gint           *width,
351                                                         gint           *height);
352 static void         get_widget_window_size             (GtkEntry       *entry,
353                                                         gint           *x,
354                                                         gint           *y,
355                                                         gint           *width,
356                                                         gint           *height);
357 static void         gtk_entry_move_adjustments         (GtkEntry       *entry);
358
359 /* Completion */
360 static gint         gtk_entry_completion_timeout       (gpointer            data);
361 static gboolean     gtk_entry_completion_key_press     (GtkWidget          *widget,
362                                                         GdkEventKey        *event,
363                                                         gpointer            user_data);
364 static void         gtk_entry_completion_changed       (GtkWidget          *entry,
365                                                         gpointer            user_data);
366 static gboolean     check_completion_callback          (GtkEntryCompletion *completion);
367 static void         clear_completion_callback          (GtkEntry           *entry,
368                                                         GParamSpec         *pspec);
369 static gboolean     accept_completion_callback         (GtkEntry           *entry);
370 static void         completion_insert_text_callback    (GtkEntry           *entry,
371                                                         const gchar        *text,
372                                                         gint                length,
373                                                         gint                position,
374                                                         GtkEntryCompletion *completion);
375 static void         completion_changed                 (GtkEntryCompletion *completion,
376                                                         GParamSpec         *pspec,
377                                                         gpointer            data);
378 static void         disconnect_completion_signals      (GtkEntry           *entry,
379                                                         GtkEntryCompletion *completion);
380 static void         connect_completion_signals         (GtkEntry           *entry,
381                                                         GtkEntryCompletion *completion);
382
383 static void         begin_change                       (GtkEntry *entry);
384 static void         end_change                         (GtkEntry *entry);
385 static void         emit_changed                       (GtkEntry *entry);
386
387 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
388                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
389                                                 gtk_entry_editable_init)
390                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
391                                                 gtk_entry_cell_editable_init))
392
393 static void
394 add_move_binding (GtkBindingSet  *binding_set,
395                   guint           keyval,
396                   guint           modmask,
397                   GtkMovementStep step,
398                   gint            count)
399 {
400   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
401   
402   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
403                                 "move_cursor", 3,
404                                 G_TYPE_ENUM, step,
405                                 G_TYPE_INT, count,
406                                 G_TYPE_BOOLEAN, FALSE);
407
408   /* Selection-extending version */
409   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
410                                 "move_cursor", 3,
411                                 G_TYPE_ENUM, step,
412                                 G_TYPE_INT, count,
413                                 G_TYPE_BOOLEAN, TRUE);
414 }
415
416 static void
417 gtk_entry_class_init (GtkEntryClass *class)
418 {
419   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
420   GtkWidgetClass *widget_class;
421   GtkObjectClass *gtk_object_class;
422   GtkBindingSet *binding_set;
423
424   widget_class = (GtkWidgetClass*) class;
425   gtk_object_class = (GtkObjectClass *)class;
426
427   gobject_class->finalize = gtk_entry_finalize;
428   gobject_class->set_property = gtk_entry_set_property;
429   gobject_class->get_property = gtk_entry_get_property;
430
431   widget_class->realize = gtk_entry_realize;
432   widget_class->unrealize = gtk_entry_unrealize;
433   widget_class->size_request = gtk_entry_size_request;
434   widget_class->size_allocate = gtk_entry_size_allocate;
435   widget_class->expose_event = gtk_entry_expose;
436   widget_class->button_press_event = gtk_entry_button_press;
437   widget_class->button_release_event = gtk_entry_button_release;
438   widget_class->motion_notify_event = gtk_entry_motion_notify;
439   widget_class->key_press_event = gtk_entry_key_press;
440   widget_class->key_release_event = gtk_entry_key_release;
441   widget_class->focus_in_event = gtk_entry_focus_in;
442   widget_class->focus_out_event = gtk_entry_focus_out;
443   widget_class->grab_focus = gtk_entry_grab_focus;
444   widget_class->style_set = gtk_entry_style_set;
445   widget_class->direction_changed = gtk_entry_direction_changed;
446   widget_class->state_changed = gtk_entry_state_changed;
447   widget_class->screen_changed = gtk_entry_screen_changed;
448   widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
449
450   widget_class->drag_drop = gtk_entry_drag_drop;
451   widget_class->drag_motion = gtk_entry_drag_motion;
452   widget_class->drag_leave = gtk_entry_drag_leave;
453   widget_class->drag_data_received = gtk_entry_drag_data_received;
454   widget_class->drag_data_get = gtk_entry_drag_data_get;
455   widget_class->drag_data_delete = gtk_entry_drag_data_delete;
456
457   widget_class->popup_menu = gtk_entry_popup_menu;
458
459   gtk_object_class->destroy = gtk_entry_destroy;
460
461   class->move_cursor = gtk_entry_move_cursor;
462   class->insert_at_cursor = gtk_entry_insert_at_cursor;
463   class->delete_from_cursor = gtk_entry_delete_from_cursor;
464   class->backspace = gtk_entry_backspace;
465   class->cut_clipboard = gtk_entry_cut_clipboard;
466   class->copy_clipboard = gtk_entry_copy_clipboard;
467   class->paste_clipboard = gtk_entry_paste_clipboard;
468   class->toggle_overwrite = gtk_entry_toggle_overwrite;
469   class->activate = gtk_entry_real_activate;
470   
471   quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
472   quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
473
474   g_object_class_install_property (gobject_class,
475                                    PROP_CURSOR_POSITION,
476                                    g_param_spec_int ("cursor-position",
477                                                      P_("Cursor Position"),
478                                                      P_("The current position of the insertion cursor in chars"),
479                                                      0,
480                                                      MAX_SIZE,
481                                                      0,
482                                                      GTK_PARAM_READABLE));
483   
484   g_object_class_install_property (gobject_class,
485                                    PROP_SELECTION_BOUND,
486                                    g_param_spec_int ("selection-bound",
487                                                      P_("Selection Bound"),
488                                                      P_("The position of the opposite end of the selection from the cursor in chars"),
489                                                      0,
490                                                      MAX_SIZE,
491                                                      0,
492                                                      GTK_PARAM_READABLE));
493   
494   g_object_class_install_property (gobject_class,
495                                    PROP_EDITABLE,
496                                    g_param_spec_boolean ("editable",
497                                                          P_("Editable"),
498                                                          P_("Whether the entry contents can be edited"),
499                                                          TRUE,
500                                                          GTK_PARAM_READWRITE));
501   
502   g_object_class_install_property (gobject_class,
503                                    PROP_MAX_LENGTH,
504                                    g_param_spec_int ("max-length",
505                                                      P_("Maximum length"),
506                                                      P_("Maximum number of characters for this entry. Zero if no maximum"),
507                                                      0,
508                                                      MAX_SIZE,
509                                                      0,
510                                                      GTK_PARAM_READWRITE));
511   g_object_class_install_property (gobject_class,
512                                    PROP_VISIBILITY,
513                                    g_param_spec_boolean ("visibility",
514                                                          P_("Visibility"),
515                                                          P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
516                                                          TRUE,
517                                                          GTK_PARAM_READWRITE));
518
519   g_object_class_install_property (gobject_class,
520                                    PROP_HAS_FRAME,
521                                    g_param_spec_boolean ("has-frame",
522                                                          P_("Has Frame"),
523                                                          P_("FALSE removes outside bevel from entry"),
524                                                          TRUE,
525                                                          GTK_PARAM_READWRITE));
526
527   g_object_class_install_property (gobject_class,
528                                    PROP_INNER_BORDER,
529                                    g_param_spec_boxed ("inner-border",
530                                                        P_("Inner Border"),
531                                                        P_("Border between text and frame. Overrides the inner-border style property"),
532                                                        GTK_TYPE_BORDER,
533                                                        GTK_PARAM_READWRITE));
534
535   g_object_class_install_property (gobject_class,
536                                    PROP_INVISIBLE_CHAR,
537                                    g_param_spec_unichar ("invisible-char",
538                                                          P_("Invisible character"),
539                                                          P_("The character to use when masking entry contents (in \"password mode\")"),
540                                                          '*',
541                                                          GTK_PARAM_READWRITE));
542
543   g_object_class_install_property (gobject_class,
544                                    PROP_ACTIVATES_DEFAULT,
545                                    g_param_spec_boolean ("activates-default",
546                                                          P_("Activates default"),
547                                                          P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
548                                                          FALSE,
549                                                          GTK_PARAM_READWRITE));
550   g_object_class_install_property (gobject_class,
551                                    PROP_WIDTH_CHARS,
552                                    g_param_spec_int ("width-chars",
553                                                      P_("Width in chars"),
554                                                      P_("Number of characters to leave space for in the entry"),
555                                                      -1,
556                                                      G_MAXINT,
557                                                      -1,
558                                                      GTK_PARAM_READWRITE));
559
560   g_object_class_install_property (gobject_class,
561                                    PROP_SCROLL_OFFSET,
562                                    g_param_spec_int ("scroll-offset",
563                                                      P_("Scroll offset"),
564                                                      P_("Number of pixels of the entry scrolled off the screen to the left"),
565                                                      0,
566                                                      G_MAXINT,
567                                                      0,
568                                                      GTK_PARAM_READABLE));
569
570   g_object_class_install_property (gobject_class,
571                                    PROP_TEXT,
572                                    g_param_spec_string ("text",
573                                                         P_("Text"),
574                                                         P_("The contents of the entry"),
575                                                         "",
576                                                         GTK_PARAM_READWRITE));
577
578   /**
579    * GtkEntry:xalign:
580    *
581    * The horizontal alignment, from 0 (left) to 1 (right). 
582    * Reversed for RTL layouts.
583    * 
584    * Since: 2.4
585    */
586   g_object_class_install_property (gobject_class,
587                                    PROP_XALIGN,
588                                    g_param_spec_float ("xalign",
589                                                        P_("X align"),
590                                                        P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
591                                                        0.0,
592                                                        1.0,
593                                                        0.0,
594                                                        GTK_PARAM_READWRITE));
595
596   /**
597    * GtkEntry:truncate-multiline:
598    *
599    * When %TRUE, pasted multi-line text is truncated to the first line.
600    *
601    * Since: 2.10
602    */
603   g_object_class_install_property (gobject_class,
604                                    PROP_TRUNCATE_MULTILINE,
605                                    g_param_spec_boolean ("truncate-multiline",
606                                                          P_("Truncate multiline"),
607                                                          P_("Whether to truncate multiline pastes to one line."),
608                                                          FALSE,
609                                                          GTK_PARAM_READWRITE));
610
611   /**
612    * GtkEntry:shadow-type:
613    *
614    * Which kind of shadow to draw around the entry when 
615    * #GtkEntry:has-frame is set to %TRUE.
616    *
617    * Since: 2.12
618    */
619   g_object_class_install_property (gobject_class,
620                                    PROP_SHADOW_TYPE,
621                                    g_param_spec_enum ("shadow-type",
622                                                       P_("Shadow type"),
623                                                       P_("Which kind of shadow to draw around the entry when has-frame is set"),
624                                                       GTK_TYPE_SHADOW_TYPE,
625                                                       GTK_SHADOW_IN,
626                                                       GTK_PARAM_READWRITE));
627
628   signals[POPULATE_POPUP] =
629     g_signal_new (I_("populate_popup"),
630                   G_OBJECT_CLASS_TYPE (gobject_class),
631                   G_SIGNAL_RUN_LAST,
632                   G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
633                   NULL, NULL,
634                   _gtk_marshal_VOID__OBJECT,
635                   G_TYPE_NONE, 1,
636                   GTK_TYPE_MENU);
637   
638  /* Action signals */
639   
640   signals[ACTIVATE] =
641     g_signal_new (I_("activate"),
642                   G_OBJECT_CLASS_TYPE (gobject_class),
643                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
644                   G_STRUCT_OFFSET (GtkEntryClass, activate),
645                   NULL, NULL,
646                   _gtk_marshal_VOID__VOID,
647                   G_TYPE_NONE, 0);
648   widget_class->activate_signal = signals[ACTIVATE];
649
650   signals[MOVE_CURSOR] = 
651     g_signal_new (I_("move_cursor"),
652                   G_OBJECT_CLASS_TYPE (gobject_class),
653                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
654                   G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
655                   NULL, NULL,
656                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
657                   G_TYPE_NONE, 3,
658                   GTK_TYPE_MOVEMENT_STEP,
659                   G_TYPE_INT,
660                   G_TYPE_BOOLEAN);
661
662   signals[INSERT_AT_CURSOR] = 
663     g_signal_new (I_("insert_at_cursor"),
664                   G_OBJECT_CLASS_TYPE (gobject_class),
665                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
666                   G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
667                   NULL, NULL,
668                   _gtk_marshal_VOID__STRING,
669                   G_TYPE_NONE, 1,
670                   G_TYPE_STRING);
671
672   signals[DELETE_FROM_CURSOR] = 
673     g_signal_new (I_("delete_from_cursor"),
674                   G_OBJECT_CLASS_TYPE (gobject_class),
675                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
676                   G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
677                   NULL, NULL,
678                   _gtk_marshal_VOID__ENUM_INT,
679                   G_TYPE_NONE, 2,
680                   GTK_TYPE_DELETE_TYPE,
681                   G_TYPE_INT);
682
683   signals[BACKSPACE] =
684     g_signal_new (I_("backspace"),
685                   G_OBJECT_CLASS_TYPE (gobject_class),
686                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
687                   G_STRUCT_OFFSET (GtkEntryClass, backspace),
688                   NULL, NULL,
689                   _gtk_marshal_VOID__VOID,
690                   G_TYPE_NONE, 0);
691
692   signals[CUT_CLIPBOARD] =
693     g_signal_new (I_("cut_clipboard"),
694                   G_OBJECT_CLASS_TYPE (gobject_class),
695                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
696                   G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
697                   NULL, NULL,
698                   _gtk_marshal_VOID__VOID,
699                   G_TYPE_NONE, 0);
700
701   signals[COPY_CLIPBOARD] =
702     g_signal_new (I_("copy_clipboard"),
703                   G_OBJECT_CLASS_TYPE (gobject_class),
704                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
705                   G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
706                   NULL, NULL,
707                   _gtk_marshal_VOID__VOID,
708                   G_TYPE_NONE, 0);
709
710   signals[PASTE_CLIPBOARD] =
711     g_signal_new (I_("paste_clipboard"),
712                   G_OBJECT_CLASS_TYPE (gobject_class),
713                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714                   G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
715                   NULL, NULL,
716                   _gtk_marshal_VOID__VOID,
717                   G_TYPE_NONE, 0);
718
719   signals[TOGGLE_OVERWRITE] =
720     g_signal_new (I_("toggle_overwrite"),
721                   G_OBJECT_CLASS_TYPE (gobject_class),
722                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
723                   G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
724                   NULL, NULL,
725                   _gtk_marshal_VOID__VOID,
726                   G_TYPE_NONE, 0);
727
728   /*
729    * Key bindings
730    */
731
732   binding_set = gtk_binding_set_by_class (class);
733
734   /* Moving the insertion point */
735   add_move_binding (binding_set, GDK_Right, 0,
736                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
737   
738   add_move_binding (binding_set, GDK_Left, 0,
739                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
740
741   add_move_binding (binding_set, GDK_KP_Right, 0,
742                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
743   
744   add_move_binding (binding_set, GDK_KP_Left, 0,
745                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
746   
747   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
748                     GTK_MOVEMENT_WORDS, 1);
749
750   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
751                     GTK_MOVEMENT_WORDS, -1);
752
753   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
754                     GTK_MOVEMENT_WORDS, 1);
755
756   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
757                     GTK_MOVEMENT_WORDS, -1);
758   
759   add_move_binding (binding_set, GDK_Home, 0,
760                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
761
762   add_move_binding (binding_set, GDK_End, 0,
763                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
764
765   add_move_binding (binding_set, GDK_KP_Home, 0,
766                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
767
768   add_move_binding (binding_set, GDK_KP_End, 0,
769                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
770   
771   add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
772                     GTK_MOVEMENT_BUFFER_ENDS, -1);
773
774   add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
775                     GTK_MOVEMENT_BUFFER_ENDS, 1);
776
777   add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
778                     GTK_MOVEMENT_BUFFER_ENDS, -1);
779
780   add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
781                     GTK_MOVEMENT_BUFFER_ENDS, 1);
782
783   /* Select all
784    */
785   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
786                                 "move_cursor", 3,
787                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
788                                 G_TYPE_INT, -1,
789                                 G_TYPE_BOOLEAN, FALSE);
790   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
791                                 "move_cursor", 3,
792                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
793                                 G_TYPE_INT, 1,
794                                 G_TYPE_BOOLEAN, TRUE);  
795
796   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
797                                 "move_cursor", 3,
798                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
799                                 G_TYPE_INT, -1,
800                                 G_TYPE_BOOLEAN, FALSE);
801   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
802                                 "move_cursor", 3,
803                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
804                                 G_TYPE_INT, 1,
805                                 G_TYPE_BOOLEAN, TRUE);  
806   /* Unselect all 
807    */
808   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
809                                 "move_cursor", 3,
810                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
811                                 G_TYPE_INT, 0,
812                                 G_TYPE_BOOLEAN, FALSE);
813   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
814                                 "move_cursor", 3,
815                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
816                                 G_TYPE_INT, 0,
817                                 G_TYPE_BOOLEAN, FALSE);
818
819   /* Activate
820    */
821   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
822                                 "activate", 0);
823   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
824                                 "activate", 0);
825   
826   /* Deleting text */
827   gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
828                                 "delete_from_cursor", 2,
829                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
830                                 G_TYPE_INT, 1);
831
832   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
833                                 "delete_from_cursor", 2,
834                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
835                                 G_TYPE_INT, 1);
836   
837   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
838                                 "backspace", 0);
839
840   /* Make this do the same as Backspace, to help with mis-typing */
841   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
842                                 "backspace", 0);
843
844   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
845                                 "delete_from_cursor", 2,
846                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
847                                 G_TYPE_INT, 1);
848
849   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
850                                 "delete_from_cursor", 2,
851                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
852                                 G_TYPE_INT, 1);
853   
854   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
855                                 "delete_from_cursor", 2,
856                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
857                                 G_TYPE_INT, -1);
858
859   /* Cut/copy/paste */
860
861   gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
862                                 "cut_clipboard", 0);
863   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
864                                 "copy_clipboard", 0);
865   gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
866                                 "paste_clipboard", 0);
867
868   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
869                                 "cut_clipboard", 0);
870   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
871                                 "copy_clipboard", 0);
872   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
873                                 "paste_clipboard", 0);
874
875   /* Overwrite */
876   gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
877                                 "toggle_overwrite", 0);
878   gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
879                                 "toggle_overwrite", 0);
880
881   /**
882    * GtkEntry:inner-border:
883    *
884    * Sets the text area's border between the text and the frame.
885    *
886    * Since: 2.10
887    */
888   gtk_widget_class_install_style_property (widget_class,
889                                            g_param_spec_boxed ("inner-border",
890                                                                P_("Inner Border"),
891                                                                P_("Border between text and frame."),
892                                                                GTK_TYPE_BORDER,
893                                                                GTK_PARAM_READABLE));
894
895    gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
896                                                        P_("Select on focus"),
897                                                        P_("Whether to select the contents of an entry when it is focused"),
898                                                        TRUE,
899                                                        GTK_PARAM_READWRITE));
900
901   /**
902    * GtkSettings:gtk-entry-password-hint-timeout:
903    *
904    * How long to show the last inputted character in hidden
905    * entries. This value is in milliseconds. 0 disables showing the
906    * last char. 600 is a good value for enabling it.
907    *
908    * Since: 2.10
909    */
910   gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
911                                                     P_("Password Hint Timeout"),
912                                                     P_("How long to show the last inputted character in hidden entries"),
913                                                     0, G_MAXUINT, 0,
914                                                     GTK_PARAM_READWRITE));
915
916   g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
917 }
918
919 static void
920 gtk_entry_editable_init (GtkEditableClass *iface)
921 {
922   iface->do_insert_text = gtk_entry_insert_text;
923   iface->do_delete_text = gtk_entry_delete_text;
924   iface->insert_text = gtk_entry_real_insert_text;
925   iface->delete_text = gtk_entry_real_delete_text;
926   iface->get_chars = gtk_entry_get_chars;
927   iface->set_selection_bounds = gtk_entry_set_selection_bounds;
928   iface->get_selection_bounds = gtk_entry_get_selection_bounds;
929   iface->set_position = gtk_entry_real_set_position;
930   iface->get_position = gtk_entry_get_position;
931 }
932
933 static void
934 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
935 {
936   iface->start_editing = gtk_entry_start_editing;
937 }
938
939 static void
940 gtk_entry_set_property (GObject         *object,
941                         guint            prop_id,
942                         const GValue    *value,
943                         GParamSpec      *pspec)
944 {
945   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
946   GtkEntry *entry = GTK_ENTRY (object);
947
948   switch (prop_id)
949     {
950     case PROP_EDITABLE:
951       {
952         gboolean new_value = g_value_get_boolean (value);
953
954         if (new_value != entry->editable)
955           {
956             if (!new_value)
957               {
958                 _gtk_entry_reset_im_context (entry);
959                 if (GTK_WIDGET_HAS_FOCUS (entry))
960                   gtk_im_context_focus_out (entry->im_context);
961
962                 entry->preedit_length = 0;
963                 entry->preedit_cursor = 0;
964               }
965
966             entry->editable = new_value;
967
968             if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
969               gtk_im_context_focus_in (entry->im_context);
970             
971             gtk_entry_queue_draw (entry);
972           }
973       }
974       break;
975
976     case PROP_MAX_LENGTH:
977       gtk_entry_set_max_length (entry, g_value_get_int (value));
978       break;
979       
980     case PROP_VISIBILITY:
981       gtk_entry_set_visibility (entry, g_value_get_boolean (value));
982       break;
983
984     case PROP_HAS_FRAME:
985       gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
986       break;
987
988     case PROP_INNER_BORDER:
989       gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
990       break;
991
992     case PROP_INVISIBLE_CHAR:
993       gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
994       break;
995
996     case PROP_ACTIVATES_DEFAULT:
997       gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
998       break;
999
1000     case PROP_WIDTH_CHARS:
1001       gtk_entry_set_width_chars (entry, g_value_get_int (value));
1002       break;
1003
1004     case PROP_TEXT:
1005       gtk_entry_set_text (entry, g_value_get_string (value));
1006       break;
1007
1008     case PROP_XALIGN:
1009       gtk_entry_set_alignment (entry, g_value_get_float (value));
1010       break;
1011
1012     case PROP_TRUNCATE_MULTILINE:
1013       entry->truncate_multiline = g_value_get_boolean (value);
1014       break;
1015
1016     case PROP_SHADOW_TYPE:
1017       priv->shadow_type = g_value_get_enum (value);
1018       break;
1019
1020     case PROP_SCROLL_OFFSET:
1021     case PROP_CURSOR_POSITION:
1022     default:
1023       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024       break;
1025     }
1026 }
1027
1028 static void
1029 gtk_entry_get_property (GObject         *object,
1030                         guint            prop_id,
1031                         GValue          *value,
1032                         GParamSpec      *pspec)
1033 {
1034   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1035   GtkEntry *entry = GTK_ENTRY (object);
1036
1037   switch (prop_id)
1038     {
1039     case PROP_CURSOR_POSITION:
1040       g_value_set_int (value, entry->current_pos);
1041       break;
1042     case PROP_SELECTION_BOUND:
1043       g_value_set_int (value, entry->selection_bound);
1044       break;
1045     case PROP_EDITABLE:
1046       g_value_set_boolean (value, entry->editable);
1047       break;
1048     case PROP_MAX_LENGTH:
1049       g_value_set_int (value, entry->text_max_length); 
1050       break;
1051     case PROP_VISIBILITY:
1052       g_value_set_boolean (value, entry->visible);
1053       break;
1054     case PROP_HAS_FRAME:
1055       g_value_set_boolean (value, entry->has_frame);
1056       break;
1057     case PROP_INNER_BORDER:
1058       g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1059       break;
1060     case PROP_INVISIBLE_CHAR:
1061       g_value_set_uint (value, entry->invisible_char);
1062       break;
1063     case PROP_ACTIVATES_DEFAULT:
1064       g_value_set_boolean (value, entry->activates_default);
1065       break;
1066     case PROP_WIDTH_CHARS:
1067       g_value_set_int (value, entry->width_chars);
1068       break;
1069     case PROP_SCROLL_OFFSET:
1070       g_value_set_int (value, entry->scroll_offset);
1071       break;
1072     case PROP_TEXT:
1073       g_value_set_string (value, gtk_entry_get_text (entry));
1074       break;
1075     case PROP_XALIGN:
1076       g_value_set_float (value, gtk_entry_get_alignment (entry));
1077       break;
1078     case PROP_TRUNCATE_MULTILINE:
1079       g_value_set_boolean (value, entry->truncate_multiline);
1080       break;
1081     case PROP_SHADOW_TYPE:
1082       g_value_set_enum (value, priv->shadow_type);
1083       break;
1084
1085     default:
1086       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1087       break;
1088     }
1089 }
1090
1091 static void
1092 gtk_entry_init (GtkEntry *entry)
1093 {
1094   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1095   
1096   GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1097
1098   entry->text_size = MIN_SIZE;
1099   entry->text = g_malloc (entry->text_size);
1100   entry->text[0] = '\0';
1101
1102   entry->editable = TRUE;
1103   entry->visible = TRUE;
1104   entry->invisible_char = '*';
1105   entry->dnd_position = -1;
1106   entry->width_chars = -1;
1107   entry->is_cell_renderer = FALSE;
1108   entry->editing_canceled = FALSE;
1109   entry->has_frame = TRUE;
1110   entry->truncate_multiline = FALSE;
1111   priv->shadow_type = GTK_SHADOW_IN;
1112   priv->xalign = 0.0;
1113
1114   gtk_drag_dest_set (GTK_WIDGET (entry),
1115                      GTK_DEST_DEFAULT_HIGHLIGHT,
1116                      NULL, 0,
1117                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
1118   gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1119
1120   /* This object is completely private. No external entity can gain a reference
1121    * to it; so we create it here and destroy it in finalize().
1122    */
1123   entry->im_context = gtk_im_multicontext_new ();
1124   
1125   g_signal_connect (entry->im_context, "commit",
1126                     G_CALLBACK (gtk_entry_commit_cb), entry);
1127   g_signal_connect (entry->im_context, "preedit_changed",
1128                     G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1129   g_signal_connect (entry->im_context, "retrieve_surrounding",
1130                     G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1131   g_signal_connect (entry->im_context, "delete_surrounding",
1132                     G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1133 }
1134
1135 static void
1136 begin_change (GtkEntry *entry)
1137 {
1138   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1139
1140   priv->change_count++;
1141 }
1142
1143 static void
1144 end_change (GtkEntry *entry)
1145 {
1146   GtkEditable *editable = GTK_EDITABLE (entry);
1147   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1148  
1149   g_return_if_fail (priv->change_count > 0);
1150
1151   priv->change_count--;
1152
1153   if (priv->change_count == 0)
1154     {
1155        if (priv->real_changed) 
1156          {
1157            g_signal_emit_by_name (editable, "changed");
1158            priv->real_changed = FALSE;
1159          }
1160     } 
1161 }
1162
1163 static void
1164 emit_changed (GtkEntry *entry)
1165 {
1166   GtkEditable *editable = GTK_EDITABLE (entry);
1167   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1168
1169   if (priv->change_count == 0)
1170     g_signal_emit_by_name (editable, "changed");
1171   else 
1172     priv->real_changed = TRUE;
1173 }
1174
1175 /*
1176  * Overwrite a memory that might contain sensitive information.
1177  */
1178 static void
1179 trash_area (gchar *area, gsize len)
1180 {
1181   volatile gchar *varea = (volatile gchar *)area;
1182   while (len-- > 0)
1183     *varea++ = 0;
1184 }
1185
1186 static void
1187 gtk_entry_destroy (GtkObject *object)
1188 {
1189   GtkEntry *entry = GTK_ENTRY (object);
1190
1191   entry->n_bytes = 0;
1192   entry->current_pos = entry->selection_bound = entry->text_length = 0;
1193   _gtk_entry_reset_im_context (entry);
1194   gtk_entry_reset_layout (entry);
1195
1196   if (entry->blink_timeout)
1197     {
1198       g_source_remove (entry->blink_timeout);
1199       entry->blink_timeout = 0;
1200     }
1201
1202   if (entry->recompute_idle)
1203     {
1204       g_source_remove (entry->recompute_idle);
1205       entry->recompute_idle = 0;
1206     }
1207
1208   if (!entry->visible)
1209     {
1210       /* We want to trash the text here because the entry might be leaked.  */
1211       trash_area (entry->text, strlen (entry->text));
1212     }
1213
1214   GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1215 }
1216
1217 static void
1218 gtk_entry_finalize (GObject *object)
1219 {
1220   GtkEntry *entry = GTK_ENTRY (object);
1221
1222   gtk_entry_set_completion (entry, NULL);
1223
1224   if (entry->cached_layout)
1225     g_object_unref (entry->cached_layout);
1226
1227   g_object_unref (entry->im_context);
1228
1229   if (entry->blink_timeout)
1230     g_source_remove (entry->blink_timeout);
1231
1232   if (entry->recompute_idle)
1233     g_source_remove (entry->recompute_idle);
1234
1235   entry->text_size = 0;
1236
1237   if (entry->text)
1238     {
1239       if (!entry->visible)
1240         trash_area (entry->text, strlen (entry->text));
1241       g_free (entry->text);
1242       entry->text = NULL;
1243     }
1244
1245   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
1246 }
1247
1248 static void
1249 gtk_entry_realize (GtkWidget *widget)
1250 {
1251   GtkEntry *entry;
1252   GdkWindowAttr attributes;
1253   gint attributes_mask;
1254
1255   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1256   entry = GTK_ENTRY (widget);
1257
1258   attributes.window_type = GDK_WINDOW_CHILD;
1259   
1260   get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1261
1262   attributes.wclass = GDK_INPUT_OUTPUT;
1263   attributes.visual = gtk_widget_get_visual (widget);
1264   attributes.colormap = gtk_widget_get_colormap (widget);
1265   attributes.event_mask = gtk_widget_get_events (widget);
1266   attributes.event_mask |= (GDK_EXPOSURE_MASK |
1267                             GDK_BUTTON_PRESS_MASK |
1268                             GDK_BUTTON_RELEASE_MASK |
1269                             GDK_BUTTON1_MOTION_MASK |
1270                             GDK_BUTTON3_MOTION_MASK |
1271                             GDK_POINTER_MOTION_HINT_MASK |
1272                             GDK_POINTER_MOTION_MASK |
1273                             GDK_ENTER_NOTIFY_MASK |
1274                             GDK_LEAVE_NOTIFY_MASK);
1275   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1276
1277   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1278   gdk_window_set_user_data (widget->window, entry);
1279
1280   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1281  
1282   if (GTK_WIDGET_IS_SENSITIVE (widget))
1283     {
1284       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1285       attributes_mask |= GDK_WA_CURSOR;
1286     }
1287
1288   entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1289
1290   gdk_window_set_user_data (entry->text_area, entry);
1291
1292   if (attributes_mask & GDK_WA_CURSOR)
1293     gdk_cursor_unref (attributes.cursor);
1294
1295   widget->style = gtk_style_attach (widget->style, widget->window);
1296
1297   gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1298   gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1299
1300   gdk_window_show (entry->text_area);
1301
1302   gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1303
1304   gtk_entry_adjust_scroll (entry);
1305   gtk_entry_update_primary_selection (entry);
1306 }
1307
1308 static void
1309 gtk_entry_unrealize (GtkWidget *widget)
1310 {
1311   GtkEntry *entry = GTK_ENTRY (widget);
1312   GtkClipboard *clipboard;
1313
1314   gtk_entry_reset_layout (entry);
1315   
1316   gtk_im_context_set_client_window (entry->im_context, NULL);
1317
1318   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1319   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1320     gtk_clipboard_clear (clipboard);
1321   
1322   if (entry->text_area)
1323     {
1324       gdk_window_set_user_data (entry->text_area, NULL);
1325       gdk_window_destroy (entry->text_area);
1326       entry->text_area = NULL;
1327     }
1328
1329   if (entry->popup_menu)
1330     {
1331       gtk_widget_destroy (entry->popup_menu);
1332       entry->popup_menu = NULL;
1333     }
1334
1335   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize)
1336     (* GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize) (widget);
1337 }
1338
1339 void
1340 _gtk_entry_get_borders (GtkEntry *entry,
1341                         gint     *xborder,
1342                         gint     *yborder)
1343 {
1344   GtkWidget *widget = GTK_WIDGET (entry);
1345   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1346
1347   if (entry->has_frame)
1348     {
1349       *xborder = widget->style->xthickness;
1350       *yborder = widget->style->ythickness;
1351     }
1352   else
1353     {
1354       *xborder = 0;
1355       *yborder = 0;
1356     }
1357
1358   if (!priv->interior_focus)
1359     {
1360       *xborder += priv->focus_width;
1361       *yborder += priv->focus_width;
1362     }
1363 }
1364
1365 static void
1366 gtk_entry_size_request (GtkWidget      *widget,
1367                         GtkRequisition *requisition)
1368 {
1369   GtkEntry *entry = GTK_ENTRY (widget);
1370   PangoFontMetrics *metrics;
1371   gint xborder, yborder;
1372   GtkBorder inner_border;
1373   PangoContext *context;
1374   
1375   gtk_widget_ensure_style (widget);
1376   context = gtk_widget_get_pango_context (widget);
1377   metrics = pango_context_get_metrics (context,
1378                                        widget->style->font_desc,
1379                                        pango_context_get_language (context));
1380
1381   entry->ascent = pango_font_metrics_get_ascent (metrics);
1382   entry->descent = pango_font_metrics_get_descent (metrics);
1383   
1384   _gtk_entry_get_borders (entry, &xborder, &yborder);
1385   _gtk_entry_effective_inner_border (entry, &inner_border);
1386
1387   if (entry->width_chars < 0)
1388     requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1389   else
1390     {
1391       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1392       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1393       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1394       
1395       requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1396     }
1397     
1398   requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1399
1400   pango_font_metrics_unref (metrics);
1401 }
1402
1403 static void
1404 get_text_area_size (GtkEntry *entry,
1405                     gint     *x,
1406                     gint     *y,
1407                     gint     *width,
1408                     gint     *height)
1409 {
1410   gint frame_height;
1411   gint xborder, yborder;
1412   GtkRequisition requisition;
1413   GtkWidget *widget = GTK_WIDGET (entry);
1414   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1415
1416   gtk_widget_get_child_requisition (widget, &requisition);
1417   _gtk_entry_get_borders (entry, &xborder, &yborder);
1418
1419   if (GTK_WIDGET_REALIZED (widget))
1420     gdk_drawable_get_size (widget->window, NULL, &frame_height);
1421   else
1422     frame_height = requisition.height;
1423
1424   if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1425       frame_height -= 2 * priv->focus_width;
1426
1427   if (x)
1428     *x = xborder;
1429
1430   if (y)
1431     *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
1432
1433   if (width)
1434     *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1435
1436   if (height)
1437     *height = requisition.height - yborder * 2;
1438 }
1439
1440 static void
1441 get_widget_window_size (GtkEntry *entry,
1442                         gint     *x,
1443                         gint     *y,
1444                         gint     *width,
1445                         gint     *height)
1446 {
1447   GtkRequisition requisition;
1448   GtkWidget *widget = GTK_WIDGET (entry);
1449       
1450   gtk_widget_get_child_requisition (widget, &requisition);
1451
1452   if (x)
1453     *x = widget->allocation.x;
1454
1455   if (y)
1456     {
1457       if (entry->is_cell_renderer)
1458         *y = widget->allocation.y;
1459       else
1460         *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1461     }
1462
1463   if (width)
1464     *width = widget->allocation.width;
1465
1466   if (height)
1467     {
1468       if (entry->is_cell_renderer)
1469         *height = widget->allocation.height;
1470       else
1471         *height = requisition.height;
1472     }
1473 }
1474
1475 void
1476 _gtk_entry_effective_inner_border (GtkEntry  *entry,
1477                                    GtkBorder *border)
1478 {
1479   GtkBorder *tmp_border;
1480
1481   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1482
1483   if (tmp_border)
1484     {
1485       *border = *tmp_border;
1486       return;
1487     }
1488
1489   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1490
1491   if (tmp_border)
1492     {
1493       *border = *tmp_border;
1494       gtk_border_free (tmp_border);
1495       return;
1496     }
1497
1498   *border = default_inner_border;
1499 }
1500
1501 static void
1502 gtk_entry_size_allocate (GtkWidget     *widget,
1503                          GtkAllocation *allocation)
1504 {
1505   GtkEntry *entry = GTK_ENTRY (widget);
1506   
1507   widget->allocation = *allocation;
1508   
1509   if (GTK_WIDGET_REALIZED (widget))
1510     {
1511       /* We call gtk_widget_get_child_requisition, since we want (for
1512        * backwards compatibility reasons) the realization here to
1513        * be affected by the usize of the entry, if set
1514        */
1515       gint x, y, width, height;
1516
1517       get_widget_window_size (entry, &x, &y, &width, &height);
1518       
1519       gdk_window_move_resize (widget->window,
1520                               x, y, width, height);   
1521
1522       get_text_area_size (entry, &x, &y, &width, &height);
1523       
1524       gdk_window_move_resize (entry->text_area,
1525                               x, y, width, height);
1526
1527       gtk_entry_recompute (entry);
1528     }
1529 }
1530
1531 static void
1532 gtk_entry_draw_frame (GtkWidget    *widget,
1533                       GdkRectangle *area)
1534 {
1535   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1536   gint x = 0, y = 0, width, height;
1537   
1538   gdk_drawable_get_size (widget->window, &width, &height);
1539   
1540   if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1541     {
1542       x += priv->focus_width;
1543       y += priv->focus_width;
1544       width -= 2 * priv->focus_width;
1545       height -= 2 * priv->focus_width;
1546     }
1547
1548   gtk_paint_shadow (widget->style, widget->window,
1549                     GTK_STATE_NORMAL, priv->shadow_type,
1550                     area, widget, "entry", x, y, width, height);
1551
1552   if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1553     {
1554       x -= priv->focus_width;
1555       y -= priv->focus_width;
1556       width += 2 * priv->focus_width;
1557       height += 2 * priv->focus_width;
1558       
1559       gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
1560                        area, widget, "entry",
1561                        0, 0, width, height);
1562     }
1563 }
1564
1565 static gint
1566 gtk_entry_expose (GtkWidget      *widget,
1567                   GdkEventExpose *event)
1568 {
1569   GtkEntry *entry = GTK_ENTRY (widget);
1570
1571   if (widget->window == event->window)
1572     gtk_entry_draw_frame (widget, &event->area);
1573   else if (entry->text_area == event->window)
1574     {
1575       gint area_width, area_height;
1576
1577       get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1578
1579       gtk_paint_flat_box (widget->style, entry->text_area, 
1580                           GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1581                           &event->area, widget, "entry_bg",
1582                           0, 0, area_width, area_height);
1583       
1584       if ((entry->visible || entry->invisible_char != 0) &&
1585           GTK_WIDGET_HAS_FOCUS (widget) &&
1586           entry->selection_bound == entry->current_pos && entry->cursor_visible)
1587         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1588
1589       if (entry->dnd_position != -1)
1590         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1591       
1592       gtk_entry_draw_text (GTK_ENTRY (widget));
1593     }
1594
1595   return FALSE;
1596 }
1597
1598 static void
1599 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
1600                             gint     **ranges,
1601                             gint      *n_ranges)
1602 {
1603   gint start_char, end_char;
1604
1605   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1606     {
1607       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1608       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
1609       const char *text = pango_layout_get_text (layout);
1610       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1611       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1612       gint real_n_ranges, i;
1613
1614       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1615
1616       if (ranges)
1617         {
1618           gint *r = *ranges;
1619           
1620           for (i = 0; i < real_n_ranges; ++i)
1621             {
1622               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1623               r[2 * i] = r[2 * i] / PANGO_SCALE;
1624             }
1625         }
1626       
1627       if (n_ranges)
1628         *n_ranges = real_n_ranges;
1629     }
1630   else
1631     {
1632       if (n_ranges)
1633         *n_ranges = 0;
1634       if (ranges)
1635         *ranges = NULL;
1636     }
1637 }
1638
1639 static gboolean
1640 in_selection (GtkEntry *entry,
1641               gint      x)
1642 {
1643   gint *ranges;
1644   gint n_ranges, i;
1645   gint retval = FALSE;
1646
1647   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1648
1649   for (i = 0; i < n_ranges; ++i)
1650     {
1651       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1652         {
1653           retval = TRUE;
1654           break;
1655         }
1656     }
1657
1658   g_free (ranges);
1659   return retval;
1660 }
1661               
1662 static gint
1663 gtk_entry_button_press (GtkWidget      *widget,
1664                         GdkEventButton *event)
1665 {
1666   GtkEntry *entry = GTK_ENTRY (widget);
1667   GtkEditable *editable = GTK_EDITABLE (widget);
1668   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1669   gint tmp_pos;
1670   gint sel_start, sel_end;
1671
1672   if (event->window != entry->text_area ||
1673       (entry->button && event->button != entry->button))
1674     return FALSE;
1675
1676   gtk_entry_reset_blink_time (entry);
1677
1678   entry->button = event->button;
1679   
1680   if (!GTK_WIDGET_HAS_FOCUS (widget))
1681     {
1682       entry->in_click = TRUE;
1683       gtk_widget_grab_focus (widget);
1684       entry->in_click = FALSE;
1685     }
1686   
1687   tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1688     
1689   if (event->button == 1)
1690     {
1691       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1692       
1693       entry->select_words = FALSE;
1694       entry->select_lines = FALSE;
1695
1696       if (event->state & GDK_SHIFT_MASK)
1697         {
1698           _gtk_entry_reset_im_context (entry);
1699           
1700           if (!have_selection) /* select from the current position to the clicked position */
1701             sel_start = sel_end = entry->current_pos;
1702           
1703           if (tmp_pos > sel_start && tmp_pos < sel_end)
1704             {
1705               /* Truncate current selection, but keep it as big as possible */
1706               if (tmp_pos - sel_start > sel_end - tmp_pos)
1707                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
1708               else
1709                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
1710             }
1711           else
1712             {
1713               gboolean extend_to_left;
1714               gint start, end;
1715
1716               /* Figure out what click selects and extend current selection */
1717               switch (event->type)
1718                 {
1719                 case GDK_BUTTON_PRESS:
1720                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1721                   break;
1722                   
1723                 case GDK_2BUTTON_PRESS:
1724                   entry->select_words = TRUE;
1725                   gtk_entry_select_word (entry);
1726                   break;
1727                   
1728                 case GDK_3BUTTON_PRESS:
1729                   entry->select_lines = TRUE;
1730                   gtk_entry_select_line (entry);
1731                   break;
1732
1733                 default:
1734                   break;
1735                 }
1736
1737               start = MIN (entry->current_pos, entry->selection_bound);
1738               start = MIN (sel_start, start);
1739               
1740               end = MAX (entry->current_pos, entry->selection_bound);
1741               end = MAX (sel_end, end);
1742
1743               if (tmp_pos == sel_start || tmp_pos == sel_end)
1744                 extend_to_left = (tmp_pos == start);
1745               else
1746                 extend_to_left = (end == sel_end);
1747               
1748               if (extend_to_left)
1749                 gtk_entry_set_positions (entry, start, end);
1750               else
1751                 gtk_entry_set_positions (entry, end, start);
1752             }
1753         }
1754       else /* no shift key */
1755         switch (event->type)
1756         {
1757         case GDK_BUTTON_PRESS:
1758           if (in_selection (entry, event->x + entry->scroll_offset))
1759             {
1760               /* Click inside the selection - we'll either start a drag, or
1761                * clear the selection
1762                */
1763               entry->in_drag = TRUE;
1764               entry->drag_start_x = event->x + entry->scroll_offset;
1765               entry->drag_start_y = event->y;
1766             }
1767           else
1768             gtk_editable_set_position (editable, tmp_pos);
1769           break;
1770  
1771         case GDK_2BUTTON_PRESS:
1772           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
1773            * receiving a GDK_2BUTTON_PRESS so we need to reset
1774            * entry->in_drag which may have been set above
1775            */
1776           entry->in_drag = FALSE;
1777           entry->select_words = TRUE;
1778           gtk_entry_select_word (entry);
1779           break;
1780         
1781         case GDK_3BUTTON_PRESS:
1782           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1783            * receiving a GDK_3BUTTON_PRESS so we need to reset
1784            * entry->in_drag which may have been set above
1785            */
1786           entry->in_drag = FALSE;
1787           entry->select_lines = TRUE;
1788           gtk_entry_select_line (entry);
1789           break;
1790
1791         default:
1792           break;
1793         }
1794
1795       return TRUE;
1796     }
1797   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
1798     {
1799       if (entry->editable)
1800         {
1801           priv->insert_pos = tmp_pos;
1802           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1803           return TRUE;
1804         }
1805       else
1806         {
1807           gtk_widget_error_bell (widget);
1808         }
1809     }
1810   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1811     {
1812       gtk_entry_do_popup (entry, event);
1813       entry->button = 0;        /* Don't wait for release, since the menu will gtk_grab_add */
1814
1815       return TRUE;
1816     }
1817
1818   return FALSE;
1819 }
1820
1821 static gint
1822 gtk_entry_button_release (GtkWidget      *widget,
1823                           GdkEventButton *event)
1824 {
1825   GtkEntry *entry = GTK_ENTRY (widget);
1826
1827   if (event->window != entry->text_area || entry->button != event->button)
1828     return FALSE;
1829
1830   if (entry->in_drag)
1831     {
1832       gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1833
1834       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1835
1836       entry->in_drag = 0;
1837     }
1838   
1839   entry->button = 0;
1840   
1841   gtk_entry_update_primary_selection (entry);
1842               
1843   return TRUE;
1844 }
1845
1846 static gchar *
1847 _gtk_entry_get_selected_text (GtkEntry *entry)
1848 {
1849   GtkEditable *editable = GTK_EDITABLE (entry);
1850   gint         start_text, end_text;
1851   gchar       *text = NULL;
1852
1853   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1854     text = gtk_editable_get_chars (editable, start_text, end_text);
1855
1856   return text;
1857 }
1858
1859 static void
1860 drag_begin_cb (GtkWidget      *widget,
1861                GdkDragContext *context,
1862                gpointer        data)
1863 {
1864   g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1865 }
1866
1867 static gint
1868 gtk_entry_motion_notify (GtkWidget      *widget,
1869                          GdkEventMotion *event)
1870 {
1871   GtkEntry *entry = GTK_ENTRY (widget);
1872   gint tmp_pos;
1873
1874   if (entry->mouse_cursor_obscured)
1875     {
1876       GdkCursor *cursor;
1877       
1878       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1879       gdk_window_set_cursor (entry->text_area, cursor);
1880       gdk_cursor_unref (cursor);
1881       entry->mouse_cursor_obscured = FALSE;
1882     }
1883
1884   if (event->window != entry->text_area || entry->button != 1)
1885     return FALSE;
1886
1887   if (entry->select_lines)
1888     return TRUE;
1889
1890   gdk_event_request_motions (event);
1891
1892   if (entry->in_drag)
1893     {
1894       if (gtk_drag_check_threshold (widget,
1895                                     entry->drag_start_x, entry->drag_start_y,
1896                                     event->x + entry->scroll_offset, event->y))
1897         {
1898           GdkDragContext *context;
1899           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
1900           guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1901           gchar *text = NULL;
1902           GdkPixmap *pixmap = NULL;
1903
1904           gtk_target_list_add_text_targets (target_list, 0);
1905
1906           if (entry->visible)
1907             {
1908               text = _gtk_entry_get_selected_text (entry);
1909               pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1910             }
1911
1912           context = gtk_drag_begin (widget, target_list, actions,
1913                                     entry->button, (GdkEvent *)event);
1914           
1915           if (pixmap)
1916             gtk_drag_set_icon_pixmap (context,
1917                                       gdk_drawable_get_colormap (pixmap),
1918                                       pixmap,
1919                                       NULL,
1920                                       -2, -2);
1921           else
1922             gtk_drag_set_icon_default (context);
1923           
1924           if (pixmap)
1925             g_object_unref (pixmap);
1926           g_free (text);
1927
1928           entry->in_drag = FALSE;
1929           entry->button = 0;
1930           
1931           gtk_target_list_unref (target_list);
1932         }
1933     }
1934   else
1935     {
1936       gint height;
1937       gdk_drawable_get_size (entry->text_area, NULL, &height);
1938
1939       if (event->y < 0)
1940         tmp_pos = 0;
1941       else if (event->y >= height)
1942         tmp_pos = entry->text_length;
1943       else
1944         tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1945       
1946       if (entry->select_words) 
1947         {
1948           gint min, max;
1949           gint old_min, old_max;
1950           gint pos, bound;
1951           
1952           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1953           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1954           
1955           pos = entry->current_pos;
1956           bound = entry->selection_bound;
1957
1958           old_min = MIN(entry->current_pos, entry->selection_bound);
1959           old_max = MAX(entry->current_pos, entry->selection_bound);
1960           
1961           if (min < old_min)
1962             {
1963               pos = min;
1964               bound = old_max;
1965             }
1966           else if (old_max < max) 
1967             {
1968               pos = max;
1969               bound = old_min;
1970             }
1971           else if (pos == old_min) 
1972             {
1973               if (entry->current_pos != min)
1974                 pos = max;
1975             }
1976           else 
1977             {
1978               if (entry->current_pos != max)
1979                 pos = min;
1980             }
1981         
1982           gtk_entry_set_positions (entry, pos, bound);
1983         }
1984       else
1985       gtk_entry_set_positions (entry, tmp_pos, -1);
1986     }
1987       
1988   return TRUE;
1989 }
1990
1991 static void
1992 set_invisible_cursor (GdkWindow *window)
1993 {
1994   GdkBitmap *empty_bitmap;
1995   GdkCursor *cursor;
1996   GdkColor useless;
1997   char invisible_cursor_bits[] = { 0x0 };       
1998         
1999   useless.red = useless.green = useless.blue = 0;
2000   useless.pixel = 0;
2001   
2002   empty_bitmap = gdk_bitmap_create_from_data (window,
2003                                               invisible_cursor_bits,
2004                                               1, 1);
2005   
2006   cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
2007                                        empty_bitmap,
2008                                        &useless,
2009                                        &useless, 0, 0);
2010   
2011   gdk_window_set_cursor (window, cursor);
2012   
2013   gdk_cursor_unref (cursor);
2014   
2015   g_object_unref (empty_bitmap);
2016 }
2017
2018 static void
2019 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
2020 {
2021   if (entry->mouse_cursor_obscured)
2022     return;
2023
2024   set_invisible_cursor (entry->text_area);
2025   
2026   entry->mouse_cursor_obscured = TRUE;  
2027 }
2028
2029 static gint
2030 gtk_entry_key_press (GtkWidget   *widget,
2031                      GdkEventKey *event)
2032 {
2033   GtkEntry *entry = GTK_ENTRY (widget);
2034
2035   gtk_entry_reset_blink_time (entry);
2036   gtk_entry_pend_cursor_blink (entry);
2037
2038   if (entry->editable)
2039     {
2040       if (gtk_im_context_filter_keypress (entry->im_context, event))
2041         {
2042           gtk_entry_obscure_mouse_cursor (entry);
2043           entry->need_im_reset = TRUE;
2044           return TRUE;
2045         }
2046     }
2047
2048   if (event->keyval == GDK_Return || 
2049       event->keyval == GDK_KP_Enter || 
2050       event->keyval == GDK_ISO_Enter || 
2051       event->keyval == GDK_Escape)
2052     {
2053       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2054       
2055       if (completion && completion->priv->completion_timeout)
2056         {
2057           g_source_remove (completion->priv->completion_timeout);
2058           completion->priv->completion_timeout = 0;
2059         }
2060
2061       _gtk_entry_reset_im_context (entry);
2062     }
2063
2064   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
2065     /* Activate key bindings
2066      */
2067     return TRUE;
2068
2069   if (!entry->editable && event->length)
2070     gtk_widget_error_bell (widget);
2071
2072   return FALSE;
2073 }
2074
2075 static gint
2076 gtk_entry_key_release (GtkWidget   *widget,
2077                        GdkEventKey *event)
2078 {
2079   GtkEntry *entry = GTK_ENTRY (widget);
2080
2081   if (entry->editable)
2082     {
2083       if (gtk_im_context_filter_keypress (entry->im_context, event))
2084         {
2085           entry->need_im_reset = TRUE;
2086           return TRUE;
2087         }
2088     }
2089
2090   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
2091 }
2092
2093 static gint
2094 gtk_entry_focus_in (GtkWidget     *widget,
2095                     GdkEventFocus *event)
2096 {
2097   GtkEntry *entry = GTK_ENTRY (widget);
2098   
2099   gtk_widget_queue_draw (widget);
2100   
2101   if (entry->editable)
2102     {
2103       entry->need_im_reset = TRUE;
2104       gtk_im_context_focus_in (entry->im_context);
2105     }
2106
2107   g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2108                     "direction_changed",
2109                     G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2110
2111   gtk_entry_reset_blink_time (entry);
2112   gtk_entry_check_cursor_blink (entry);
2113
2114   return FALSE;
2115 }
2116
2117 static gint
2118 gtk_entry_focus_out (GtkWidget     *widget,
2119                      GdkEventFocus *event)
2120 {
2121   GtkEntry *entry = GTK_ENTRY (widget);
2122   GtkEntryCompletion *completion;
2123   
2124   gtk_widget_queue_draw (widget);
2125
2126   if (entry->editable)
2127     {
2128       entry->need_im_reset = TRUE;
2129       gtk_im_context_focus_out (entry->im_context);
2130     }
2131
2132   gtk_entry_check_cursor_blink (entry);
2133   
2134   g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2135                                         gtk_entry_keymap_direction_changed,
2136                                         entry);
2137
2138   completion = gtk_entry_get_completion (entry);
2139   if (completion)
2140     _gtk_entry_completion_popdown (completion);
2141   
2142   return FALSE;
2143 }
2144
2145 static void
2146 gtk_entry_grab_focus (GtkWidget        *widget)
2147 {
2148   GtkEntry *entry = GTK_ENTRY (widget);
2149   gboolean select_on_focus;
2150   
2151   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
2152
2153   if (entry->editable && !entry->in_click)
2154     {
2155       g_object_get (gtk_widget_get_settings (widget),
2156                     "gtk-entry-select-on-focus",
2157                     &select_on_focus,
2158                     NULL);
2159   
2160       if (select_on_focus)
2161         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2162     }
2163 }
2164
2165 static void 
2166 gtk_entry_direction_changed (GtkWidget        *widget,
2167                              GtkTextDirection  previous_dir)
2168 {
2169   GtkEntry *entry = GTK_ENTRY (widget);
2170
2171   gtk_entry_recompute (entry);
2172       
2173   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
2174 }
2175
2176 static void
2177 gtk_entry_state_changed (GtkWidget      *widget,
2178                          GtkStateType    previous_state)
2179 {
2180   GtkEntry *entry = GTK_ENTRY (widget);
2181   GdkCursor *cursor;
2182   
2183   if (GTK_WIDGET_REALIZED (widget))
2184     {
2185       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2186       gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2187
2188       if (GTK_WIDGET_IS_SENSITIVE (widget))
2189         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2190       else 
2191         cursor = NULL;
2192       
2193       gdk_window_set_cursor (entry->text_area, cursor);
2194
2195       if (cursor)
2196         gdk_cursor_unref (cursor);
2197
2198       entry->mouse_cursor_obscured = FALSE;
2199     }
2200
2201   if (!GTK_WIDGET_IS_SENSITIVE (widget))
2202     {
2203       /* Clear any selection */
2204       gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);      
2205     }
2206   
2207   gtk_widget_queue_draw (widget);
2208 }
2209
2210 static void
2211 gtk_entry_screen_changed (GtkWidget *widget,
2212                           GdkScreen *old_screen)
2213 {
2214   gtk_entry_recompute (GTK_ENTRY (widget));
2215 }
2216
2217 /* GtkEditable method implementations
2218  */
2219 static void
2220 gtk_entry_insert_text (GtkEditable *editable,
2221                        const gchar *new_text,
2222                        gint         new_text_length,
2223                        gint        *position)
2224 {
2225   GtkEntry *entry = GTK_ENTRY (editable);
2226   gchar buf[64];
2227   gchar *text;
2228
2229   if (*position < 0 || *position > entry->text_length)
2230     *position = entry->text_length;
2231   
2232   g_object_ref (editable);
2233   
2234   if (new_text_length <= 63)
2235     text = buf;
2236   else
2237     text = g_new (gchar, new_text_length + 1);
2238
2239   text[new_text_length] = '\0';
2240   strncpy (text, new_text, new_text_length);
2241
2242   g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2243
2244   if (!entry->visible)
2245     trash_area (text, new_text_length);
2246
2247   if (new_text_length > 63)
2248     g_free (text);
2249
2250   g_object_unref (editable);
2251 }
2252
2253 static void
2254 gtk_entry_delete_text (GtkEditable *editable,
2255                        gint         start_pos,
2256                        gint         end_pos)
2257 {
2258   GtkEntry *entry = GTK_ENTRY (editable);
2259
2260   if (end_pos < 0 || end_pos > entry->text_length)
2261     end_pos = entry->text_length;
2262   if (start_pos < 0)
2263     start_pos = 0;
2264   if (start_pos > end_pos)
2265     start_pos = end_pos;
2266   
2267   g_object_ref (editable);
2268
2269   g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2270
2271   g_object_unref (editable);
2272 }
2273
2274 static gchar *    
2275 gtk_entry_get_chars      (GtkEditable   *editable,
2276                           gint           start_pos,
2277                           gint           end_pos)
2278 {
2279   GtkEntry *entry = GTK_ENTRY (editable);
2280   gint start_index, end_index;
2281   
2282   if (end_pos < 0)
2283     end_pos = entry->text_length;
2284
2285   start_pos = MIN (entry->text_length, start_pos);
2286   end_pos = MIN (entry->text_length, end_pos);
2287
2288   start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2289   end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2290
2291   return g_strndup (entry->text + start_index, end_index - start_index);
2292 }
2293
2294 static void
2295 gtk_entry_real_set_position (GtkEditable *editable,
2296                              gint         position)
2297 {
2298   GtkEntry *entry = GTK_ENTRY (editable);
2299
2300   if (position < 0 || position > entry->text_length)
2301     position = entry->text_length;
2302
2303   if (position != entry->current_pos ||
2304       position != entry->selection_bound)
2305     {
2306       _gtk_entry_reset_im_context (entry);
2307       gtk_entry_set_positions (entry, position, position);
2308     }
2309 }
2310
2311 static gint
2312 gtk_entry_get_position (GtkEditable *editable)
2313 {
2314   return GTK_ENTRY (editable)->current_pos;
2315 }
2316
2317 static void
2318 gtk_entry_set_selection_bounds (GtkEditable *editable,
2319                                 gint         start,
2320                                 gint         end)
2321 {
2322   GtkEntry *entry = GTK_ENTRY (editable);
2323
2324   if (start < 0)
2325     start = entry->text_length;
2326   if (end < 0)
2327     end = entry->text_length;
2328   
2329   _gtk_entry_reset_im_context (entry);
2330
2331   gtk_entry_set_positions (entry,
2332                            MIN (end, entry->text_length),
2333                            MIN (start, entry->text_length));
2334
2335   gtk_entry_update_primary_selection (entry);
2336 }
2337
2338 static gboolean
2339 gtk_entry_get_selection_bounds (GtkEditable *editable,
2340                                 gint        *start,
2341                                 gint        *end)
2342 {
2343   GtkEntry *entry = GTK_ENTRY (editable);
2344
2345   *start = entry->selection_bound;
2346   *end = entry->current_pos;
2347
2348   return (entry->selection_bound != entry->current_pos);
2349 }
2350
2351 static void 
2352 gtk_entry_style_set     (GtkWidget      *widget,
2353                          GtkStyle       *previous_style)
2354 {
2355   GtkEntry *entry = GTK_ENTRY (widget);
2356   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2357
2358   gtk_widget_style_get (widget,
2359                         "focus-line-width", &priv->focus_width,
2360                         "interior-focus", &priv->interior_focus,
2361                         NULL);
2362   
2363   gtk_entry_recompute (entry);
2364
2365   if (previous_style && GTK_WIDGET_REALIZED (widget))
2366     {
2367       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2368       gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2369     }
2370 }
2371
2372 /* GtkCellEditable method implementations
2373  */
2374 static void
2375 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2376 {
2377   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2378   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2379 }
2380
2381 static gboolean
2382 gtk_cell_editable_key_press_event (GtkEntry    *entry,
2383                                    GdkEventKey *key_event,
2384                                    gpointer     data)
2385 {
2386   if (key_event->keyval == GDK_Escape)
2387     {
2388       entry->editing_canceled = TRUE;
2389       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2390       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2391
2392       return TRUE;
2393     }
2394
2395   /* override focus */
2396   if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2397     {
2398       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2399       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2400
2401       return TRUE;
2402     }
2403
2404   return FALSE;
2405 }
2406
2407 static void
2408 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2409                          GdkEvent        *event)
2410 {
2411   GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2412
2413   g_signal_connect (cell_editable, "activate",
2414                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2415   g_signal_connect (cell_editable, "key_press_event",
2416                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2417 }
2418
2419 static void
2420 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2421 {
2422   if (password_hint->password_hint_timeout_id)
2423     g_source_remove (password_hint->password_hint_timeout_id);
2424
2425   g_free (password_hint);
2426 }
2427
2428 /* Default signal handlers
2429  */
2430 static void
2431 gtk_entry_real_insert_text (GtkEditable *editable,
2432                             const gchar *new_text,
2433                             gint         new_text_length,
2434                             gint        *position)
2435 {
2436   GtkEntry *entry = GTK_ENTRY (editable);
2437   gint index;
2438   gint n_chars;
2439
2440   if (new_text_length < 0)
2441     new_text_length = strlen (new_text);
2442
2443   n_chars = g_utf8_strlen (new_text, new_text_length);
2444   if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2445     {
2446       gtk_widget_error_bell (GTK_WIDGET (entry));
2447       n_chars = entry->text_max_length - entry->text_length;
2448       new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2449     }
2450
2451   if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2452     {
2453       gsize prev_size = entry->text_size;
2454
2455       while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2456         {
2457           if (entry->text_size == 0)
2458             entry->text_size = MIN_SIZE;
2459           else
2460             {
2461               if (2 * (guint)entry->text_size < MAX_SIZE &&
2462                   2 * (guint)entry->text_size > entry->text_size)
2463                 entry->text_size *= 2;
2464               else
2465                 {
2466                   entry->text_size = MAX_SIZE;
2467                   if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2468                     {
2469                       new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2470                       new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2471                       n_chars = g_utf8_strlen (new_text, new_text_length);
2472                     }
2473                   break;
2474                 }
2475             }
2476         }
2477
2478       if (entry->visible)
2479         entry->text = g_realloc (entry->text, entry->text_size);
2480       else
2481         {
2482           /* Same thing, just slower and without leaving stuff in memory.  */
2483           gchar *et_new = g_malloc (entry->text_size);
2484           memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2485           trash_area (entry->text, prev_size);
2486           g_free (entry->text);
2487           entry->text = et_new;
2488         }
2489     }
2490
2491   index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2492
2493   g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2494   memcpy (entry->text + index, new_text, new_text_length);
2495
2496   entry->n_bytes += new_text_length;
2497   entry->text_length += n_chars;
2498
2499   /* NUL terminate for safety and convenience */
2500   entry->text[entry->n_bytes] = '\0';
2501   
2502   if (entry->current_pos > *position)
2503     entry->current_pos += n_chars;
2504   
2505   if (entry->selection_bound > *position)
2506     entry->selection_bound += n_chars;
2507
2508   if (n_chars == 1 && !entry->visible && (new_text_length < PASSWORD_HINT_MAX))
2509     {
2510       guint password_hint_timeout;
2511
2512       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2513                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
2514                     NULL);
2515
2516       if (password_hint_timeout > 0)
2517         {
2518           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2519                                                                     quark_password_hint);
2520
2521           if (!password_hint)
2522             {
2523               password_hint = g_new0 (GtkEntryPasswordHint, 1);
2524               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2525                                        password_hint,
2526                                        (GDestroyNotify) gtk_entry_password_hint_free);
2527             }
2528
2529           memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2530           password_hint->password_hint_length = new_text_length;
2531           memcpy (&password_hint->password_hint, new_text, new_text_length);
2532           password_hint->password_hint_position = *position + n_chars;
2533        }
2534     }
2535   else
2536     {
2537       g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2538     }
2539
2540   *position += n_chars;
2541
2542   gtk_entry_recompute (entry);
2543
2544   emit_changed (entry);
2545   g_object_notify (G_OBJECT (editable), "text");
2546 }
2547
2548 static void
2549 gtk_entry_real_delete_text (GtkEditable *editable,
2550                             gint         start_pos,
2551                             gint         end_pos)
2552 {
2553   GtkEntry *entry = GTK_ENTRY (editable);
2554
2555   if (start_pos < 0)
2556     start_pos = 0;
2557   if (end_pos < 0 || end_pos > entry->text_length)
2558     end_pos = entry->text_length;
2559   
2560   if (start_pos < end_pos)
2561     {
2562       gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2563       gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2564       gint current_pos;
2565       gint selection_bound;
2566
2567       g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2568       entry->text_length -= (end_pos - start_pos);
2569       entry->n_bytes -= (end_index - start_index);
2570
2571       /* In password-mode, make sure we don't leave anything sensitive after
2572        * the terminating zero.  Note, that the terminating zero already trashed
2573        * one byte.
2574        */
2575       if (!entry->visible)
2576         trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2577       
2578       current_pos = entry->current_pos;
2579       if (current_pos > start_pos)
2580         current_pos -= MIN (current_pos, end_pos) - start_pos;
2581
2582       selection_bound = entry->selection_bound;
2583       if (selection_bound > start_pos)
2584         selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2585
2586       gtk_entry_set_positions (entry, current_pos, selection_bound);
2587
2588       /* We might have deleted the selection
2589        */
2590       gtk_entry_update_primary_selection (entry);
2591       
2592       gtk_entry_recompute (entry);
2593
2594       emit_changed (entry);
2595       g_object_notify (G_OBJECT (editable), "text");
2596     }
2597 }
2598
2599 /* Compute the X position for an offset that corresponds to the "more important
2600  * cursor position for that offset. We use this when trying to guess to which
2601  * end of the selection we should go to when the user hits the left or
2602  * right arrow key.
2603  */
2604 static gint
2605 get_better_cursor_x (GtkEntry *entry,
2606                      gint      offset)
2607 {
2608   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2609   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2610   gboolean split_cursor;
2611   
2612   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2613   const gchar *text = pango_layout_get_text (layout);
2614   gint index = g_utf8_offset_to_pointer (text, offset) - text;
2615   
2616   PangoRectangle strong_pos, weak_pos;
2617   
2618   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2619                 "gtk-split-cursor", &split_cursor,
2620                 NULL);
2621
2622   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2623
2624   if (split_cursor)
2625     return strong_pos.x / PANGO_SCALE;
2626   else
2627     return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2628 }
2629
2630 static void
2631 gtk_entry_move_cursor (GtkEntry       *entry,
2632                        GtkMovementStep step,
2633                        gint            count,
2634                        gboolean        extend_selection)
2635 {
2636   gint new_pos = entry->current_pos;
2637
2638   _gtk_entry_reset_im_context (entry);
2639
2640   if (entry->current_pos != entry->selection_bound && !extend_selection)
2641     {
2642       /* If we have a current selection and aren't extending it, move to the
2643        * start/or end of the selection as appropriate
2644        */
2645       switch (step)
2646         {
2647         case GTK_MOVEMENT_VISUAL_POSITIONS:
2648           {
2649             gint current_x = get_better_cursor_x (entry, entry->current_pos);
2650             gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2651
2652             if (count <= 0)
2653               new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2654             else 
2655               new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2656             break;
2657           }
2658         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2659         case GTK_MOVEMENT_WORDS:
2660           if (count < 0)
2661             new_pos = MIN (entry->current_pos, entry->selection_bound);
2662           else
2663             new_pos = MAX (entry->current_pos, entry->selection_bound);
2664           break;
2665         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2666         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2667         case GTK_MOVEMENT_BUFFER_ENDS:
2668           new_pos = count < 0 ? 0 : entry->text_length;
2669           break;
2670         case GTK_MOVEMENT_DISPLAY_LINES:
2671         case GTK_MOVEMENT_PARAGRAPHS:
2672         case GTK_MOVEMENT_PAGES:
2673         case GTK_MOVEMENT_HORIZONTAL_PAGES:
2674           break;
2675         }
2676     }
2677   else
2678     {
2679       switch (step)
2680         {
2681         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2682           new_pos = gtk_entry_move_logically (entry, new_pos, count);
2683           break;
2684         case GTK_MOVEMENT_VISUAL_POSITIONS:
2685           new_pos = gtk_entry_move_visually (entry, new_pos, count);
2686           if (entry->current_pos == new_pos)
2687             {
2688               if (!extend_selection)
2689                 {
2690                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
2691                                                  count > 0 ?
2692                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
2693                     {
2694                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
2695
2696                       if (toplevel)
2697                         gtk_widget_child_focus (toplevel,
2698                                                 count > 0 ?
2699                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
2700                     }
2701                 }
2702               else
2703                 {
2704                   gtk_widget_error_bell (GTK_WIDGET (entry));
2705                 }
2706             }
2707           break;
2708         case GTK_MOVEMENT_WORDS:
2709           while (count > 0)
2710             {
2711               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2712               count--;
2713             }
2714           while (count < 0)
2715             {
2716               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2717               count++;
2718             }
2719           if (entry->current_pos == new_pos)
2720             gtk_widget_error_bell (GTK_WIDGET (entry));
2721           break;
2722         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2723         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2724         case GTK_MOVEMENT_BUFFER_ENDS:
2725           new_pos = count < 0 ? 0 : entry->text_length;
2726           if (entry->current_pos == new_pos)
2727             gtk_widget_error_bell (GTK_WIDGET (entry));
2728           break;
2729         case GTK_MOVEMENT_DISPLAY_LINES:
2730         case GTK_MOVEMENT_PARAGRAPHS:
2731         case GTK_MOVEMENT_PAGES:
2732         case GTK_MOVEMENT_HORIZONTAL_PAGES:
2733           break;
2734         }
2735     }
2736
2737   if (extend_selection)
2738     gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2739   else
2740     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2741   
2742   gtk_entry_pend_cursor_blink (entry);
2743 }
2744
2745 static void
2746 gtk_entry_insert_at_cursor (GtkEntry    *entry,
2747                             const gchar *str)
2748 {
2749   GtkEditable *editable = GTK_EDITABLE (entry);
2750   gint pos = entry->current_pos;
2751
2752   if (entry->editable)
2753     {
2754       _gtk_entry_reset_im_context (entry);
2755
2756       gtk_editable_insert_text (editable, str, -1, &pos);
2757       gtk_editable_set_position (editable, pos);
2758     }
2759 }
2760
2761 static void
2762 gtk_entry_delete_from_cursor (GtkEntry       *entry,
2763                               GtkDeleteType   type,
2764                               gint            count)
2765 {
2766   GtkEditable *editable = GTK_EDITABLE (entry);
2767   gint start_pos = entry->current_pos;
2768   gint end_pos = entry->current_pos;
2769   gint old_n_bytes = entry->n_bytes;
2770   
2771   _gtk_entry_reset_im_context (entry);
2772
2773   if (!entry->editable)
2774     {
2775       gtk_widget_error_bell (GTK_WIDGET (entry));
2776       return;
2777     }
2778
2779   if (entry->selection_bound != entry->current_pos)
2780     {
2781       gtk_editable_delete_selection (editable);
2782       return;
2783     }
2784   
2785   switch (type)
2786     {
2787     case GTK_DELETE_CHARS:
2788       end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2789       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2790       break;
2791     case GTK_DELETE_WORDS:
2792       if (count < 0)
2793         {
2794           /* Move to end of current word, or if not on a word, end of previous word */
2795           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2796           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2797         }
2798       else if (count > 0)
2799         {
2800           /* Move to beginning of current word, or if not on a word, begining of next word */
2801           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2802           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2803         }
2804         
2805       /* Fall through */
2806     case GTK_DELETE_WORD_ENDS:
2807       while (count < 0)
2808         {
2809           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2810           count++;
2811         }
2812       while (count > 0)
2813         {
2814           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2815           count--;
2816         }
2817       gtk_editable_delete_text (editable, start_pos, end_pos);
2818       break;
2819     case GTK_DELETE_DISPLAY_LINE_ENDS:
2820     case GTK_DELETE_PARAGRAPH_ENDS:
2821       if (count < 0)
2822         gtk_editable_delete_text (editable, 0, entry->current_pos);
2823       else
2824         gtk_editable_delete_text (editable, entry->current_pos, -1);
2825       break;
2826     case GTK_DELETE_DISPLAY_LINES:
2827     case GTK_DELETE_PARAGRAPHS:
2828       gtk_editable_delete_text (editable, 0, -1);  
2829       break;
2830     case GTK_DELETE_WHITESPACE:
2831       gtk_entry_delete_whitespace (entry);
2832       break;
2833     }
2834
2835   if (entry->n_bytes == old_n_bytes)
2836     gtk_widget_error_bell (GTK_WIDGET (entry));
2837
2838   gtk_entry_pend_cursor_blink (entry);
2839 }
2840
2841 static void
2842 gtk_entry_backspace (GtkEntry *entry)
2843 {
2844   GtkEditable *editable = GTK_EDITABLE (entry);
2845   gint prev_pos;
2846
2847   _gtk_entry_reset_im_context (entry);
2848
2849   if (!entry->editable || !entry->text)
2850     {
2851       gtk_widget_error_bell (GTK_WIDGET (entry));
2852       return;
2853     }
2854
2855   if (entry->selection_bound != entry->current_pos)
2856     {
2857       gtk_editable_delete_selection (editable);
2858       return;
2859     }
2860
2861   prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2862
2863   if (prev_pos < entry->current_pos)
2864     {
2865       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2866       PangoLogAttr *log_attrs;
2867       gint n_attrs;
2868
2869       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2870
2871       if (log_attrs[entry->current_pos].backspace_deletes_character)
2872         {
2873           gchar *cluster_text;
2874           gchar *normalized_text;
2875           glong  len;
2876
2877           cluster_text = gtk_editable_get_chars (editable,
2878                                                  prev_pos,
2879                                                  entry->current_pos);
2880           normalized_text = g_utf8_normalize (cluster_text,
2881                                               strlen (cluster_text),
2882                                               G_NORMALIZE_NFD);
2883           len = g_utf8_strlen (normalized_text, -1);
2884
2885           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2886           if (len > 1)
2887             {
2888               gint pos = entry->current_pos;
2889
2890               gtk_editable_insert_text (editable, normalized_text,
2891                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2892                                         &pos);
2893               gtk_editable_set_position (editable, pos);
2894             }
2895
2896           g_free (normalized_text);
2897           g_free (cluster_text);
2898         }
2899       else
2900         {
2901           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2902         }
2903       
2904       g_free (log_attrs);
2905     }
2906   else
2907     {
2908       gtk_widget_error_bell (GTK_WIDGET (entry));
2909     }
2910
2911   gtk_entry_pend_cursor_blink (entry);
2912 }
2913
2914 static void
2915 gtk_entry_copy_clipboard (GtkEntry *entry)
2916 {
2917   GtkEditable *editable = GTK_EDITABLE (entry);
2918   gint start, end;
2919
2920   if (gtk_editable_get_selection_bounds (editable, &start, &end))
2921     {
2922       gchar *str = gtk_entry_get_public_chars (entry, start, end);
2923       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2924                                                         GDK_SELECTION_CLIPBOARD),
2925                               str, -1);
2926       g_free (str);
2927     }
2928 }
2929
2930 static void
2931 gtk_entry_cut_clipboard (GtkEntry *entry)
2932 {
2933   GtkEditable *editable = GTK_EDITABLE (entry);
2934   gint start, end;
2935
2936   gtk_entry_copy_clipboard (entry);
2937
2938   if (entry->editable)
2939     {
2940       if (gtk_editable_get_selection_bounds (editable, &start, &end))
2941         gtk_editable_delete_text (editable, start, end);
2942     }
2943   else
2944     {
2945       gtk_widget_error_bell (GTK_WIDGET (entry));
2946     }
2947 }
2948
2949 static void
2950 gtk_entry_paste_clipboard (GtkEntry *entry)
2951 {
2952   if (entry->editable)
2953     gtk_entry_paste (entry, GDK_NONE);
2954   else
2955     gtk_widget_error_bell (GTK_WIDGET (entry));
2956 }
2957
2958 static void
2959 gtk_entry_delete_cb (GtkEntry *entry)
2960 {
2961   GtkEditable *editable = GTK_EDITABLE (entry);
2962   gint start, end;
2963
2964   if (entry->editable)
2965     {
2966       if (gtk_editable_get_selection_bounds (editable, &start, &end))
2967         gtk_editable_delete_text (editable, start, end);
2968     }
2969 }
2970
2971 static void
2972 gtk_entry_toggle_overwrite (GtkEntry *entry)
2973 {
2974   entry->overwrite_mode = !entry->overwrite_mode;
2975 }
2976
2977 static void
2978 gtk_entry_select_all (GtkEntry *entry)
2979 {
2980   gtk_entry_select_line (entry);
2981 }
2982
2983 static void
2984 gtk_entry_real_activate (GtkEntry *entry)
2985 {
2986   GtkWindow *window;
2987   GtkWidget *toplevel;
2988   GtkWidget *widget;
2989
2990   widget = GTK_WIDGET (entry);
2991
2992   if (entry->activates_default)
2993     {
2994       toplevel = gtk_widget_get_toplevel (widget);
2995       if (GTK_IS_WINDOW (toplevel))
2996         {
2997           window = GTK_WINDOW (toplevel);
2998       
2999           if (window &&
3000               widget != window->default_widget &&
3001               !(widget == window->focus_widget &&
3002                 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3003             gtk_window_activate_default (window);
3004         }
3005     }
3006 }
3007
3008 static void
3009 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
3010                                     GtkEntry  *entry)
3011 {
3012   gtk_entry_recompute (entry);
3013 }
3014
3015 /* IM Context Callbacks
3016  */
3017
3018 static void
3019 gtk_entry_commit_cb (GtkIMContext *context,
3020                      const gchar  *str,
3021                      GtkEntry     *entry)
3022 {
3023   if (entry->editable)
3024     gtk_entry_enter_text (entry, str);
3025 }
3026
3027 static void 
3028 gtk_entry_preedit_changed_cb (GtkIMContext *context,
3029                               GtkEntry     *entry)
3030 {
3031   if (entry->editable)
3032     {
3033       gchar *preedit_string;
3034       gint cursor_pos;
3035       
3036       gtk_im_context_get_preedit_string (entry->im_context,
3037                                          &preedit_string, NULL,
3038                                          &cursor_pos);
3039       entry->preedit_length = strlen (preedit_string);
3040       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
3041       entry->preedit_cursor = cursor_pos;
3042       g_free (preedit_string);
3043     
3044       gtk_entry_recompute (entry);
3045     }
3046 }
3047
3048 static gboolean
3049 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
3050                                GtkEntry     *entry)
3051 {
3052   gtk_im_context_set_surrounding (context,
3053                                   entry->text,
3054                                   entry->n_bytes,
3055                                   g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
3056
3057   return TRUE;
3058 }
3059
3060 static gboolean
3061 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
3062                                  gint          offset,
3063                                  gint          n_chars,
3064                                  GtkEntry     *entry)
3065 {
3066   if (entry->editable)
3067     gtk_editable_delete_text (GTK_EDITABLE (entry),
3068                               entry->current_pos + offset,
3069                               entry->current_pos + offset + n_chars);
3070
3071   return TRUE;
3072 }
3073
3074 /* Internal functions
3075  */
3076
3077 /* Used for im_commit_cb and inserting Unicode chars */
3078 static void
3079 gtk_entry_enter_text (GtkEntry       *entry,
3080                       const gchar    *str)
3081 {
3082   GtkEditable *editable = GTK_EDITABLE (entry);
3083   gint tmp_pos;
3084   gboolean old_need_im_reset;
3085
3086   old_need_im_reset = entry->need_im_reset;
3087   entry->need_im_reset = FALSE;
3088
3089   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
3090     gtk_editable_delete_selection (editable);
3091   else
3092     {
3093       if (entry->overwrite_mode)
3094         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
3095     }
3096
3097   tmp_pos = entry->current_pos;
3098   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
3099   gtk_editable_set_position (editable, tmp_pos);
3100
3101   entry->need_im_reset = old_need_im_reset;
3102 }
3103
3104 /* All changes to entry->current_pos and entry->selection_bound
3105  * should go through this function.
3106  */
3107 static void
3108 gtk_entry_set_positions (GtkEntry *entry,
3109                          gint      current_pos,
3110                          gint      selection_bound)
3111 {
3112   gboolean changed = FALSE;
3113
3114   g_object_freeze_notify (G_OBJECT (entry));
3115   
3116   if (current_pos != -1 &&
3117       entry->current_pos != current_pos)
3118     {
3119       entry->current_pos = current_pos;
3120       changed = TRUE;
3121
3122       g_object_notify (G_OBJECT (entry), "cursor-position");
3123     }
3124
3125   if (selection_bound != -1 &&
3126       entry->selection_bound != selection_bound)
3127     {
3128       entry->selection_bound = selection_bound;
3129       changed = TRUE;
3130       
3131       g_object_notify (G_OBJECT (entry), "selection-bound");
3132     }
3133
3134   g_object_thaw_notify (G_OBJECT (entry));
3135
3136   if (changed) 
3137     {
3138       gtk_entry_move_adjustments (entry);
3139       gtk_entry_recompute (entry);
3140     }
3141 }
3142
3143 static void
3144 gtk_entry_reset_layout (GtkEntry *entry)
3145 {
3146   if (entry->cached_layout)
3147     {
3148       g_object_unref (entry->cached_layout);
3149       entry->cached_layout = NULL;
3150     }
3151 }
3152
3153 static void
3154 update_im_cursor_location (GtkEntry *entry)
3155 {
3156   GdkRectangle area;
3157   gint strong_x;
3158   gint strong_xoffset;
3159   gint area_width, area_height;
3160
3161   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
3162 ;
3163   get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3164
3165   strong_xoffset = strong_x - entry->scroll_offset;
3166   if (strong_xoffset < 0)
3167     {
3168       strong_xoffset = 0;
3169     }
3170   else if (strong_xoffset > area_width)
3171     {
3172       strong_xoffset = area_width;
3173     }
3174   area.x = strong_xoffset;
3175   area.y = 0;
3176   area.width = 0;
3177   area.height = area_height;
3178
3179   gtk_im_context_set_cursor_location (entry->im_context, &area);
3180 }
3181
3182 static gboolean
3183 recompute_idle_func (gpointer data)
3184 {
3185   GtkEntry *entry;
3186
3187   entry = GTK_ENTRY (data);
3188
3189   entry->recompute_idle = 0;
3190   
3191   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3192     {
3193       gtk_entry_adjust_scroll (entry);
3194       gtk_entry_queue_draw (entry);
3195       
3196       update_im_cursor_location (entry);
3197     }
3198
3199   return FALSE;
3200 }
3201
3202 static void
3203 gtk_entry_recompute (GtkEntry *entry)
3204 {
3205   gtk_entry_reset_layout (entry);
3206   gtk_entry_check_cursor_blink (entry);
3207   
3208   if (!entry->recompute_idle)
3209     {
3210       entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3211                                                recompute_idle_func, entry, NULL); 
3212     }
3213 }
3214
3215 static void
3216 append_char (GString *str,
3217              gunichar ch,
3218              gint     count)
3219 {
3220   gint i;
3221   gint char_len;
3222   gchar buf[7];
3223   
3224   char_len = g_unichar_to_utf8 (ch, buf);
3225   
3226   i = 0;
3227   while (i < count)
3228     {
3229       g_string_append_len (str, buf, char_len);
3230       ++i;
3231     }
3232 }
3233
3234 static gboolean
3235 gtk_entry_remove_password_hint (gpointer data)
3236 {
3237   /* Force the string to be redrawn, but now without a visible character */
3238   gtk_entry_recompute (GTK_ENTRY (data));
3239
3240   return FALSE;
3241 }
3242
3243 static PangoLayout *
3244 gtk_entry_create_layout (GtkEntry *entry,
3245                          gboolean  include_preedit)
3246 {
3247   GtkWidget *widget = GTK_WIDGET (entry);
3248   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3249   PangoAttrList *tmp_attrs = pango_attr_list_new ();
3250   
3251   gchar *preedit_string = NULL;
3252   gint preedit_length = 0;
3253   PangoAttrList *preedit_attrs = NULL;
3254
3255   pango_layout_set_single_paragraph_mode (layout, TRUE);
3256
3257   if (include_preedit)
3258     {
3259       gtk_im_context_get_preedit_string (entry->im_context,
3260                                          &preedit_string, &preedit_attrs, NULL);
3261       preedit_length = entry->preedit_length;
3262     }
3263
3264   if (preedit_length)
3265     {
3266       GString *tmp_string = g_string_new (NULL);
3267       
3268       gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3269       
3270       if (entry->visible)
3271         {
3272           g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3273           g_string_insert (tmp_string, cursor_index, preedit_string);
3274         }
3275       else
3276         {
3277           gint ch_len;
3278           gint preedit_len_chars;
3279           gunichar invisible_char;
3280
3281           ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3282           preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3283           ch_len += preedit_len_chars;
3284
3285           if (entry->invisible_char != 0)
3286             invisible_char = entry->invisible_char;
3287           else
3288             invisible_char = ' '; /* just pick a char */
3289           
3290           append_char (tmp_string, invisible_char, ch_len);
3291           
3292           /* Fix cursor index to point to invisible char corresponding
3293            * to the preedit, fix preedit_length to be the length of
3294            * the invisible chars representing the preedit
3295            */
3296           cursor_index =
3297             g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3298             tmp_string->str;
3299           preedit_length =
3300             preedit_len_chars *
3301             g_unichar_to_utf8 (invisible_char, NULL);
3302         }
3303       
3304       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3305       
3306       pango_attr_list_splice (tmp_attrs, preedit_attrs,
3307                               cursor_index, preedit_length);
3308       
3309       g_string_free (tmp_string, TRUE);
3310     }
3311   else
3312     {
3313       PangoDirection pango_dir;
3314       
3315       if (entry->visible)
3316         pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3317
3318       else
3319         pango_dir = PANGO_DIRECTION_NEUTRAL;
3320
3321       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3322         {
3323           if (GTK_WIDGET_HAS_FOCUS (widget))
3324             {
3325               GdkDisplay *display = gtk_widget_get_display (widget);
3326               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3327               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3328                 pango_dir = PANGO_DIRECTION_RTL;
3329               else
3330                 pango_dir = PANGO_DIRECTION_LTR;
3331             }
3332           else
3333             {
3334               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3335                 pango_dir = PANGO_DIRECTION_RTL;
3336               else
3337                 pango_dir = PANGO_DIRECTION_LTR;
3338             }
3339         }
3340
3341       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3342                                   pango_dir);
3343
3344       pango_layout_set_alignment (layout, pango_dir);
3345
3346       entry->resolved_dir = pango_dir;
3347       
3348       if (entry->visible)
3349         {
3350           pango_layout_set_text (layout, entry->text, entry->n_bytes);
3351         }
3352       else
3353         {
3354           GString *str = g_string_new (NULL);
3355           gunichar invisible_char;
3356           guint password_hint_timeout;
3357           GtkEntryPasswordHint *password_hint;
3358
3359           g_object_get (gtk_widget_get_settings (widget),
3360                         "gtk-entry-password-hint-timeout", &password_hint_timeout,
3361                         NULL);
3362
3363           if (entry->invisible_char != 0)
3364             invisible_char = entry->invisible_char;
3365           else
3366             invisible_char = ' '; /* just pick a char */
3367
3368           password_hint = g_object_get_qdata (G_OBJECT (entry),
3369                                               quark_password_hint);
3370
3371           if (password_hint && password_hint->password_hint_timeout_id)
3372             {
3373               g_source_remove (password_hint->password_hint_timeout_id);
3374               password_hint->password_hint_timeout_id = 0;
3375             }
3376
3377           if (password_hint_timeout == 0 || password_hint == NULL ||
3378               (password_hint && password_hint->password_hint_length == 0))
3379             {
3380               append_char (str, invisible_char, entry->text_length);
3381             }
3382           else if (password_hint)
3383             {
3384               /* Draw hidden characters upto the inserted position,
3385                * then the real thing, pad up to full length
3386                */
3387               if (password_hint->password_hint_position > 1)
3388                 append_char (str, invisible_char,
3389                              password_hint->password_hint_position - 1);
3390
3391               g_string_append_len (str, password_hint->password_hint,
3392                                    password_hint->password_hint_length);
3393
3394               if (password_hint->password_hint_position < entry->text_length)
3395                 append_char (str, invisible_char,
3396                              entry->text_length -
3397                              password_hint->password_hint_position);
3398
3399               /* Now remove this last inputted character, don't need
3400                * it anymore
3401                */
3402               memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3403               password_hint->password_hint_length = 0;
3404
3405               password_hint->password_hint_timeout_id =
3406                 gdk_threads_add_timeout (password_hint_timeout,
3407                                (GSourceFunc) gtk_entry_remove_password_hint,
3408                                entry);
3409             }
3410
3411           pango_layout_set_text (layout, str->str, str->len);
3412           g_string_free (str, TRUE);
3413         }
3414     }
3415       
3416   pango_layout_set_attributes (layout, tmp_attrs);
3417
3418   g_free (preedit_string);
3419   if (preedit_attrs)
3420     pango_attr_list_unref (preedit_attrs);
3421       
3422   pango_attr_list_unref (tmp_attrs);
3423
3424   return layout;
3425 }
3426
3427 static PangoLayout *
3428 gtk_entry_ensure_layout (GtkEntry *entry,
3429                          gboolean  include_preedit)
3430 {
3431   if (entry->preedit_length > 0 &&
3432       !include_preedit != !entry->cache_includes_preedit)
3433     gtk_entry_reset_layout (entry);
3434
3435   if (!entry->cached_layout)
3436     {
3437       entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3438       entry->cache_includes_preedit = include_preedit;
3439     }
3440   
3441   return entry->cached_layout;
3442 }
3443
3444 static void
3445 get_layout_position (GtkEntry *entry,
3446                      gint     *x,
3447                      gint     *y)
3448 {
3449   PangoLayout *layout;
3450   PangoRectangle logical_rect;
3451   gint area_width, area_height;
3452   GtkBorder inner_border;
3453   gint y_pos;
3454   PangoLayoutLine *line;
3455   
3456   layout = gtk_entry_ensure_layout (entry, TRUE);
3457
3458   get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3459   _gtk_entry_effective_inner_border (entry, &inner_border);
3460
3461   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3462
3463   line = pango_layout_get_lines_readonly (layout)->data;
3464   pango_layout_line_get_extents (line, NULL, &logical_rect);
3465   
3466   /* Align primarily for locale's ascent/descent */
3467   y_pos = ((area_height - entry->ascent - entry->descent) / 2 + 
3468            entry->ascent + logical_rect.y);
3469   
3470   /* Now see if we need to adjust to fit in actual drawn string */
3471   if (logical_rect.height > area_height)
3472     y_pos = (area_height - logical_rect.height) / 2;
3473   else if (y_pos < 0)
3474     y_pos = 0;
3475   else if (y_pos + logical_rect.height > area_height)
3476     y_pos = area_height - logical_rect.height;
3477   
3478   y_pos = inner_border.top + y_pos / PANGO_SCALE;
3479
3480   if (x)
3481     *x = inner_border.left - entry->scroll_offset;
3482
3483   if (y)
3484     *y = y_pos;
3485 }
3486
3487 static void
3488 gtk_entry_draw_text (GtkEntry *entry)
3489 {
3490   GtkWidget *widget;
3491   
3492   if (!entry->visible && entry->invisible_char == 0)
3493     return;
3494   
3495   if (GTK_WIDGET_DRAWABLE (entry))
3496     {
3497       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3498       cairo_t *cr;
3499       gint x, y;
3500       gint start_pos, end_pos;
3501       
3502       widget = GTK_WIDGET (entry);
3503       
3504       get_layout_position (entry, &x, &y);
3505
3506       cr = gdk_cairo_create (entry->text_area);
3507
3508       cairo_move_to (cr, x, y);
3509       gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3510       pango_cairo_show_layout (cr, layout);
3511
3512       if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3513         {
3514           gint *ranges;
3515           gint n_ranges, i;
3516           PangoRectangle logical_rect;
3517           GdkColor *selection_color, *text_color;
3518           GtkBorder inner_border;
3519
3520           pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3521           gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3522
3523           if (GTK_WIDGET_HAS_FOCUS (entry))
3524             {
3525               selection_color = &widget->style->base [GTK_STATE_SELECTED];
3526               text_color = &widget->style->text [GTK_STATE_SELECTED];
3527             }
3528           else
3529             {
3530               selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3531               text_color = &widget->style->text [GTK_STATE_ACTIVE];
3532             }
3533
3534           _gtk_entry_effective_inner_border (entry, &inner_border);
3535
3536           for (i = 0; i < n_ranges; ++i)
3537             cairo_rectangle (cr,
3538                              inner_border.left - entry->scroll_offset + ranges[2 * i],
3539                              y,
3540                              ranges[2 * i + 1],
3541                              logical_rect.height);
3542
3543           cairo_clip (cr);
3544           
3545           gdk_cairo_set_source_color (cr, selection_color);
3546           cairo_paint (cr);
3547
3548           cairo_move_to (cr, x, y);
3549           gdk_cairo_set_source_color (cr, text_color);
3550           pango_cairo_show_layout (cr, layout);
3551           
3552           g_free (ranges);
3553         }
3554
3555       cairo_destroy (cr);
3556     }
3557 }
3558
3559 static void
3560 draw_insertion_cursor (GtkEntry      *entry,
3561                        GdkRectangle  *cursor_location,
3562                        gboolean       is_primary,
3563                        PangoDirection direction,
3564                        gboolean       draw_arrow)
3565 {
3566   GtkWidget *widget = GTK_WIDGET (entry);
3567   GtkTextDirection text_dir;
3568
3569   if (direction == PANGO_DIRECTION_LTR)
3570     text_dir = GTK_TEXT_DIR_LTR;
3571   else
3572     text_dir = GTK_TEXT_DIR_RTL;
3573
3574   gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3575                              cursor_location,
3576                              is_primary, text_dir, draw_arrow);
3577 }
3578
3579 static void
3580 gtk_entry_draw_cursor (GtkEntry  *entry,
3581                        CursorType type)
3582 {
3583   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3584   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3585   
3586   if (GTK_WIDGET_DRAWABLE (entry))
3587     {
3588       GtkWidget *widget = GTK_WIDGET (entry);
3589       GdkRectangle cursor_location;
3590       gboolean split_cursor;
3591
3592       GtkBorder inner_border;
3593       gint xoffset;
3594       gint strong_x, weak_x;
3595       gint text_area_height;
3596       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3597       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3598       gint x1 = 0;
3599       gint x2 = 0;
3600
3601       _gtk_entry_effective_inner_border (entry, &inner_border);
3602
3603       xoffset = inner_border.left - entry->scroll_offset;
3604
3605       gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3606       
3607       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3608
3609       g_object_get (gtk_widget_get_settings (widget),
3610                     "gtk-split-cursor", &split_cursor,
3611                     NULL);
3612
3613       dir1 = entry->resolved_dir;
3614       
3615       if (split_cursor)
3616         {
3617           x1 = strong_x;
3618
3619           if (weak_x != strong_x)
3620             {
3621               dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3622               x2 = weak_x;
3623             }
3624         }
3625       else
3626         {
3627           if (keymap_direction == entry->resolved_dir)
3628             x1 = strong_x;
3629           else
3630             x1 = weak_x;
3631         }
3632
3633       cursor_location.x = xoffset + x1;
3634       cursor_location.y = inner_border.top;
3635       cursor_location.width = 0;
3636       cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3637
3638       draw_insertion_cursor (entry,
3639                              &cursor_location, TRUE, dir1,
3640                              dir2 != PANGO_DIRECTION_NEUTRAL);
3641       
3642       if (dir2 != PANGO_DIRECTION_NEUTRAL)
3643         {
3644           cursor_location.x = xoffset + x2;
3645           draw_insertion_cursor (entry,
3646                                  &cursor_location, FALSE, dir2,
3647                                  TRUE);
3648         }
3649     }
3650 }
3651
3652 static void
3653 gtk_entry_queue_draw (GtkEntry *entry)
3654 {
3655   if (GTK_WIDGET_DRAWABLE (entry))
3656     gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3657 }
3658
3659 void
3660 _gtk_entry_reset_im_context (GtkEntry *entry)
3661 {
3662   if (entry->need_im_reset)
3663     {
3664       entry->need_im_reset = 0;
3665       gtk_im_context_reset (entry->im_context);
3666     }
3667 }
3668
3669 static gint
3670 gtk_entry_find_position (GtkEntry *entry,
3671                          gint      x)
3672 {
3673   PangoLayout *layout;
3674   PangoLayoutLine *line;
3675   gint index;
3676   gint pos;
3677   gint trailing;
3678   const gchar *text;
3679   gint cursor_index;
3680   
3681   layout = gtk_entry_ensure_layout (entry, TRUE);
3682   text = pango_layout_get_text (layout);
3683   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3684   
3685   line = pango_layout_get_lines_readonly (layout)->data;
3686   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3687
3688   if (index >= cursor_index && entry->preedit_length)
3689     {
3690       if (index >= cursor_index + entry->preedit_length)
3691         index -= entry->preedit_length;
3692       else
3693         {
3694           index = cursor_index;
3695           trailing = 0;
3696         }
3697     }
3698
3699   pos = g_utf8_pointer_to_offset (text, text + index);
3700   pos += trailing;
3701
3702   return pos;
3703 }
3704
3705 static void
3706 gtk_entry_get_cursor_locations (GtkEntry   *entry,
3707                                 CursorType  type,
3708                                 gint       *strong_x,
3709                                 gint       *weak_x)
3710 {
3711   if (!entry->visible && !entry->invisible_char)
3712     {
3713       if (strong_x)
3714         *strong_x = 0;
3715       
3716       if (weak_x)
3717         *weak_x = 0;
3718     }
3719   else
3720     {
3721       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3722       const gchar *text = pango_layout_get_text (layout);
3723       PangoRectangle strong_pos, weak_pos;
3724       gint index;
3725   
3726       if (type == CURSOR_STANDARD)
3727         {
3728           index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3729         }
3730       else /* type == CURSOR_DND */
3731         {
3732           index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3733
3734           if (entry->dnd_position > entry->current_pos)
3735             {
3736               if (entry->visible)
3737                 index += entry->preedit_length;
3738               else
3739                 {
3740                   gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3741                   index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3742                 }
3743             }
3744         }
3745       
3746       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3747       
3748       if (strong_x)
3749         *strong_x = strong_pos.x / PANGO_SCALE;
3750       
3751       if (weak_x)
3752         *weak_x = weak_pos.x / PANGO_SCALE;
3753     }
3754 }
3755
3756 static void
3757 gtk_entry_adjust_scroll (GtkEntry *entry)
3758 {
3759   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3760   gint min_offset, max_offset;
3761   gint text_area_width, text_width;
3762   GtkBorder inner_border;
3763   gint strong_x, weak_x;
3764   gint strong_xoffset, weak_xoffset;
3765   gfloat xalign;
3766   PangoLayout *layout;
3767   PangoLayoutLine *line;
3768   PangoRectangle logical_rect;
3769
3770   if (!GTK_WIDGET_REALIZED (entry))
3771     return;
3772
3773   _gtk_entry_effective_inner_border (entry, &inner_border);
3774
3775   gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3776   text_area_width -= inner_border.left + inner_border.right;
3777   if (text_area_width < 0)
3778     text_area_width = 0;
3779
3780   layout = gtk_entry_ensure_layout (entry, TRUE);
3781   line = pango_layout_get_lines_readonly (layout)->data;
3782
3783   pango_layout_line_get_extents (line, NULL, &logical_rect);
3784
3785   /* Display as much text as we can */
3786
3787   if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3788       xalign = priv->xalign;
3789   else
3790       xalign = 1.0 - priv->xalign;
3791
3792   text_width = PANGO_PIXELS(logical_rect.width);
3793
3794   if (text_width > text_area_width)
3795     {
3796       min_offset = 0;
3797       max_offset = text_width - text_area_width;
3798     }
3799   else
3800     {
3801       min_offset = (text_width - text_area_width) * xalign;
3802       max_offset = min_offset;
3803     }
3804
3805   entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3806
3807   /* And make sure cursors are on screen. Note that the cursor is
3808    * actually drawn one pixel into the INNER_BORDER space on
3809    * the right, when the scroll is at the utmost right. This
3810    * looks better to to me than confining the cursor inside the
3811    * border entirely, though it means that the cursor gets one
3812    * pixel closer to the edge of the widget on the right than
3813    * on the left. This might need changing if one changed
3814    * INNER_BORDER from 2 to 1, as one would do on a
3815    * small-screen-real-estate display.
3816    *
3817    * We always make sure that the strong cursor is on screen, and
3818    * put the weak cursor on screen if possible.
3819    */
3820
3821   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3822   
3823   strong_xoffset = strong_x - entry->scroll_offset;
3824
3825   if (strong_xoffset < 0)
3826     {
3827       entry->scroll_offset += strong_xoffset;
3828       strong_xoffset = 0;
3829     }
3830   else if (strong_xoffset > text_area_width)
3831     {
3832       entry->scroll_offset += strong_xoffset - text_area_width;
3833       strong_xoffset = text_area_width;
3834     }
3835
3836   weak_xoffset = weak_x - entry->scroll_offset;
3837
3838   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3839     {
3840       entry->scroll_offset += weak_xoffset;
3841     }
3842   else if (weak_xoffset > text_area_width &&
3843            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3844     {
3845       entry->scroll_offset += weak_xoffset - text_area_width;
3846     }
3847
3848   g_object_notify (G_OBJECT (entry), "scroll-offset");
3849 }
3850
3851 static void
3852 gtk_entry_move_adjustments (GtkEntry *entry)
3853 {
3854   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3855   PangoContext *context;
3856   PangoFontMetrics *metrics;
3857   gint x, layout_x, border_x, border_y;
3858   gint char_width;
3859
3860   if (!priv->cursor_hadjustment)
3861     return;
3862
3863   /* Cursor position, layout offset, border width, and widget allocation */
3864   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
3865   get_layout_position (entry, &layout_x, NULL);
3866   _gtk_entry_get_borders (entry, &border_x, &border_y);
3867   x += entry->widget.allocation.x + layout_x + border_x;
3868
3869   /* Approximate width of a char, so user can see what is ahead/behind */
3870   context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
3871   metrics = pango_context_get_metrics (context, 
3872                                        entry->widget.style->font_desc,
3873                                        pango_context_get_language (context));
3874   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
3875
3876   /* Scroll it */
3877   gtk_adjustment_clamp_page (priv->cursor_hadjustment, 
3878                              x - (char_width + 1),   /* one char + one pixel before */
3879                              x + (char_width + 2));  /* one char + cursor + one pixel after */
3880 }
3881
3882 static gint
3883 gtk_entry_move_visually (GtkEntry *entry,
3884                          gint      start,
3885                          gint      count)
3886 {
3887   gint index;
3888   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3889   const gchar *text;
3890
3891   text = pango_layout_get_text (layout);
3892   
3893   index = g_utf8_offset_to_pointer (text, start) - text;
3894
3895   while (count != 0)
3896     {
3897       int new_index, new_trailing;
3898       gboolean split_cursor;
3899       gboolean strong;
3900
3901       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3902                     "gtk-split-cursor", &split_cursor,
3903                     NULL);
3904
3905       if (split_cursor)
3906         strong = TRUE;
3907       else
3908         {
3909           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3910           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3911
3912           strong = keymap_direction == entry->resolved_dir;
3913         }
3914       
3915       if (count > 0)
3916         {
3917           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3918           count--;
3919         }
3920       else
3921         {
3922           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3923           count++;
3924         }
3925
3926       if (new_index < 0)
3927         index = 0;
3928       else if (new_index != G_MAXINT)
3929         index = new_index;
3930       
3931       while (new_trailing--)
3932         index = g_utf8_next_char (text + index) - text;
3933     }
3934   
3935   return g_utf8_pointer_to_offset (text, text + index);
3936 }
3937
3938 static gint
3939 gtk_entry_move_logically (GtkEntry *entry,
3940                           gint      start,
3941                           gint      count)
3942 {
3943   gint new_pos = start;
3944
3945   /* Prevent any leak of information */
3946   if (!entry->visible)
3947     {
3948       new_pos = CLAMP (start + count, 0, entry->text_length);
3949     }
3950   else if (entry->text)
3951     {
3952       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3953       PangoLogAttr *log_attrs;
3954       gint n_attrs;
3955
3956       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3957
3958       while (count > 0 && new_pos < entry->text_length)
3959         {
3960           do
3961             new_pos++;
3962           while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3963           
3964           count--;
3965         }
3966       while (count < 0 && new_pos > 0)
3967         {
3968           do
3969             new_pos--;
3970           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3971           
3972           count++;
3973         }
3974       
3975       g_free (log_attrs);
3976     }
3977
3978   return new_pos;
3979 }
3980
3981 static gint
3982 gtk_entry_move_forward_word (GtkEntry *entry,
3983                              gint      start,
3984                              gboolean  allow_whitespace)
3985 {
3986   gint new_pos = start;
3987
3988   /* Prevent any leak of information */
3989   if (!entry->visible)
3990     {
3991       new_pos = entry->text_length;
3992     }
3993   else if (entry->text && (new_pos < entry->text_length))
3994     {
3995       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3996       PangoLogAttr *log_attrs;
3997       gint n_attrs;
3998
3999       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4000       
4001       /* Find the next word boundary */
4002       new_pos++;
4003       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
4004                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
4005         new_pos++;
4006
4007       g_free (log_attrs);
4008     }
4009
4010   return new_pos;
4011 }
4012
4013
4014 static gint
4015 gtk_entry_move_backward_word (GtkEntry *entry,
4016                               gint      start,
4017                               gboolean  allow_whitespace)
4018 {
4019   gint new_pos = start;
4020
4021   /* Prevent any leak of information */
4022   if (!entry->visible)
4023     {
4024       new_pos = 0;
4025     }
4026   else if (entry->text && start > 0)
4027     {
4028       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4029       PangoLogAttr *log_attrs;
4030       gint n_attrs;
4031
4032       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4033
4034       new_pos = start - 1;
4035
4036       /* Find the previous word boundary */
4037       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
4038                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
4039         new_pos--;
4040
4041       g_free (log_attrs);
4042     }
4043
4044   return new_pos;
4045 }
4046
4047 static void
4048 gtk_entry_delete_whitespace (GtkEntry *entry)
4049 {
4050   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4051   PangoLogAttr *log_attrs;
4052   gint n_attrs;
4053   gint start, end;
4054
4055   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4056
4057   start = end = entry->current_pos;
4058   
4059   while (start > 0 && log_attrs[start-1].is_white)
4060     start--;
4061
4062   while (end < n_attrs && log_attrs[end].is_white)
4063     end++;
4064
4065   g_free (log_attrs);
4066
4067   if (start != end)
4068     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
4069 }
4070
4071
4072 static void
4073 gtk_entry_select_word (GtkEntry *entry)
4074 {
4075   gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
4076   gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
4077
4078   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
4079 }
4080
4081 static void
4082 gtk_entry_select_line (GtkEntry *entry)
4083 {
4084   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
4085 }
4086
4087 /*
4088  * Like gtk_editable_get_chars, but handle not-visible entries
4089  * correctly.
4090  */
4091 static char *    
4092 gtk_entry_get_public_chars (GtkEntry *entry,
4093                             gint      start,
4094                             gint      end)
4095 {
4096   if (end < 0)
4097     end = entry->text_length;
4098   
4099   if (entry->visible)
4100     return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
4101   else if (!entry->invisible_char)
4102     return g_strdup ("");
4103   else
4104     {
4105       GString *str = g_string_new (NULL);
4106       append_char (str, entry->invisible_char, end - start);
4107       return g_string_free (str, FALSE);
4108     }
4109 }
4110
4111 static gint
4112 truncate_multiline (const gchar *text)
4113 {
4114   gint length;
4115
4116   for (length = 0;
4117        text[length] && text[length] != '\n' && text[length] != '\r';
4118        length++);
4119
4120   return length;
4121 }
4122
4123 static void
4124 paste_received (GtkClipboard *clipboard,
4125                 const gchar  *text,
4126                 gpointer      data)
4127 {
4128   GtkEntry *entry = GTK_ENTRY (data);
4129   GtkEditable *editable = GTK_EDITABLE (entry);
4130   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4131       
4132   if (entry->button == 2)
4133     {
4134       gint pos, start, end;
4135       pos = priv->insert_pos;
4136       gtk_editable_get_selection_bounds (editable, &start, &end);
4137       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
4138         gtk_editable_select_region (editable, pos, pos);
4139     }
4140       
4141   if (text)
4142     {
4143       gint pos, start, end;
4144       gint length = -1;
4145       gboolean popup_completion;
4146       GtkEntryCompletion *completion;
4147
4148       completion = gtk_entry_get_completion (entry);
4149
4150       if (entry->truncate_multiline)
4151         length = truncate_multiline (text);
4152
4153       /* only complete if the selection is at the end */
4154       popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
4155
4156       if (completion)
4157         {
4158           if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
4159             _gtk_entry_completion_popdown (completion);
4160
4161           if (!popup_completion && completion->priv->changed_id > 0)
4162             g_signal_handler_block (entry, completion->priv->changed_id);
4163         }
4164
4165       begin_change (entry);
4166       g_object_freeze_notify (G_OBJECT (entry));
4167       if (gtk_editable_get_selection_bounds (editable, &start, &end))
4168         gtk_editable_delete_text (editable, start, end);
4169
4170       pos = entry->current_pos;
4171       gtk_editable_insert_text (editable, text, length, &pos);
4172       gtk_editable_set_position (editable, pos);
4173       g_object_thaw_notify (G_OBJECT (entry));
4174       end_change (entry);
4175
4176       if (completion &&
4177           !popup_completion && completion->priv->changed_id > 0)
4178         g_signal_handler_unblock (entry, completion->priv->changed_id);
4179     }
4180
4181   g_object_unref (entry);
4182 }
4183
4184 static void
4185 gtk_entry_paste (GtkEntry *entry,
4186                  GdkAtom   selection)
4187 {
4188   g_object_ref (entry);
4189   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
4190                               paste_received, entry);
4191 }
4192
4193 static void
4194 primary_get_cb (GtkClipboard     *clipboard,
4195                 GtkSelectionData *selection_data,
4196                 guint             info,
4197                 gpointer          data)
4198 {
4199   GtkEntry *entry = GTK_ENTRY (data);
4200   gint start, end;
4201   
4202   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4203     {
4204       gchar *str = gtk_entry_get_public_chars (entry, start, end);
4205       gtk_selection_data_set_text (selection_data, str, -1);
4206       g_free (str);
4207     }
4208 }
4209
4210 static void
4211 primary_clear_cb (GtkClipboard *clipboard,
4212                   gpointer      data)
4213 {
4214   GtkEntry *entry = GTK_ENTRY (data);
4215
4216   gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4217 }
4218
4219 static void
4220 gtk_entry_update_primary_selection (GtkEntry *entry)
4221 {
4222   static GtkTargetEntry targets[] = {
4223     { "UTF8_STRING", 0, 0 },
4224     { "STRING", 0, 0 },
4225     { "TEXT",   0, 0 }, 
4226     { "COMPOUND_TEXT", 0, 0 },
4227     { "text/plain;charset=utf-8",   0, 0 }, 
4228     { NULL,   0, 0 },
4229     { "text/plain", 0, 0 }
4230   };
4231   
4232   GtkClipboard *clipboard;
4233   gint start, end;
4234
4235   if (targets[5].target == NULL)
4236     {
4237       const gchar *charset;
4238
4239       g_get_charset (&charset);
4240       targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4241     }
4242
4243   if (!GTK_WIDGET_REALIZED (entry))
4244     return;
4245
4246   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4247   
4248   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4249     {
4250       if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4251                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4252         primary_clear_cb (clipboard, entry);
4253     }
4254   else
4255     {
4256       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4257         gtk_clipboard_clear (clipboard);
4258     }
4259 }
4260
4261 /* Public API
4262  */
4263
4264 /**
4265  * gtk_entry_new:
4266  *
4267  * Creates a new entry.
4268  *
4269  * Return value: a new #GtkEntry.
4270  */
4271 GtkWidget*
4272 gtk_entry_new (void)
4273 {
4274   return g_object_new (GTK_TYPE_ENTRY, NULL);
4275 }
4276
4277 /**
4278  * gtk_entry_new_with_max_length:
4279  * @max: the maximum length of the entry, or 0 for no maximum.
4280  *   (other than the maximum length of entries.) The value passed in will
4281  *   be clamped to the range 0-65536.
4282  *
4283  * Creates a new #GtkEntry widget with the given maximum length.
4284  * 
4285  * Note: the existence of this function is inconsistent
4286  * with the rest of the GTK+ API. The normal setup would
4287  * be to just require the user to make an extra call
4288  * to gtk_entry_set_max_length() instead. It is not
4289  * expected that this function will be removed, but
4290  * it would be better practice not to use it.
4291  * 
4292  * Return value: a new #GtkEntry.
4293  **/
4294 GtkWidget*
4295 gtk_entry_new_with_max_length (gint max)
4296 {
4297   GtkEntry *entry;
4298
4299   max = CLAMP (max, 0, MAX_SIZE);
4300
4301   entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4302   entry->text_max_length = max;
4303
4304   return GTK_WIDGET (entry);
4305 }
4306
4307 /**
4308  * gtk_entry_set_text:
4309  * @entry: a #GtkEntry
4310  * @text: the new text
4311  *
4312  * Sets the text in the widget to the given
4313  * value, replacing the current contents.
4314  */
4315 void
4316 gtk_entry_set_text (GtkEntry    *entry,
4317                     const gchar *text)
4318 {
4319   gint tmp_pos;
4320   GtkEntryCompletion *completion;
4321
4322   g_return_if_fail (GTK_IS_ENTRY (entry));
4323   g_return_if_fail (text != NULL);
4324
4325   /* Actually setting the text will affect the cursor and selection;
4326    * if the contents don't actually change, this will look odd to the user.
4327    */
4328   if (strcmp (entry->text, text) == 0)
4329     return;
4330
4331   completion = gtk_entry_get_completion (entry);
4332   if (completion && completion->priv->changed_id > 0)
4333     g_signal_handler_block (entry, completion->priv->changed_id);
4334
4335   begin_change (entry);
4336   g_object_freeze_notify (G_OBJECT (entry));
4337   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4338
4339   tmp_pos = 0;
4340   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4341   g_object_thaw_notify (G_OBJECT (entry));
4342   end_change (entry);
4343
4344   if (completion && completion->priv->changed_id > 0)
4345     g_signal_handler_unblock (entry, completion->priv->changed_id);
4346 }
4347
4348 /**
4349  * gtk_entry_append_text:
4350  * @entry: a #GtkEntry
4351  * @text: the text to append
4352  *
4353  * Appends the given text to the contents of the widget.
4354  *
4355  * Deprecated: gtk_entry_append_text() is deprecated and should not
4356  *   be used in newly-written code. Use gtk_editable_insert_text()
4357  *   instead.
4358  */
4359 void
4360 gtk_entry_append_text (GtkEntry *entry,
4361                        const gchar *text)
4362 {
4363   gint tmp_pos;
4364
4365   g_return_if_fail (GTK_IS_ENTRY (entry));
4366   g_return_if_fail (text != NULL);
4367
4368   tmp_pos = entry->text_length;
4369   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4370 }
4371
4372 /**
4373  * gtk_entry_prepend_text:
4374  * @entry: a #GtkEntry
4375  * @text: the text to prepend
4376  *
4377  * Prepends the given text to the contents of the widget.
4378  *
4379  * Deprecated: gtk_entry_prepend_text() is deprecated and should not
4380  *    be used in newly-written code. Use gtk_editable_insert_text()
4381  *    instead.
4382  */
4383 void
4384 gtk_entry_prepend_text (GtkEntry *entry,
4385                         const gchar *text)
4386 {
4387   gint tmp_pos;
4388
4389   g_return_if_fail (GTK_IS_ENTRY (entry));
4390   g_return_if_fail (text != NULL);
4391
4392   tmp_pos = 0;
4393   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4394 }
4395
4396 /**
4397  * gtk_entry_set_position:
4398  * @entry: a #GtkEntry
4399  * @position:  the position of the cursor. The cursor is displayed
4400  *    before the character with the given (base 0) index in the widget. 
4401  *    The value must be less than or equal to the number of characters 
4402  *    in the widget. A value of -1 indicates that the position should
4403  *    be set after the last character in the entry. Note that this 
4404  *    position is in characters, not in bytes.
4405  *
4406  * Sets the cursor position in an entry to the given value. 
4407  *
4408  * Deprecated: Use gtk_editable_set_position() instead.
4409  */
4410 void
4411 gtk_entry_set_position (GtkEntry *entry,
4412                         gint       position)
4413 {
4414   g_return_if_fail (GTK_IS_ENTRY (entry));
4415
4416   gtk_editable_set_position (GTK_EDITABLE (entry), position);
4417 }
4418
4419 /**
4420  * gtk_entry_set_visibility:
4421  * @entry: a #GtkEntry
4422  * @visible: %TRUE if the contents of the entry are displayed
4423  *           as plaintext
4424  *
4425  * Sets whether the contents of the entry are visible or not. 
4426  * When visibility is set to %FALSE, characters are displayed 
4427  * as the invisible char, and will also appear that way when 
4428  * the text in the entry widget is copied elsewhere.
4429  *
4430  * The default invisible char is the asterisk '*', but it can
4431  * be changed with gtk_entry_set_invisible_char().
4432  */
4433 void
4434 gtk_entry_set_visibility (GtkEntry *entry,
4435                           gboolean visible)
4436 {
4437   g_return_if_fail (GTK_IS_ENTRY (entry));
4438
4439   visible = visible != FALSE;
4440
4441   if (entry->visible != visible)
4442     {
4443       if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4444         gtk_im_context_focus_out (entry->im_context);
4445
4446       g_object_unref (entry->im_context);
4447
4448       if (visible)
4449         entry->im_context = gtk_im_multicontext_new ();
4450       else
4451         entry->im_context = gtk_im_context_simple_new ();
4452       
4453       g_signal_connect (entry->im_context, "commit",
4454                         G_CALLBACK (gtk_entry_commit_cb), entry);
4455       g_signal_connect (entry->im_context, "preedit_changed",
4456                         G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4457       g_signal_connect (entry->im_context, "retrieve_surrounding",
4458                         G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4459       g_signal_connect (entry->im_context, "delete_surrounding",
4460                         G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4461
4462       if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4463         gtk_im_context_focus_in (entry->im_context); 
4464
4465       entry->visible = visible;
4466
4467       g_object_notify (G_OBJECT (entry), "visibility");
4468       gtk_entry_recompute (entry);
4469     }
4470 }
4471
4472 /**
4473  * gtk_entry_get_visibility:
4474  * @entry: a #GtkEntry
4475  *
4476  * Retrieves whether the text in @entry is visible. See
4477  * gtk_entry_set_visibility().
4478  *
4479  * Return value: %TRUE if the text is currently visible
4480  **/
4481 gboolean
4482 gtk_entry_get_visibility (GtkEntry *entry)
4483 {
4484   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4485
4486   return entry->visible;
4487 }
4488
4489 /**
4490  * gtk_entry_set_invisible_char:
4491  * @entry: a #GtkEntry
4492  * @ch: a Unicode character
4493  * 
4494  * Sets the character to use in place of the actual text when
4495  * gtk_entry_set_visibility() has been called to set text visibility
4496  * to %FALSE. i.e. this is the character used in "password mode" to
4497  * show the user how many characters have been typed. The default
4498  * invisible char is an asterisk ('*').  If you set the invisible char
4499  * to 0, then the user will get no feedback at all; there will be
4500  * no text on the screen as they type.
4501  **/
4502 void
4503 gtk_entry_set_invisible_char (GtkEntry *entry,
4504                               gunichar  ch)
4505 {
4506   g_return_if_fail (GTK_IS_ENTRY (entry));
4507
4508   if (ch == entry->invisible_char)
4509     return;
4510
4511   entry->invisible_char = ch;
4512   g_object_notify (G_OBJECT (entry), "invisible-char");
4513   gtk_entry_recompute (entry);  
4514 }
4515
4516 /**
4517  * gtk_entry_get_invisible_char:
4518  * @entry: a #GtkEntry
4519  *
4520  * Retrieves the character displayed in place of the real characters
4521  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
4522  *
4523  * Return value: the current invisible char, or 0, if the entry does not
4524  *               show invisible text at all. 
4525  **/
4526 gunichar
4527 gtk_entry_get_invisible_char (GtkEntry *entry)
4528 {
4529   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4530
4531   return entry->invisible_char;
4532 }
4533
4534 /**
4535  * gtk_entry_set_editable:
4536  * @entry: a #GtkEntry
4537  * @editable: %TRUE if the user is allowed to edit the text
4538  *   in the widget
4539  *
4540  * Determines if the user can edit the text in the editable
4541  * widget or not. 
4542  *
4543  * Deprecated: Use gtk_editable_set_editable() instead.
4544  */
4545 void
4546 gtk_entry_set_editable (GtkEntry *entry,
4547                         gboolean  editable)
4548 {
4549   g_return_if_fail (GTK_IS_ENTRY (entry));
4550
4551   gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4552 }
4553
4554 /**
4555  * gtk_entry_get_text:
4556  * @entry: a #GtkEntry
4557  *
4558  * Retrieves the contents of the entry widget.
4559  * See also gtk_editable_get_chars().
4560  *
4561  * Return value: a pointer to the contents of the widget as a
4562  *      string. This string points to internally allocated
4563  *      storage in the widget and must not be freed, modified or
4564  *      stored.
4565  **/
4566 G_CONST_RETURN gchar*
4567 gtk_entry_get_text (GtkEntry *entry)
4568 {
4569   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4570
4571   return entry->text;
4572 }
4573
4574 /**
4575  * gtk_entry_select_region:
4576  * @entry: a #GtkEntry
4577  * @start: the starting position
4578  * @end: the end position
4579  *
4580  * Selects a region of text. The characters that are selected are 
4581  * those characters at positions from @start_pos up to, but not 
4582  * including @end_pos. If @end_pos is negative, then the the characters 
4583  * selected will be those characters from @start_pos to the end of 
4584  * the text. 
4585  *
4586  * Deprecated: Use gtk_editable_select_region() instead.
4587  */
4588 void       
4589 gtk_entry_select_region  (GtkEntry       *entry,
4590                           gint            start,
4591                           gint            end)
4592 {
4593   gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4594 }
4595
4596 /**
4597  * gtk_entry_set_max_length:
4598  * @entry: a #GtkEntry
4599  * @max: the maximum length of the entry, or 0 for no maximum.
4600  *   (other than the maximum length of entries.) The value passed in will
4601  *   be clamped to the range 0-65536.
4602  * 
4603  * Sets the maximum allowed length of the contents of the widget. If
4604  * the current contents are longer than the given length, then they
4605  * will be truncated to fit.
4606  **/
4607 void
4608 gtk_entry_set_max_length (GtkEntry     *entry,
4609                           gint          max)
4610 {
4611   g_return_if_fail (GTK_IS_ENTRY (entry));
4612
4613   max = CLAMP (max, 0, MAX_SIZE);
4614
4615   if (max > 0 && entry->text_length > max)
4616     gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4617   
4618   entry->text_max_length = max;
4619   g_object_notify (G_OBJECT (entry), "max-length");
4620 }
4621
4622 /**
4623  * gtk_entry_get_max_length:
4624  * @entry: a #GtkEntry
4625  *
4626  * Retrieves the maximum allowed length of the text in
4627  * @entry. See gtk_entry_set_max_length().
4628  *
4629  * Return value: the maximum allowed number of characters
4630  *               in #GtkEntry, or 0 if there is no maximum.
4631  **/
4632 gint
4633 gtk_entry_get_max_length (GtkEntry *entry)
4634 {
4635   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4636
4637   return entry->text_max_length;
4638 }
4639
4640 /**
4641  * gtk_entry_set_activates_default:
4642  * @entry: a #GtkEntry
4643  * @setting: %TRUE to activate window's default widget on Enter keypress
4644  *
4645  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4646  * widget for the window containing the entry. This usually means that
4647  * the dialog box containing the entry will be closed, since the default
4648  * widget is usually one of the dialog buttons.
4649  *
4650  * (For experts: if @setting is %TRUE, the entry calls
4651  * gtk_window_activate_default() on the window containing the entry, in
4652  * the default handler for the #GtkWidget::activate signal.)
4653  **/
4654 void
4655 gtk_entry_set_activates_default (GtkEntry *entry,
4656                                  gboolean  setting)
4657 {
4658   g_return_if_fail (GTK_IS_ENTRY (entry));
4659   setting = setting != FALSE;
4660
4661   if (setting != entry->activates_default)
4662     {
4663       entry->activates_default = setting;
4664       g_object_notify (G_OBJECT (entry), "activates-default");
4665     }
4666 }
4667
4668 /**
4669  * gtk_entry_get_activates_default:
4670  * @entry: a #GtkEntry
4671  * 
4672  * Retrieves the value set by gtk_entry_set_activates_default().
4673  * 
4674  * Return value: %TRUE if the entry will activate the default widget
4675  **/
4676 gboolean
4677 gtk_entry_get_activates_default (GtkEntry *entry)
4678 {
4679   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4680
4681   return entry->activates_default;
4682 }
4683
4684 /**
4685  * gtk_entry_set_width_chars:
4686  * @entry: a #GtkEntry
4687  * @n_chars: width in chars
4688  *
4689  * Changes the size request of the entry to be about the right size
4690  * for @n_chars characters. Note that it changes the size
4691  * <emphasis>request</emphasis>, the size can still be affected by
4692  * how you pack the widget into containers. If @n_chars is -1, the
4693  * size reverts to the default entry size.
4694  **/
4695 void
4696 gtk_entry_set_width_chars (GtkEntry *entry,
4697                            gint      n_chars)
4698 {
4699   g_return_if_fail (GTK_IS_ENTRY (entry));
4700
4701   if (entry->width_chars != n_chars)
4702     {
4703       entry->width_chars = n_chars;
4704       g_object_notify (G_OBJECT (entry), "width-chars");
4705       gtk_widget_queue_resize (GTK_WIDGET (entry));
4706     }
4707 }
4708
4709 /**
4710  * gtk_entry_get_width_chars:
4711  * @entry: a #GtkEntry
4712  * 
4713  * Gets the value set by gtk_entry_set_width_chars().
4714  * 
4715  * Return value: number of chars to request space for, or negative if unset
4716  **/
4717 gint
4718 gtk_entry_get_width_chars (GtkEntry *entry)
4719 {
4720   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4721
4722   return entry->width_chars;
4723 }
4724
4725 /**
4726  * gtk_entry_set_has_frame:
4727  * @entry: a #GtkEntry
4728  * @setting: new value
4729  * 
4730  * Sets whether the entry has a beveled frame around it.
4731  **/
4732 void
4733 gtk_entry_set_has_frame (GtkEntry *entry,
4734                          gboolean  setting)
4735 {
4736   g_return_if_fail (GTK_IS_ENTRY (entry));
4737
4738   setting = (setting != FALSE);
4739
4740   if (entry->has_frame == setting)
4741     return;
4742
4743   gtk_widget_queue_resize (GTK_WIDGET (entry));
4744   entry->has_frame = setting;
4745   g_object_notify (G_OBJECT (entry), "has-frame");
4746 }
4747
4748 /**
4749  * gtk_entry_get_has_frame:
4750  * @entry: a #GtkEntry
4751  * 
4752  * Gets the value set by gtk_entry_set_has_frame().
4753  * 
4754  * Return value: whether the entry has a beveled frame
4755  **/
4756 gboolean
4757 gtk_entry_get_has_frame (GtkEntry *entry)
4758 {
4759   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4760
4761   return entry->has_frame;
4762 }
4763
4764 /**
4765  * gtk_entry_set_inner_border:
4766  * @entry: a #GtkEntry
4767  * @border: a #GtkBorder, or %NULL
4768  *
4769  * Sets %entry's inner-border property to %border, or clears it if %NULL
4770  * is passed. The inner-border is the area around the entry's text, but
4771  * inside its frame.
4772  *
4773  * If set, this property overrides the inner-border style property.
4774  * Overriding the style-provided border is useful when you want to do
4775  * in-place editing of some text in a canvas or list widget, where
4776  * pixel-exact positioning of the entry is important.
4777  *
4778  * Since: 2.10
4779  **/
4780 void
4781 gtk_entry_set_inner_border (GtkEntry        *entry,
4782                             const GtkBorder *border)
4783 {
4784   g_return_if_fail (GTK_IS_ENTRY (entry));
4785
4786   gtk_widget_queue_resize (GTK_WIDGET (entry));
4787
4788   if (border)
4789     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4790                              gtk_border_copy (border),
4791                              (GDestroyNotify) gtk_border_free);
4792   else
4793     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4794
4795   g_object_notify (G_OBJECT (entry), "inner-border");
4796 }
4797
4798 /**
4799  * gtk_entry_get_inner_border:
4800  * @entry: a #GtkEntry
4801  *
4802  * This function returns the entry's #GtkEntry:inner-border property. See
4803  * gtk_entry_set_inner_border() for more information.
4804  *
4805  * Return value: the entry's #GtkBorder, or %NULL if none was set.
4806  *
4807  * Since: 2.10
4808  **/
4809 G_CONST_RETURN GtkBorder *
4810 gtk_entry_get_inner_border (GtkEntry *entry)
4811 {
4812   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4813
4814   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4815 }
4816
4817 /**
4818  * gtk_entry_get_layout:
4819  * @entry: a #GtkEntry
4820  * 
4821  * Gets the #PangoLayout used to display the entry.
4822  * The layout is useful to e.g. convert text positions to
4823  * pixel positions, in combination with gtk_entry_get_layout_offsets().
4824  * The returned layout is owned by the entry and must not be 
4825  * modified or freed by the caller.
4826  *
4827  * Keep in mind that the layout text may contain a preedit string, so
4828  * gtk_entry_layout_index_to_text_index() and
4829  * gtk_entry_text_index_to_layout_index() are needed to convert byte
4830  * indices in the layout to byte indices in the entry contents.
4831  * 
4832  * Return value: the #PangoLayout for this entry
4833  **/
4834 PangoLayout*
4835 gtk_entry_get_layout (GtkEntry *entry)
4836 {
4837   PangoLayout *layout;
4838   
4839   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4840
4841   layout = gtk_entry_ensure_layout (entry, TRUE);
4842
4843   return layout;
4844 }
4845
4846
4847 /**
4848  * gtk_entry_layout_index_to_text_index:
4849  * @entry: a #GtkEntry
4850  * @layout_index: byte index into the entry layout text
4851  * 
4852  * Converts from a position in the entry contents (returned
4853  * by gtk_entry_get_text()) to a position in the
4854  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4855  * with text retrieved via pango_layout_get_text()).
4856  * 
4857  * Return value: byte index into the entry contents
4858  **/
4859 gint
4860 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4861                                       gint      layout_index)
4862 {
4863   PangoLayout *layout;
4864   const gchar *text;
4865   gint cursor_index;
4866   
4867   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4868
4869   layout = gtk_entry_ensure_layout (entry, TRUE);
4870   text = pango_layout_get_text (layout);
4871   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4872   
4873   if (layout_index >= cursor_index && entry->preedit_length)
4874     {
4875       if (layout_index >= cursor_index + entry->preedit_length)
4876         layout_index -= entry->preedit_length;
4877       else
4878         layout_index = cursor_index;
4879     }
4880
4881   return layout_index;
4882 }
4883
4884 /**
4885  * gtk_entry_text_index_to_layout_index:
4886  * @entry: a #GtkEntry
4887  * @text_index: byte index into the entry contents
4888  * 
4889  * Converts from a position in the entry's #PangoLayout (returned by
4890  * gtk_entry_get_layout()) to a position in the entry contents
4891  * (returned by gtk_entry_get_text()).
4892  * 
4893  * Return value: byte index into the entry layout text
4894  **/
4895 gint
4896 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4897                                       gint      text_index)
4898 {
4899   PangoLayout *layout;
4900   const gchar *text;
4901   gint cursor_index;
4902   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4903
4904   layout = gtk_entry_ensure_layout (entry, TRUE);
4905   text = pango_layout_get_text (layout);
4906   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4907   
4908   if (text_index > cursor_index)
4909     text_index += entry->preedit_length;
4910
4911   return text_index;
4912 }
4913
4914 /**
4915  * gtk_entry_get_layout_offsets:
4916  * @entry: a #GtkEntry
4917  * @x: location to store X offset of layout, or %NULL
4918  * @y: location to store Y offset of layout, or %NULL
4919  *
4920  *
4921  * Obtains the position of the #PangoLayout used to render text
4922  * in the entry, in widget coordinates. Useful if you want to line
4923  * up the text in an entry with some other text, e.g. when using the
4924  * entry to implement editable cells in a sheet widget.
4925  *
4926  * Also useful to convert mouse events into coordinates inside the
4927  * #PangoLayout, e.g. to take some action if some part of the entry text
4928  * is clicked.
4929  * 
4930  * Note that as the user scrolls around in the entry the offsets will
4931  * change; you'll need to connect to the "notify::scroll-offset"
4932  * signal to track this. Remember when using the #PangoLayout
4933  * functions you need to convert to and from pixels using
4934  * PANGO_PIXELS() or #PANGO_SCALE.
4935  *
4936  * Keep in mind that the layout text may contain a preedit string, so
4937  * gtk_entry_layout_index_to_text_index() and
4938  * gtk_entry_text_index_to_layout_index() are needed to convert byte
4939  * indices in the layout to byte indices in the entry contents.
4940  **/
4941 void
4942 gtk_entry_get_layout_offsets (GtkEntry *entry,
4943                               gint     *x,
4944                               gint     *y)
4945 {
4946   gint text_area_x, text_area_y;
4947   
4948   g_return_if_fail (GTK_IS_ENTRY (entry));
4949
4950   /* this gets coords relative to text area */
4951   get_layout_position (entry, x, y);
4952
4953   /* convert to widget coords */
4954   get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
4955   
4956   if (x)
4957     *x += text_area_x;
4958
4959   if (y)
4960     *y += text_area_y;
4961 }
4962
4963
4964 /**
4965  * gtk_entry_set_alignment:
4966  * @entry: a #GtkEntry
4967  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
4968  *          Reversed for RTL layouts
4969  * 
4970  * Sets the alignment for the contents of the entry. This controls
4971  * the horizontal positioning of the contents when the displayed
4972  * text is shorter than the width of the entry.
4973  *
4974  * Since: 2.4
4975  **/
4976 void
4977 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
4978 {
4979   GtkEntryPrivate *priv;
4980   
4981   g_return_if_fail (GTK_IS_ENTRY (entry));
4982
4983   priv = GTK_ENTRY_GET_PRIVATE (entry);
4984
4985   if (xalign < 0.0)
4986     xalign = 0.0;
4987   else if (xalign > 1.0)
4988     xalign = 1.0;
4989
4990   if (xalign != priv->xalign)
4991     {
4992       priv->xalign = xalign;
4993
4994       gtk_entry_recompute (entry);
4995
4996       g_object_notify (G_OBJECT (entry), "xalign");
4997     }
4998 }
4999
5000 /**
5001  * gtk_entry_get_alignment:
5002  * @entry: a #GtkEntry
5003  * 
5004  * Gets the value set by gtk_entry_set_alignment().
5005  * 
5006  * Return value: the alignment
5007  *
5008  * Since: 2.4
5009  **/
5010 gfloat
5011 gtk_entry_get_alignment (GtkEntry *entry)
5012 {
5013   GtkEntryPrivate *priv;
5014   
5015   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
5016
5017   priv = GTK_ENTRY_GET_PRIVATE (entry);
5018
5019   return priv->xalign;
5020 }
5021
5022 /* Quick hack of a popup menu
5023  */
5024 static void
5025 activate_cb (GtkWidget *menuitem,
5026              GtkEntry  *entry)
5027 {
5028   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5029   g_signal_emit_by_name (entry, signal);
5030 }
5031
5032
5033 static gboolean
5034 gtk_entry_mnemonic_activate (GtkWidget *widget,
5035                              gboolean   group_cycling)
5036 {
5037   gtk_widget_grab_focus (widget);
5038   return TRUE;
5039 }
5040
5041 static void
5042 append_action_signal (GtkEntry     *entry,
5043                       GtkWidget    *menu,
5044                       const gchar  *stock_id,
5045                       const gchar  *signal,
5046                       gboolean      sensitive)
5047 {
5048   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5049
5050   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5051   g_signal_connect (menuitem, "activate",
5052                     G_CALLBACK (activate_cb), entry);
5053
5054   gtk_widget_set_sensitive (menuitem, sensitive);
5055   
5056   gtk_widget_show (menuitem);
5057   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5058 }
5059         
5060 static void
5061 popup_menu_detach (GtkWidget *attach_widget,
5062                    GtkMenu   *menu)
5063 {
5064   GTK_ENTRY (attach_widget)->popup_menu = NULL;
5065 }
5066
5067 static void
5068 popup_position_func (GtkMenu   *menu,
5069                      gint      *x,
5070                      gint      *y,
5071                      gboolean  *push_in,
5072                      gpointer   user_data)
5073 {
5074   GtkEntry *entry = GTK_ENTRY (user_data);
5075   GtkWidget *widget = GTK_WIDGET (entry);
5076   GdkScreen *screen;
5077   GtkRequisition menu_req;
5078   GdkRectangle monitor;
5079   GtkBorder inner_border;
5080   gint monitor_num, strong_x, height;
5081  
5082   g_return_if_fail (GTK_WIDGET_REALIZED (entry));
5083
5084   gdk_window_get_origin (entry->text_area, x, y);
5085
5086   screen = gtk_widget_get_screen (widget);
5087   monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
5088   if (monitor_num < 0)
5089     monitor_num = 0;
5090   gtk_menu_set_monitor (menu, monitor_num);
5091
5092   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
5093   gtk_widget_size_request (entry->popup_menu, &menu_req);
5094   gdk_drawable_get_size (entry->text_area, NULL, &height);
5095   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5096   _gtk_entry_effective_inner_border (entry, &inner_border);
5097
5098   *x += inner_border.left + strong_x - entry->scroll_offset;
5099   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5100     *x -= menu_req.width;
5101
5102   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
5103     *y += height;
5104   else if ((*y - menu_req.height) >= monitor.y)
5105     *y -= menu_req.height;
5106   else if (monitor.y + monitor.height - (*y + height) > *y)
5107     *y += height;
5108   else
5109     *y -= menu_req.height;
5110
5111   *push_in = FALSE;
5112 }
5113
5114 static void
5115 unichar_chosen_func (const char *text,
5116                      gpointer    data)
5117 {
5118   GtkEntry *entry = GTK_ENTRY (data);
5119
5120   if (entry->editable)
5121     gtk_entry_enter_text (entry, text);
5122 }
5123
5124 typedef struct
5125 {
5126   GtkEntry *entry;
5127   gint button;
5128   guint time;
5129 } PopupInfo;
5130
5131 static void
5132 popup_targets_received (GtkClipboard     *clipboard,
5133                         GtkSelectionData *data,
5134                         gpointer          user_data)
5135 {
5136   PopupInfo *info = user_data;
5137   GtkEntry *entry = info->entry;
5138   
5139   if (GTK_WIDGET_REALIZED (entry))
5140     {
5141       gboolean clipboard_contains_text;
5142       GtkWidget *menuitem;
5143       GtkWidget *submenu;
5144       gboolean show_input_method_menu;
5145       gboolean show_unicode_menu;
5146       
5147         clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5148       if (entry->popup_menu)
5149         gtk_widget_destroy (entry->popup_menu);
5150       
5151       entry->popup_menu = gtk_menu_new ();
5152       
5153       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
5154                                  GTK_WIDGET (entry),
5155                                  popup_menu_detach);
5156       
5157       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5158                             entry->editable && entry->current_pos != entry->selection_bound);
5159       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5160                             entry->current_pos != entry->selection_bound);
5161       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5162                             entry->editable && clipboard_contains_text);
5163       
5164       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5165       gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
5166       g_signal_connect_swapped (menuitem, "activate",
5167                                 G_CALLBACK (gtk_entry_delete_cb), entry);
5168       gtk_widget_show (menuitem);
5169       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5170
5171       menuitem = gtk_separator_menu_item_new ();
5172       gtk_widget_show (menuitem);
5173       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5174       
5175       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5176       g_signal_connect_swapped (menuitem, "activate",
5177                                 G_CALLBACK (gtk_entry_select_all), entry);
5178       gtk_widget_show (menuitem);
5179       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5180       
5181       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
5182                     "gtk-show-input-method-menu", &show_input_method_menu,
5183                     "gtk-show-unicode-menu", &show_unicode_menu,
5184                     NULL);
5185
5186       if (!entry->visible)
5187         show_input_method_menu = FALSE;
5188
5189       if (show_input_method_menu || show_unicode_menu)
5190         {
5191           menuitem = gtk_separator_menu_item_new ();
5192           gtk_widget_show (menuitem);
5193           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5194         }
5195       
5196       if (show_input_method_menu)
5197         {
5198           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
5199           gtk_widget_set_sensitive (menuitem, entry->editable);      
5200           gtk_widget_show (menuitem);
5201           submenu = gtk_menu_new ();
5202           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5203           
5204           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5205       
5206           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
5207                                                 GTK_MENU_SHELL (submenu));
5208         }
5209       
5210       if (show_unicode_menu)
5211         {
5212           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
5213           gtk_widget_set_sensitive (menuitem, entry->editable);      
5214           gtk_widget_show (menuitem);
5215           
5216           submenu = gtk_menu_new ();
5217           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5218           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
5219           
5220           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
5221                                                         unichar_chosen_func,
5222                                                         entry);
5223         }
5224       
5225       g_signal_emit (entry,
5226                      signals[POPULATE_POPUP],
5227                      0,
5228                      entry->popup_menu);
5229   
5230
5231       if (info->button)
5232         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5233                         NULL, NULL,
5234                         info->button, info->time);
5235       else
5236         {
5237           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5238                           popup_position_func, entry,
5239                           info->button, info->time);
5240           gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
5241         }
5242     }
5243
5244   g_object_unref (entry);
5245   g_free (info);
5246 }
5247                         
5248 static void
5249 gtk_entry_do_popup (GtkEntry       *entry,
5250                     GdkEventButton *event)
5251 {
5252   PopupInfo *info = g_new (PopupInfo, 1);
5253
5254   /* In order to know what entries we should make sensitive, we
5255    * ask for the current targets of the clipboard, and when
5256    * we get them, then we actually pop up the menu.
5257    */
5258   info->entry = g_object_ref (entry);
5259   
5260   if (event)
5261     {
5262       info->button = event->button;
5263       info->time = event->time;
5264     }
5265   else
5266     {
5267       info->button = 0;
5268       info->time = gtk_get_current_event_time ();
5269     }
5270
5271   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
5272                                   gdk_atom_intern_static_string ("TARGETS"),
5273                                   popup_targets_received,
5274                                   info);
5275 }
5276
5277 static gboolean
5278 gtk_entry_popup_menu (GtkWidget *widget)
5279 {
5280   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5281   return TRUE;
5282 }
5283
5284 static void
5285 gtk_entry_drag_leave (GtkWidget        *widget,
5286                       GdkDragContext   *context,
5287                       guint             time)
5288 {
5289   GtkEntry *entry = GTK_ENTRY (widget);
5290
5291   entry->dnd_position = -1;
5292   gtk_widget_queue_draw (widget);
5293 }
5294
5295 static gboolean
5296 gtk_entry_drag_drop  (GtkWidget        *widget,
5297                       GdkDragContext   *context,
5298                       gint              x,
5299                       gint              y,
5300                       guint             time)
5301 {
5302   GtkEntry *entry = GTK_ENTRY (widget);
5303   GdkAtom target = GDK_NONE;
5304   
5305   if (entry->editable)
5306     target = gtk_drag_dest_find_target (widget, context, NULL);
5307
5308   if (target != GDK_NONE)
5309     gtk_drag_get_data (widget, context, target, time);
5310   else
5311     gtk_drag_finish (context, FALSE, FALSE, time);
5312   
5313   return TRUE;
5314 }
5315
5316 static gboolean
5317 gtk_entry_drag_motion (GtkWidget        *widget,
5318                        GdkDragContext   *context,
5319                        gint              x,
5320                        gint              y,
5321                        guint             time)
5322 {
5323   GtkEntry *entry = GTK_ENTRY (widget);
5324   GtkWidget *source_widget;
5325   GdkDragAction suggested_action;
5326   gint new_position, old_position;
5327   gint sel1, sel2;
5328   
5329   x -= widget->style->xthickness;
5330   y -= widget->style->ythickness;
5331   
5332   old_position = entry->dnd_position;
5333   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5334
5335   if (entry->editable &&
5336       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5337     {
5338       source_widget = gtk_drag_get_source_widget (context);
5339       suggested_action = context->suggested_action;
5340
5341       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5342           new_position < sel1 || new_position > sel2)
5343         {
5344           if (source_widget == widget)
5345             {
5346               /* Default to MOVE, unless the user has
5347                * pressed ctrl or alt to affect available actions
5348                */
5349               if ((context->actions & GDK_ACTION_MOVE) != 0)
5350                 suggested_action = GDK_ACTION_MOVE;
5351             }
5352               
5353           entry->dnd_position = new_position;
5354         }
5355       else
5356         {
5357           if (source_widget == widget)
5358             suggested_action = 0;       /* Can't drop in selection where drag started */
5359           
5360           entry->dnd_position = -1;
5361         }
5362     }
5363   else
5364     {
5365       /* Entry not editable, or no text */
5366       suggested_action = 0;
5367       entry->dnd_position = -1;
5368     }
5369   
5370   gdk_drag_status (context, suggested_action, time);
5371   
5372   if (entry->dnd_position != old_position)
5373     gtk_widget_queue_draw (widget);
5374
5375   return TRUE;
5376 }
5377
5378 static void
5379 gtk_entry_drag_data_received (GtkWidget        *widget,
5380                               GdkDragContext   *context,
5381                               gint              x,
5382                               gint              y,
5383                               GtkSelectionData *selection_data,
5384                               guint             info,
5385                               guint             time)
5386 {
5387   GtkEntry *entry = GTK_ENTRY (widget);
5388   GtkEditable *editable = GTK_EDITABLE (widget);
5389   gchar *str;
5390
5391   str = (gchar *) gtk_selection_data_get_text (selection_data);
5392
5393   x -= widget->style->xthickness;
5394   y -= widget->style->ythickness;
5395
5396   if (str && entry->editable)
5397     {
5398       gint new_position;
5399       gint sel1, sel2;
5400       gint length = -1;
5401
5402       if (entry->truncate_multiline)
5403         length = truncate_multiline (str);
5404
5405       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5406
5407       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5408           new_position < sel1 || new_position > sel2)
5409         {
5410           gtk_editable_insert_text (editable, str, length, &new_position);
5411         }
5412       else
5413         {
5414           /* Replacing selection */
5415           begin_change (entry);
5416           g_object_freeze_notify (G_OBJECT (entry));
5417           gtk_editable_delete_text (editable, sel1, sel2);
5418           gtk_editable_insert_text (editable, str, length, &sel1);
5419           g_object_thaw_notify (G_OBJECT (entry));
5420           end_change (entry);
5421         }
5422       
5423       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5424     }
5425   else
5426     {
5427       /* Drag and drop didn't happen! */
5428       gtk_drag_finish (context, FALSE, FALSE, time);
5429     }
5430
5431   g_free (str);
5432 }
5433
5434 static void
5435 gtk_entry_drag_data_get (GtkWidget        *widget,
5436                          GdkDragContext   *context,
5437                          GtkSelectionData *selection_data,
5438                          guint             info,
5439                          guint             time)
5440 {
5441   gint sel_start, sel_end;
5442
5443   GtkEditable *editable = GTK_EDITABLE (widget);
5444   
5445   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5446     {
5447       gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5448
5449       gtk_selection_data_set_text (selection_data, str, -1);
5450       
5451       g_free (str);
5452     }
5453
5454 }
5455
5456 static void
5457 gtk_entry_drag_data_delete (GtkWidget      *widget,
5458                             GdkDragContext *context)
5459 {
5460   gint sel_start, sel_end;
5461
5462   GtkEditable *editable = GTK_EDITABLE (widget);
5463   
5464   if (GTK_ENTRY (widget)->editable &&
5465       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5466     gtk_editable_delete_text (editable, sel_start, sel_end);
5467 }
5468
5469 /* We display the cursor when
5470  *
5471  *  - the selection is empty, AND
5472  *  - the widget has focus
5473  */
5474
5475 #define CURSOR_ON_MULTIPLIER 2
5476 #define CURSOR_OFF_MULTIPLIER 1
5477 #define CURSOR_PEND_MULTIPLIER 3
5478 #define CURSOR_DIVIDER 3
5479
5480 static gboolean
5481 cursor_blinks (GtkEntry *entry)
5482 {
5483   if (GTK_WIDGET_HAS_FOCUS (entry) &&
5484       entry->editable &&
5485       entry->selection_bound == entry->current_pos)
5486     {
5487       GtkSettings *settings;
5488       gboolean blink;
5489
5490       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5491       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5492
5493       return blink;
5494     }
5495   else
5496     return FALSE;
5497 }
5498
5499 static gint
5500 get_cursor_time (GtkEntry *entry)
5501 {
5502   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5503   gint time;
5504
5505   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5506
5507   return time;
5508 }
5509
5510 static gint
5511 get_cursor_blink_timeout (GtkEntry *entry)
5512 {
5513   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5514   gint timeout;
5515
5516   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
5517
5518   return timeout;
5519 }
5520
5521 static void
5522 show_cursor (GtkEntry *entry)
5523 {
5524   if (!entry->cursor_visible)
5525     {
5526       entry->cursor_visible = TRUE;
5527
5528       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5529         gtk_widget_queue_draw (GTK_WIDGET (entry));
5530     }
5531 }
5532
5533 static void
5534 hide_cursor (GtkEntry *entry)
5535 {
5536   if (entry->cursor_visible)
5537     {
5538       entry->cursor_visible = FALSE;
5539
5540       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5541         gtk_widget_queue_draw (GTK_WIDGET (entry));
5542     }
5543 }
5544
5545 /*
5546  * Blink!
5547  */
5548 static gint
5549 blink_cb (gpointer data)
5550 {
5551   GtkEntry *entry;
5552   GtkEntryPrivate *priv; 
5553   gint blink_timeout;
5554
5555   entry = GTK_ENTRY (data);
5556   priv = GTK_ENTRY_GET_PRIVATE (entry);
5557  
5558   if (!GTK_WIDGET_HAS_FOCUS (entry))
5559     {
5560       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5561                  "connect a handler to this signal, it must return\n"
5562                  "FALSE so the entry gets the event as well");
5563
5564       gtk_entry_check_cursor_blink (entry);
5565
5566       return FALSE;
5567     }
5568   
5569   g_assert (entry->selection_bound == entry->current_pos);
5570   
5571   blink_timeout = get_cursor_blink_timeout (entry);
5572   if (priv->blink_time > 1000 * blink_timeout && 
5573       blink_timeout < G_MAXINT/1000) 
5574     {
5575       /* we've blinked enough without the user doing anything, stop blinking */
5576       show_cursor (entry);
5577       entry->blink_timeout = 0;
5578     } 
5579   else if (entry->cursor_visible)
5580     {
5581       hide_cursor (entry);
5582       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5583                                             blink_cb,
5584                                             entry);
5585     }
5586   else
5587     {
5588       show_cursor (entry);
5589       priv->blink_time += get_cursor_time (entry);
5590       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5591                                             blink_cb,
5592                                             entry);
5593     }
5594
5595   /* Remove ourselves */
5596   return FALSE;
5597 }
5598
5599 static void
5600 gtk_entry_check_cursor_blink (GtkEntry *entry)
5601 {
5602   GtkEntryPrivate *priv; 
5603   
5604   priv = GTK_ENTRY_GET_PRIVATE (entry);
5605
5606   if (cursor_blinks (entry))
5607     {
5608       if (!entry->blink_timeout)
5609         {
5610           show_cursor (entry);
5611           entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5612                                                 blink_cb,
5613                                                 entry);
5614         }
5615     }
5616   else
5617     {
5618       if (entry->blink_timeout)  
5619         { 
5620           g_source_remove (entry->blink_timeout);
5621           entry->blink_timeout = 0;
5622         }
5623       
5624       entry->cursor_visible = TRUE;
5625     }
5626   
5627 }
5628
5629 static void
5630 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5631 {
5632   if (cursor_blinks (entry))
5633     {
5634       if (entry->blink_timeout != 0)
5635         g_source_remove (entry->blink_timeout);
5636       
5637       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5638                                             blink_cb,
5639                                             entry);
5640       show_cursor (entry);
5641     }
5642 }
5643
5644 static void
5645 gtk_entry_reset_blink_time (GtkEntry *entry)
5646 {
5647   GtkEntryPrivate *priv; 
5648   
5649   priv = GTK_ENTRY_GET_PRIVATE (entry);
5650   
5651   priv->blink_time = 0;
5652 }
5653
5654
5655 /* completion */
5656 static gint
5657 gtk_entry_completion_timeout (gpointer data)
5658 {
5659   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5660
5661   completion->priv->completion_timeout = 0;
5662
5663   if (completion->priv->filter_model &&
5664       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5665       >= completion->priv->minimum_key_length)
5666     {
5667       gint matches;
5668       gint actions;
5669       GtkTreeSelection *s;
5670       gboolean popup_single;
5671
5672       gtk_entry_completion_complete (completion);
5673       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5674
5675       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5676
5677       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5678
5679       gtk_tree_selection_unselect_all (s);
5680
5681       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5682
5683       g_object_get (completion, "popup-single-match", &popup_single, NULL);
5684       if ((matches > (popup_single ? 0: 1)) || actions > 0)
5685         { 
5686           if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5687             _gtk_entry_completion_resize_popup (completion);
5688           else
5689             _gtk_entry_completion_popup (completion);
5690         }
5691       else 
5692         _gtk_entry_completion_popdown (completion);
5693     }
5694   else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5695     _gtk_entry_completion_popdown (completion);
5696
5697   return FALSE;
5698 }
5699
5700 static inline gboolean
5701 keyval_is_cursor_move (guint keyval)
5702 {
5703   if (keyval == GDK_Up || keyval == GDK_KP_Up)
5704     return TRUE;
5705
5706   if (keyval == GDK_Down || keyval == GDK_KP_Down)
5707     return TRUE;
5708
5709   if (keyval == GDK_Page_Up)
5710     return TRUE;
5711
5712   if (keyval == GDK_Page_Down)
5713     return TRUE;
5714
5715   return FALSE;
5716 }
5717
5718 static gboolean
5719 gtk_entry_completion_key_press (GtkWidget   *widget,
5720                                 GdkEventKey *event,
5721                                 gpointer     user_data)
5722 {
5723   gint matches, actions = 0;
5724   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5725
5726   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5727     return FALSE;
5728
5729   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5730
5731   if (completion->priv->actions)
5732     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5733
5734   if (keyval_is_cursor_move (event->keyval))
5735     {
5736       GtkTreePath *path = NULL;
5737       
5738       if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5739         {
5740           if (completion->priv->current_selected < 0)
5741             completion->priv->current_selected = matches + actions - 1;
5742           else
5743             completion->priv->current_selected--;
5744         }
5745       else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5746         {
5747           if (completion->priv->current_selected < matches + actions - 1)
5748             completion->priv->current_selected++;
5749           else
5750             completion->priv->current_selected = -1;
5751         }
5752       else if (event->keyval == GDK_Page_Up)
5753         {
5754           if (completion->priv->current_selected < 0)
5755             completion->priv->current_selected = matches + actions - 1;
5756           else if (completion->priv->current_selected == 0)
5757             completion->priv->current_selected = -1;
5758           else if (completion->priv->current_selected < matches) 
5759             {
5760               completion->priv->current_selected -= 14;
5761               if (completion->priv->current_selected < 0)
5762                 completion->priv->current_selected = 0;
5763             }
5764           else 
5765             {
5766               completion->priv->current_selected -= 14;
5767               if (completion->priv->current_selected < matches - 1)
5768                 completion->priv->current_selected = matches - 1;
5769             }
5770         }
5771       else if (event->keyval == GDK_Page_Down)
5772         {
5773           if (completion->priv->current_selected < 0)
5774             completion->priv->current_selected = 0;
5775           else if (completion->priv->current_selected < matches - 1)
5776             {
5777               completion->priv->current_selected += 14;
5778               if (completion->priv->current_selected > matches - 1)
5779                 completion->priv->current_selected = matches - 1;
5780             }
5781           else if (completion->priv->current_selected == matches + actions - 1)
5782             {
5783               completion->priv->current_selected = -1;
5784             }
5785           else
5786             {
5787               completion->priv->current_selected += 14;
5788               if (completion->priv->current_selected > matches + actions - 1)
5789                 completion->priv->current_selected = matches + actions - 1;
5790             }
5791         }
5792
5793       if (completion->priv->current_selected < 0)
5794         {
5795           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5796           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5797         }
5798       else if (completion->priv->current_selected < matches)
5799         {
5800           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5801
5802           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5803           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5804                                     path, NULL, FALSE);
5805
5806           if (completion->priv->inline_selection)
5807             {
5808
5809               GtkTreeIter iter;
5810               GtkTreeModel *model = NULL;
5811               GtkTreeSelection *sel;
5812               gboolean entry_set;
5813
5814               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5815               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5816                 return FALSE;
5817               
5818               if (completion->priv->completion_prefix == NULL)
5819                 {
5820                   completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
5821                 }
5822               
5823               g_signal_emit_by_name (completion, "cursor_on_match", model,
5824                                      &iter, &entry_set);
5825             }
5826         }
5827       else if (completion->priv->current_selected - matches >= 0)
5828         {
5829           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5830
5831           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5832           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5833                                     path, NULL, FALSE);
5834         }
5835
5836       gtk_tree_path_free (path);
5837
5838       return TRUE;
5839     }
5840   else if (event->keyval == GDK_Escape ||
5841            event->keyval == GDK_Left ||
5842            event->keyval == GDK_KP_Left ||
5843            event->keyval == GDK_Right ||
5844            event->keyval == GDK_KP_Right) 
5845     {
5846       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5847       _gtk_entry_completion_popdown (completion);
5848
5849       if (completion->priv->inline_selection)
5850         {
5851           /* Escape rejects the tentative completion */
5852           if (event->keyval == GDK_Escape)
5853             {
5854               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), completion->priv->completion_prefix);
5855             }
5856
5857           /* Move the cursor to the end for Right/Esc, to the
5858              beginning for Left */
5859           if (event->keyval == GDK_Right ||
5860               event->keyval == GDK_KP_Right ||
5861               event->keyval == GDK_Escape)
5862             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5863           else
5864             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
5865
5866           g_free (completion->priv->completion_prefix);
5867           completion->priv->completion_prefix = NULL;
5868         }
5869
5870       return TRUE;
5871     }
5872   else if (event->keyval == GDK_Tab || 
5873            event->keyval == GDK_KP_Tab ||
5874            event->keyval == GDK_ISO_Left_Tab) 
5875     {
5876       GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ? 
5877         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5878
5879       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5880       _gtk_entry_completion_popdown (completion);
5881
5882       if (completion->priv->completion_prefix)
5883         {
5884            g_free (completion->priv->completion_prefix);
5885            completion->priv->completion_prefix = NULL;
5886         }
5887
5888       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
5889
5890       return TRUE;
5891     }
5892   else if (event->keyval == GDK_ISO_Enter ||
5893            event->keyval == GDK_KP_Enter ||
5894            event->keyval == GDK_Return)
5895     {
5896       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5897       _gtk_entry_completion_popdown (completion);
5898
5899       if (completion->priv->current_selected < matches)
5900         {
5901           GtkTreeIter iter;
5902           GtkTreeModel *model = NULL;
5903           GtkTreeSelection *sel;
5904           gboolean entry_set;
5905
5906           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5907           if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5908             return FALSE;
5909
5910           g_signal_handler_block (widget, completion->priv->changed_id);
5911           g_signal_emit_by_name (completion, "match_selected",
5912                                  model, &iter, &entry_set);
5913           g_signal_handler_unblock (widget, completion->priv->changed_id);
5914
5915           if (!entry_set)
5916             {
5917               gchar *str = NULL;
5918
5919               gtk_tree_model_get (model, &iter,
5920                                   completion->priv->text_column, &str,
5921                                   -1);
5922
5923               gtk_entry_set_text (GTK_ENTRY (widget), str);
5924
5925               /* move the cursor to the end */
5926               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5927
5928               g_free (str);
5929             }
5930
5931           return TRUE;
5932         }
5933       else if (completion->priv->current_selected - matches >= 0)
5934         {
5935           GtkTreePath *path;
5936
5937           _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5938
5939           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5940
5941           g_signal_emit_by_name (completion, "action_activated",
5942                                  gtk_tree_path_get_indices (path)[0]);
5943           gtk_tree_path_free (path);
5944
5945           return TRUE;
5946         }
5947     }
5948
5949   return FALSE;
5950 }
5951
5952 static void
5953 gtk_entry_completion_changed (GtkWidget *entry,
5954                               gpointer   user_data)
5955 {
5956   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5957
5958   /* (re)install completion timeout */
5959   if (completion->priv->completion_timeout)
5960     g_source_remove (completion->priv->completion_timeout);
5961
5962   if (!gtk_entry_get_text (GTK_ENTRY (entry)))
5963     return;
5964
5965   /* no need to normalize for this test */
5966   if (completion->priv->minimum_key_length > 0 &&
5967       strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
5968     {
5969       if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5970         _gtk_entry_completion_popdown (completion);
5971       return;
5972     }
5973
5974   completion->priv->completion_timeout =
5975     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
5976                    gtk_entry_completion_timeout,
5977                    completion);
5978 }
5979
5980 static gboolean
5981 check_completion_callback (GtkEntryCompletion *completion)
5982 {
5983   completion->priv->check_completion_idle = NULL;
5984   
5985   gtk_entry_completion_complete (completion);
5986   gtk_entry_completion_insert_prefix (completion);
5987
5988   return FALSE;
5989 }
5990
5991 static void
5992 clear_completion_callback (GtkEntry   *entry,
5993                            GParamSpec *pspec)
5994 {
5995   if (pspec->name == I_("cursor-position") ||
5996       pspec->name == I_("selection-bound"))
5997     {
5998       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5999       
6000       completion->priv->has_completion = FALSE;
6001     }
6002 }
6003
6004 static gboolean
6005 accept_completion_callback (GtkEntry *entry)
6006 {
6007   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
6008
6009   if (completion->priv->has_completion)
6010     gtk_editable_set_position (GTK_EDITABLE (entry),
6011                                entry->text_length);
6012
6013   return FALSE;
6014 }
6015
6016 static void
6017 completion_insert_text_callback (GtkEntry           *entry,
6018                                  const gchar        *text,
6019                                  gint                length,
6020                                  gint                position,
6021                                  GtkEntryCompletion *completion)
6022 {
6023   /* idle to update the selection based on the file list */
6024   if (completion->priv->check_completion_idle == NULL)
6025     {
6026       completion->priv->check_completion_idle = g_idle_source_new ();
6027       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
6028       g_source_set_closure (completion->priv->check_completion_idle,
6029                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
6030                                                    G_OBJECT (completion)));
6031       g_source_attach (completion->priv->check_completion_idle, NULL);
6032     }
6033 }
6034
6035 static void
6036 completion_changed (GtkEntryCompletion *completion,
6037                     GParamSpec         *pspec,
6038                     gpointer            data)
6039 {
6040   GtkEntry *entry = GTK_ENTRY (data);
6041
6042   if (pspec->name == I_("popup-completion") ||
6043       pspec->name == I_("inline-completion"))
6044     {
6045       disconnect_completion_signals (entry, completion);
6046       connect_completion_signals (entry, completion);
6047     }
6048 }
6049
6050 static void
6051 disconnect_completion_signals (GtkEntry           *entry,
6052                                GtkEntryCompletion *completion)
6053 {
6054   g_signal_handlers_disconnect_by_func (completion, 
6055                                        G_CALLBACK (completion_changed), entry);
6056   if (completion->priv->changed_id > 0 &&
6057       g_signal_handler_is_connected (entry, completion->priv->changed_id))
6058     {
6059       g_signal_handler_disconnect (entry, completion->priv->changed_id);
6060       completion->priv->changed_id = 0;
6061     }
6062   g_signal_handlers_disconnect_by_func (entry, 
6063                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
6064   if (completion->priv->insert_text_id > 0 &&
6065       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
6066     {
6067       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
6068       completion->priv->insert_text_id = 0;
6069     }
6070   g_signal_handlers_disconnect_by_func (entry, 
6071                                         G_CALLBACK (completion_insert_text_callback), completion);
6072   g_signal_handlers_disconnect_by_func (entry, 
6073                                         G_CALLBACK (clear_completion_callback), completion);
6074   g_signal_handlers_disconnect_by_func (entry, 
6075                                         G_CALLBACK (accept_completion_callback), completion);
6076 }
6077
6078 static void
6079 connect_completion_signals (GtkEntry           *entry,
6080                             GtkEntryCompletion *completion)
6081 {
6082   if (completion->priv->popup_completion)
6083     {
6084       completion->priv->changed_id =
6085         g_signal_connect (entry, "changed",
6086                           G_CALLBACK (gtk_entry_completion_changed), completion);
6087       g_signal_connect (entry, "key_press_event",
6088                         G_CALLBACK (gtk_entry_completion_key_press), completion);
6089     }
6090  
6091   if (completion->priv->inline_completion)
6092     {
6093       completion->priv->insert_text_id =
6094         g_signal_connect (entry, "insert_text",
6095                           G_CALLBACK (completion_insert_text_callback), completion);
6096       g_signal_connect (entry, "notify",
6097                         G_CALLBACK (clear_completion_callback), completion);
6098       g_signal_connect (entry, "activate",
6099                         G_CALLBACK (accept_completion_callback), completion);
6100       g_signal_connect (entry, "focus_out_event",
6101                         G_CALLBACK (accept_completion_callback), completion);
6102     }
6103
6104   g_signal_connect (completion, "notify",
6105                     G_CALLBACK (completion_changed), entry);
6106 }
6107
6108 /**
6109  * gtk_entry_set_completion:
6110  * @entry: A #GtkEntry
6111  * @completion: The #GtkEntryCompletion or %NULL
6112  *
6113  * Sets @completion to be the auxiliary completion object to use with @entry.
6114  * All further configuration of the completion mechanism is done on
6115  * @completion using the #GtkEntryCompletion API. Completion is disabled if
6116  * @completion is set to %NULL.
6117  *
6118  * Since: 2.4
6119  */
6120 void
6121 gtk_entry_set_completion (GtkEntry           *entry,
6122                           GtkEntryCompletion *completion)
6123 {
6124   GtkEntryCompletion *old;
6125
6126   g_return_if_fail (GTK_IS_ENTRY (entry));
6127   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
6128
6129   old = gtk_entry_get_completion (entry);
6130
6131   if (old == completion)
6132     return;
6133   
6134   if (old)
6135     {
6136       if (old->priv->completion_timeout)
6137         {
6138           g_source_remove (old->priv->completion_timeout);
6139           old->priv->completion_timeout = 0;
6140         }
6141
6142       if (GTK_WIDGET_MAPPED (old->priv->popup_window))
6143         _gtk_entry_completion_popdown (old);
6144
6145       disconnect_completion_signals (entry, old);
6146       old->priv->entry = NULL;
6147
6148       g_object_unref (old);
6149     }
6150
6151   if (!completion)
6152     {
6153       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
6154       return;
6155     }
6156
6157   /* hook into the entry */
6158   g_object_ref (completion);
6159
6160   connect_completion_signals (entry, completion);    
6161   completion->priv->entry = GTK_WIDGET (entry);
6162   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
6163 }
6164
6165 /**
6166  * gtk_entry_get_completion:
6167  * @entry: A #GtkEntry
6168  *
6169  * Returns the auxiliary completion object currently in use by @entry.
6170  *
6171  * Return value: The auxiliary completion object currently in use by @entry.
6172  *
6173  * Since: 2.4
6174  */
6175 GtkEntryCompletion *
6176 gtk_entry_get_completion (GtkEntry *entry)
6177 {
6178   GtkEntryCompletion *completion;
6179
6180   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6181
6182   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
6183                                      GTK_ENTRY_COMPLETION_KEY));
6184
6185   return completion;
6186 }
6187
6188 /**
6189  * gtk_entry_set_cursor_hadjustment:
6190  * @entry: a #GtkEntry
6191  * @adjustment: an adjustment which should be adjusted when the cursor 
6192  *              is moved, or %NULL
6193  *
6194  * Hooks up an adjustment to the cursor position in an entry, so that when 
6195  * the cursor is moved, the adjustment is scrolled to show that position. 
6196  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
6197  * the adjustment.
6198  *
6199  * The adjustment has to be in pixel units and in the same coordinate system 
6200  * as the entry. 
6201  * 
6202  * Since: 2.12
6203  */
6204 void
6205 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
6206                                   GtkAdjustment *adjustment)
6207 {
6208   GtkEntryPrivate *priv;
6209
6210   g_return_if_fail (GTK_IS_ENTRY (entry));
6211   if (adjustment)
6212     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
6213
6214   priv = GTK_ENTRY_GET_PRIVATE (entry);
6215   if (priv->cursor_hadjustment)
6216     g_object_unref (priv->cursor_hadjustment);
6217   if (adjustment)
6218     g_object_ref (adjustment);
6219   priv->cursor_hadjustment = adjustment;
6220 }
6221
6222 /**
6223  * gtk_entry_get_cursor_hadjustment:
6224  * @entry: a #GtkEntry
6225  *
6226  * Retrieves the horizontal cursor adjustment for the entry. 
6227  * See gtk_entry_set_cursor_hadjustment().
6228  *
6229  * Return value: the horizontal cursor adjustment, or %NULL 
6230  *   if none has been set.
6231  * 
6232  * Since: 2.12
6233  */
6234 GtkAdjustment*
6235 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
6236 {
6237   GtkEntryPrivate *priv;
6238     
6239   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6240
6241   priv = GTK_ENTRY_GET_PRIVATE (entry);
6242
6243   return priv->cursor_hadjustment;
6244 }
6245
6246 #define __GTK_ENTRY_C__
6247 #include "gtkaliasdef.c"