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