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