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