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