]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
d9bf421f3d5941fe3dd2c864e0cf3e40640f33a4
[~andy/gtk] / gtk / gtkentry.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <string.h>
28
29 #include <pango/pango.h>
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
33 #include "gtkcelleditable.h"
34 #include "gtkclipboard.h"
35 #include "gtkdnd.h"
36 #include "gtkentry.h"
37 #include "gtkimagemenuitem.h"
38 #include "gtkimmulticontext.h"
39 #include "gtkintl.h"
40 #include "gtkmain.h"
41 #include "gtkmarshalers.h"
42 #include "gtkmenu.h"
43 #include "gtkmenuitem.h"
44 #include "gtkseparatormenuitem.h"
45 #include "gtkselection.h"
46 #include "gtksettings.h"
47 #include "gtkstock.h"
48 #include "gtksignal.h"
49 #include "gtktextutil.h"
50 #include "gtkwindow.h"
51
52 #define MIN_ENTRY_WIDTH  150
53 #define DRAW_TIMEOUT     20
54 #define INNER_BORDER     2
55
56 /* Initial size of buffer, in bytes */
57 #define MIN_SIZE 16
58
59 /* Maximum size of text buffer, in bytes */
60 #define MAX_SIZE G_MAXUSHORT
61
62 enum {
63   ACTIVATE,
64   POPULATE_POPUP,
65   MOVE_CURSOR,
66   INSERT_AT_CURSOR,
67   DELETE_FROM_CURSOR,
68   CUT_CLIPBOARD,
69   COPY_CLIPBOARD,
70   PASTE_CLIPBOARD,
71   TOGGLE_OVERWRITE,
72   LAST_SIGNAL
73 };
74
75 enum {
76   PROP_0,
77   PROP_CURSOR_POSITION,
78   PROP_SELECTION_BOUND,
79   PROP_EDITABLE,
80   PROP_MAX_LENGTH,
81   PROP_VISIBILITY,
82   PROP_HAS_FRAME,
83   PROP_INVISIBLE_CHAR,
84   PROP_ACTIVATES_DEFAULT,
85   PROP_WIDTH_CHARS,
86   PROP_SCROLL_OFFSET,
87   PROP_TEXT
88 };
89
90 static guint signals[LAST_SIGNAL] = { 0 };
91
92 typedef enum {
93   CURSOR_STANDARD,
94   CURSOR_DND
95 } CursorType;
96
97 static GtkTargetEntry target_table[] = {
98   { "UTF8_STRING",   0, 0 },
99   { "COMPOUND_TEXT", 0, 0 },
100   { "TEXT",          0, 0 },
101   { "text/plain",    0, 0 },
102   { "STRING",        0, 0 }
103 };
104
105 /* GObject, GtkObject methods
106  */
107 static void   gtk_entry_class_init           (GtkEntryClass        *klass);
108 static void   gtk_entry_editable_init        (GtkEditableClass     *iface);
109 static void   gtk_entry_cell_editable_init   (GtkCellEditableIface *iface);
110 static void   gtk_entry_init                 (GtkEntry         *entry);
111 static void   gtk_entry_set_property (GObject         *object,
112                                       guint            prop_id,
113                                       const GValue    *value,
114                                       GParamSpec      *pspec);
115 static void   gtk_entry_get_property (GObject         *object,
116                                       guint            prop_id,
117                                       GValue          *value,
118                                       GParamSpec      *pspec);
119 static void   gtk_entry_finalize             (GObject          *object);
120
121 /* GtkWidget methods
122  */
123 static void   gtk_entry_realize              (GtkWidget        *widget);
124 static void   gtk_entry_unrealize            (GtkWidget        *widget);
125 static void   gtk_entry_size_request         (GtkWidget        *widget,
126                                               GtkRequisition   *requisition);
127 static void   gtk_entry_size_allocate        (GtkWidget        *widget,
128                                               GtkAllocation    *allocation);
129 static void   gtk_entry_draw_frame           (GtkWidget        *widget);
130 static gint   gtk_entry_expose               (GtkWidget        *widget,
131                                               GdkEventExpose   *event);
132 static gint   gtk_entry_button_press         (GtkWidget        *widget,
133                                               GdkEventButton   *event);
134 static gint   gtk_entry_button_release       (GtkWidget        *widget,
135                                               GdkEventButton   *event);
136 static gint   gtk_entry_motion_notify        (GtkWidget        *widget,
137                                               GdkEventMotion   *event);
138 static gint   gtk_entry_key_press            (GtkWidget        *widget,
139                                               GdkEventKey      *event);
140 static gint   gtk_entry_key_release          (GtkWidget        *widget,
141                                               GdkEventKey      *event);
142 static gint   gtk_entry_focus_in             (GtkWidget        *widget,
143                                               GdkEventFocus    *event);
144 static gint   gtk_entry_focus_out            (GtkWidget        *widget,
145                                               GdkEventFocus    *event);
146 static void   gtk_entry_grab_focus           (GtkWidget        *widget);
147 static void   gtk_entry_style_set            (GtkWidget        *widget,
148                                               GtkStyle         *previous_style);
149 static void   gtk_entry_direction_changed    (GtkWidget        *widget,
150                                               GtkTextDirection  previous_dir);
151 static void   gtk_entry_state_changed        (GtkWidget        *widget,
152                                               GtkStateType      previous_state);
153
154 static gboolean gtk_entry_drag_drop          (GtkWidget        *widget,
155                                               GdkDragContext   *context,
156                                               gint              x,
157                                               gint              y,
158                                               guint             time);
159 static gboolean gtk_entry_drag_motion        (GtkWidget        *widget,
160                                               GdkDragContext   *context,
161                                               gint              x,
162                                               gint              y,
163                                               guint             time);
164 static void     gtk_entry_drag_leave         (GtkWidget        *widget,
165                                               GdkDragContext   *context,
166                                               guint             time);
167 static void     gtk_entry_drag_data_received (GtkWidget        *widget,
168                                               GdkDragContext   *context,
169                                               gint              x,
170                                               gint              y,
171                                               GtkSelectionData *selection_data,
172                                               guint             info,
173                                               guint             time);
174 static void     gtk_entry_drag_data_get      (GtkWidget        *widget,
175                                               GdkDragContext   *context,
176                                               GtkSelectionData *selection_data,
177                                               guint             info,
178                                               guint             time);
179 static void     gtk_entry_drag_data_delete   (GtkWidget        *widget,
180                                               GdkDragContext   *context);
181
182 /* GtkEditable method implementations
183  */
184 static void     gtk_entry_insert_text          (GtkEditable *editable,
185                                                 const gchar *new_text,
186                                                 gint         new_text_length,
187                                                 gint        *position);
188 static void     gtk_entry_delete_text          (GtkEditable *editable,
189                                                 gint         start_pos,
190                                                 gint         end_pos);
191 static gchar *  gtk_entry_get_chars            (GtkEditable *editable,
192                                                 gint         start_pos,
193                                                 gint         end_pos);
194 static void     gtk_entry_real_set_position    (GtkEditable *editable,
195                                                 gint         position);
196 static gint     gtk_entry_get_position         (GtkEditable *editable);
197 static void     gtk_entry_set_selection_bounds (GtkEditable *editable,
198                                                 gint         start,
199                                                 gint         end);
200 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
201                                                 gint        *start,
202                                                 gint        *end);
203
204 /* GtkCellEditable method implementations
205  */
206 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
207                                      GdkEvent        *event);
208
209 /* Default signal handlers
210  */
211 static void gtk_entry_real_insert_text   (GtkEditable     *editable,
212                                           const gchar     *new_text,
213                                           gint             new_text_length,
214                                           gint            *position);
215 static void gtk_entry_real_delete_text   (GtkEditable     *editable,
216                                           gint             start_pos,
217                                           gint             end_pos);
218 static void gtk_entry_move_cursor        (GtkEntry        *entry,
219                                           GtkMovementStep  step,
220                                           gint             count,
221                                           gboolean         extend_selection);
222 static void gtk_entry_insert_at_cursor   (GtkEntry        *entry,
223                                           const gchar     *str);
224 static void gtk_entry_delete_from_cursor (GtkEntry        *entry,
225                                           GtkDeleteType    type,
226                                           gint             count);
227 static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
228 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
229 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
230 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
231 static void gtk_entry_select_all         (GtkEntry        *entry);
232 static void gtk_entry_real_activate      (GtkEntry        *entry);
233 static void gtk_entry_popup_menu         (GtkWidget      *widget);
234
235 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
236                                                 GtkEntry  *entry);
237 /* IM Context Callbacks
238  */
239 static void     gtk_entry_commit_cb               (GtkIMContext *context,
240                                                    const gchar  *str,
241                                                    GtkEntry     *entry);
242 static void     gtk_entry_preedit_changed_cb      (GtkIMContext *context,
243                                                    GtkEntry     *entry);
244 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
245                                                    GtkEntry     *entry);
246 static gboolean gtk_entry_delete_surrounding_cb   (GtkIMContext *context,
247                                                    gint          offset,
248                                                    gint          n_chars,
249                                                    GtkEntry     *entry);
250
251 /* Internal routines
252  */
253 static void         gtk_entry_enter_text               (GtkEntry       *entry,
254                                                         const gchar    *str);
255 static void         gtk_entry_set_positions            (GtkEntry       *entry,
256                                                         gint            current_pos,
257                                                         gint            selection_bound);
258 static void         gtk_entry_draw_text                (GtkEntry       *entry);
259 static void         gtk_entry_draw_cursor              (GtkEntry       *entry,
260                                                         CursorType      type);
261 static PangoLayout *gtk_entry_ensure_layout            (GtkEntry       *entry,
262                                                         gboolean        include_preedit);
263 static void         gtk_entry_queue_draw               (GtkEntry       *entry);
264 static void         gtk_entry_reset_im_context         (GtkEntry       *entry);
265 static void         gtk_entry_recompute                (GtkEntry       *entry);
266 static gint         gtk_entry_find_position            (GtkEntry       *entry,
267                                                         gint            x);
268 static void         gtk_entry_get_cursor_locations     (GtkEntry       *entry,
269                                                         CursorType      type,
270                                                         gint           *strong_x,
271                                                         gint           *weak_x);
272 static void         gtk_entry_adjust_scroll            (GtkEntry       *entry);
273 static gint         gtk_entry_move_visually            (GtkEntry       *editable,
274                                                         gint            start,
275                                                         gint            count);
276 static gint         gtk_entry_move_logically           (GtkEntry       *entry,
277                                                         gint            start,
278                                                         gint            count);
279 static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
280                                                         gint            start);
281 static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
282                                                         gint            start);
283 static void         gtk_entry_delete_whitespace        (GtkEntry       *entry);
284 static void         gtk_entry_select_word              (GtkEntry       *entry);
285 static void         gtk_entry_select_line              (GtkEntry       *entry);
286 static char *       gtk_entry_get_public_chars         (GtkEntry       *entry,
287                                                         gint            start,
288                                                         gint            end);
289 static void         gtk_entry_paste                    (GtkEntry       *entry,
290                                                         GdkAtom         selection);
291 static void         gtk_entry_update_primary_selection (GtkEntry       *entry);
292 static void         gtk_entry_do_popup                 (GtkEntry       *entry,
293                                                         GdkEventButton *event);
294 static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
295                                                         gboolean        group_cycling);
296 static void         gtk_entry_check_cursor_blink       (GtkEntry       *entry);
297 static void         gtk_entry_pend_cursor_blink        (GtkEntry       *entry);
298 static void         get_text_area_size                 (GtkEntry       *entry,
299                                                         gint           *x,
300                                                         gint           *y,
301                                                         gint           *width,
302                                                         gint           *height);
303 static void         get_widget_window_size             (GtkEntry       *entry,
304                                                         gint           *x,
305                                                         gint           *y,
306                                                         gint           *width,
307                                                         gint           *height);
308
309 static GtkWidgetClass *parent_class = NULL;
310
311 GtkType
312 gtk_entry_get_type (void)
313 {
314   static GtkType entry_type = 0;
315
316   if (!entry_type)
317     {
318       static const GtkTypeInfo entry_info =
319       {
320         "GtkEntry",
321         sizeof (GtkEntry),
322         sizeof (GtkEntryClass),
323         (GtkClassInitFunc) gtk_entry_class_init,
324         (GtkObjectInitFunc) gtk_entry_init,
325         /* reserved_1 */ NULL,
326         /* reserved_2 */ NULL,
327         (GtkClassInitFunc) NULL,
328       };
329       
330       static const GInterfaceInfo editable_info =
331       {
332         (GInterfaceInitFunc) gtk_entry_editable_init,    /* interface_init */
333         NULL,                                            /* interface_finalize */
334         NULL                                             /* interface_data */
335       };
336
337       static const GInterfaceInfo cell_editable_info =
338       {
339         (GInterfaceInitFunc) gtk_entry_cell_editable_init,    /* interface_init */
340         NULL,                                                 /* interface_finalize */
341         NULL                                                  /* interface_data */
342       };
343       
344       entry_type = gtk_type_unique (GTK_TYPE_WIDGET, &entry_info);
345       g_type_add_interface_static (entry_type,
346                                    GTK_TYPE_EDITABLE,
347                                    &editable_info);
348       g_type_add_interface_static (entry_type,
349                                    GTK_TYPE_CELL_EDITABLE,
350                                    &cell_editable_info);
351     }
352
353   return entry_type;
354 }
355
356 static void
357 add_move_binding (GtkBindingSet  *binding_set,
358                   guint           keyval,
359                   guint           modmask,
360                   GtkMovementStep step,
361                   gint            count)
362 {
363   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
364   
365   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
366                                 "move_cursor", 3,
367                                 GTK_TYPE_ENUM, step,
368                                 G_TYPE_INT, count,
369                                 G_TYPE_BOOLEAN, FALSE);
370
371   /* Selection-extending version */
372   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
373                                 "move_cursor", 3,
374                                 GTK_TYPE_ENUM, step,
375                                 G_TYPE_INT, count,
376                                 G_TYPE_BOOLEAN, TRUE);
377 }
378
379 static void
380 gtk_entry_class_init (GtkEntryClass *class)
381 {
382   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
383   GtkObjectClass *object_class;
384   GtkWidgetClass *widget_class;
385   GtkBindingSet *binding_set;
386
387   object_class = (GtkObjectClass*) class;
388   widget_class = (GtkWidgetClass*) class;
389   parent_class = gtk_type_class (GTK_TYPE_WIDGET);
390
391   gobject_class->finalize = gtk_entry_finalize;
392   gobject_class->set_property = gtk_entry_set_property;
393   gobject_class->get_property = gtk_entry_get_property;
394
395   widget_class->realize = gtk_entry_realize;
396   widget_class->unrealize = gtk_entry_unrealize;
397   widget_class->size_request = gtk_entry_size_request;
398   widget_class->size_allocate = gtk_entry_size_allocate;
399   widget_class->expose_event = gtk_entry_expose;
400   widget_class->button_press_event = gtk_entry_button_press;
401   widget_class->button_release_event = gtk_entry_button_release;
402   widget_class->motion_notify_event = gtk_entry_motion_notify;
403   widget_class->key_press_event = gtk_entry_key_press;
404   widget_class->key_release_event = gtk_entry_key_release;
405   widget_class->focus_in_event = gtk_entry_focus_in;
406   widget_class->focus_out_event = gtk_entry_focus_out;
407   widget_class->grab_focus = gtk_entry_grab_focus;
408   widget_class->style_set = gtk_entry_style_set;
409   widget_class->direction_changed = gtk_entry_direction_changed;
410   widget_class->state_changed = gtk_entry_state_changed;
411   widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
412
413   widget_class->drag_drop = gtk_entry_drag_drop;
414   widget_class->drag_motion = gtk_entry_drag_motion;
415   widget_class->drag_leave = gtk_entry_drag_leave;
416   widget_class->drag_data_received = gtk_entry_drag_data_received;
417   widget_class->drag_data_get = gtk_entry_drag_data_get;
418   widget_class->drag_data_delete = gtk_entry_drag_data_delete;
419
420   widget_class->popup_menu = gtk_entry_popup_menu;
421
422   class->move_cursor = gtk_entry_move_cursor;
423   class->insert_at_cursor = gtk_entry_insert_at_cursor;
424   class->delete_from_cursor = gtk_entry_delete_from_cursor;
425   class->cut_clipboard = gtk_entry_cut_clipboard;
426   class->copy_clipboard = gtk_entry_copy_clipboard;
427   class->paste_clipboard = gtk_entry_paste_clipboard;
428   class->toggle_overwrite = gtk_entry_toggle_overwrite;
429   class->activate = gtk_entry_real_activate;
430   
431   g_object_class_install_property (gobject_class,
432                                    PROP_CURSOR_POSITION,
433                                    g_param_spec_int ("cursor_position",
434                                                      _("Cursor Position"),
435                                                      _("The current position of the insertion cursor in chars."),
436                                                      0,
437                                                      G_MAXINT,
438                                                      0,
439                                                      G_PARAM_READABLE));
440   
441   g_object_class_install_property (gobject_class,
442                                    PROP_SELECTION_BOUND,
443                                    g_param_spec_int ("selection_bound",
444                                                      _("Selection Bound"),
445                                                      _("The position of the opposite end of the selection from the cursor in chars."),
446                                                      0,
447                                                      G_MAXINT,
448                                                      0,
449                                                      G_PARAM_READABLE));
450   
451   g_object_class_install_property (gobject_class,
452                                    PROP_EDITABLE,
453                                    g_param_spec_boolean ("editable",
454                                                          _("Editable"),
455                                                          _("Whether the entry contents can be edited"),
456                                                          TRUE,
457                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
458   
459   g_object_class_install_property (gobject_class,
460                                    PROP_MAX_LENGTH,
461                                    g_param_spec_int ("max_length",
462                                                      _("Maximum length"),
463                                                      _("Maximum number of characters for this entry"),
464                                                      -1,
465                                                      G_MAXINT,
466                                                      -1,
467                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
468   g_object_class_install_property (gobject_class,
469                                    PROP_VISIBILITY,
470                                    g_param_spec_boolean ("visibility",
471                                                          _("Visibility"),
472                                                          _("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
473                                                          TRUE,
474                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
475
476   g_object_class_install_property (gobject_class,
477                                    PROP_HAS_FRAME,
478                                    g_param_spec_boolean ("has_frame",
479                                                          _("Has Frame"),
480                                                          _("FALSE removes outside bevel from entry."),
481                                                          TRUE,
482                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
483
484     g_object_class_install_property (gobject_class,
485                                    PROP_INVISIBLE_CHAR,
486                                    g_param_spec_unichar ("invisible_char",
487                                                          _("Invisible character"),
488                                                          _("The character to use when masking entry contents (in \"password mode\")"),
489                                                          '*',
490                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
491
492   g_object_class_install_property (gobject_class,
493                                    PROP_ACTIVATES_DEFAULT,
494                                    g_param_spec_boolean ("activates_default",
495                                                          _("Activates default"),
496                                                          _("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed."),
497                                                          FALSE,
498                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
499   g_object_class_install_property (gobject_class,
500                                    PROP_WIDTH_CHARS,
501                                    g_param_spec_int ("width_chars",
502                                                      _("Width in chars"),
503                                                      _("Number of characters to leave space for in the entry."),
504                                                      -1,
505                                                      G_MAXINT,
506                                                      -1,
507                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
508
509   g_object_class_install_property (gobject_class,
510                                    PROP_SCROLL_OFFSET,
511                                    g_param_spec_int ("scroll_offset",
512                                                      _("Scroll offset"),
513                                                      _("Number of pixels of the entry scrolled off the screen to the left"),
514                                                      0,
515                                                      G_MAXINT,
516                                                      0,
517                                                      G_PARAM_READABLE));
518
519   g_object_class_install_property (gobject_class,
520                                    PROP_TEXT,
521                                    g_param_spec_string ("text",
522                                                         _("Text"),
523                                                         _("The contents of the entry"),
524                                                         "",
525                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
526   
527   gtk_widget_class_install_style_property (widget_class,
528                                            g_param_spec_boxed ("cursor_color",
529                                                                _("Cursor color"),
530                                                                _("Color with which to draw insertion cursor"),
531                                                                GDK_TYPE_COLOR,
532                                                                G_PARAM_READABLE));
533
534   signals[POPULATE_POPUP] =
535     gtk_signal_new ("populate_popup",
536                     GTK_RUN_LAST,
537                     GTK_CLASS_TYPE (object_class),
538                     GTK_SIGNAL_OFFSET (GtkEntryClass, populate_popup),
539                     _gtk_marshal_VOID__OBJECT,
540                     GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
541   
542  /* Action signals */
543   
544   signals[ACTIVATE] =
545     gtk_signal_new ("activate",
546                     GTK_RUN_LAST | GTK_RUN_ACTION,
547                     GTK_CLASS_TYPE (object_class),
548                     GTK_SIGNAL_OFFSET (GtkEntryClass, activate),
549                     _gtk_marshal_VOID__VOID,
550                     GTK_TYPE_NONE, 0);
551   widget_class->activate_signal = signals[ACTIVATE];
552
553   signals[MOVE_CURSOR] = 
554       gtk_signal_new ("move_cursor",
555                       GTK_RUN_LAST | GTK_RUN_ACTION,
556                       GTK_CLASS_TYPE (object_class),
557                       GTK_SIGNAL_OFFSET (GtkEntryClass, move_cursor),
558                       _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
559                       GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
560
561   signals[INSERT_AT_CURSOR] = 
562       gtk_signal_new ("insert_at_cursor",
563                       GTK_RUN_LAST | GTK_RUN_ACTION,
564                       GTK_CLASS_TYPE (object_class),
565                       GTK_SIGNAL_OFFSET (GtkEntryClass, insert_at_cursor),
566                       _gtk_marshal_VOID__STRING,
567                       GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
568
569   signals[DELETE_FROM_CURSOR] = 
570       gtk_signal_new ("delete_from_cursor",
571                       GTK_RUN_LAST | GTK_RUN_ACTION,
572                       GTK_CLASS_TYPE (object_class),
573                       GTK_SIGNAL_OFFSET (GtkEntryClass, delete_from_cursor),
574                       _gtk_marshal_VOID__ENUM_INT,
575                       GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
576
577   signals[CUT_CLIPBOARD] =
578     gtk_signal_new ("cut_clipboard",
579                     GTK_RUN_LAST | GTK_RUN_ACTION,
580                     GTK_CLASS_TYPE (object_class),
581                     GTK_SIGNAL_OFFSET (GtkEntryClass, cut_clipboard),
582                     _gtk_marshal_VOID__VOID,
583                     GTK_TYPE_NONE, 0);
584
585   signals[COPY_CLIPBOARD] =
586     gtk_signal_new ("copy_clipboard",
587                     GTK_RUN_LAST | GTK_RUN_ACTION,
588                     GTK_CLASS_TYPE (object_class),
589                     GTK_SIGNAL_OFFSET (GtkEntryClass, copy_clipboard),
590                     _gtk_marshal_VOID__VOID,
591                     GTK_TYPE_NONE, 0);
592
593   signals[PASTE_CLIPBOARD] =
594     gtk_signal_new ("paste_clipboard",
595                     GTK_RUN_LAST | GTK_RUN_ACTION,
596                     GTK_CLASS_TYPE (object_class),
597                     GTK_SIGNAL_OFFSET (GtkEntryClass, paste_clipboard),
598                     _gtk_marshal_VOID__VOID,
599                     GTK_TYPE_NONE, 0);
600
601   signals[TOGGLE_OVERWRITE] =
602     gtk_signal_new ("toggle_overwrite",
603                     GTK_RUN_LAST | GTK_RUN_ACTION,
604                     GTK_CLASS_TYPE (object_class),
605                     GTK_SIGNAL_OFFSET (GtkEntryClass, toggle_overwrite),
606                     _gtk_marshal_VOID__VOID,
607                     GTK_TYPE_NONE, 0);
608
609   /*
610    * Key bindings
611    */
612
613   binding_set = gtk_binding_set_by_class (class);
614
615   /* Moving the insertion point */
616   add_move_binding (binding_set, GDK_Right, 0,
617                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
618   
619   add_move_binding (binding_set, GDK_Left, 0,
620                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
621
622   add_move_binding (binding_set, GDK_KP_Right, 0,
623                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
624   
625   add_move_binding (binding_set, GDK_KP_Left, 0,
626                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
627   
628   add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
629                     GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
630   
631   add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
632                     GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
633   
634   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
635                     GTK_MOVEMENT_WORDS, 1);
636
637   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
638                     GTK_MOVEMENT_WORDS, -1);
639
640   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
641                     GTK_MOVEMENT_WORDS, 1);
642
643   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
644                     GTK_MOVEMENT_WORDS, -1);
645   
646   add_move_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
647                     GTK_MOVEMENT_PARAGRAPH_ENDS, -1);
648
649   add_move_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
650                     GTK_MOVEMENT_PARAGRAPH_ENDS, 1);
651
652   add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
653                     GTK_MOVEMENT_WORDS, 1);
654
655   add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
656                     GTK_MOVEMENT_WORDS, -1);
657
658   add_move_binding (binding_set, GDK_Home, 0,
659                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
660
661   add_move_binding (binding_set, GDK_End, 0,
662                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
663
664   add_move_binding (binding_set, GDK_KP_Home, 0,
665                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
666
667   add_move_binding (binding_set, GDK_KP_End, 0,
668                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
669   
670   add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
671                     GTK_MOVEMENT_BUFFER_ENDS, -1);
672
673   add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
674                     GTK_MOVEMENT_BUFFER_ENDS, 1);
675
676   add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
677                     GTK_MOVEMENT_BUFFER_ENDS, -1);
678
679   add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
680                     GTK_MOVEMENT_BUFFER_ENDS, 1);
681
682   /* Activate */
683
684   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
685                                 "activate", 0);
686
687   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
688                                 "activate", 0);
689   
690   /* Deleting text */
691   gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
692                                 "delete_from_cursor", 2,
693                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
694                                 GTK_TYPE_INT, 1);
695
696   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
697                                 "delete_from_cursor", 2,
698                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
699                                 GTK_TYPE_INT, 1);
700   
701   gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_CONTROL_MASK,
702                                 "delete_from_cursor", 2,
703                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
704                                 GTK_TYPE_INT, 1);
705
706   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
707                                 "delete_from_cursor", 2,
708                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
709                                 GTK_TYPE_INT, -1);
710
711   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
712                                 "delete_from_cursor", 2,
713                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
714                                 GTK_TYPE_INT, 1);
715
716   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
717                                 "delete_from_cursor", 2,
718                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
719                                 GTK_TYPE_INT, 1);
720   
721   gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_MOD1_MASK,
722                                 "delete_from_cursor", 2,
723                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
724                                 GTK_TYPE_INT, 1);
725
726   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
727                                 "delete_from_cursor", 2,
728                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
729                                 GTK_TYPE_INT, -1);
730
731   gtk_binding_entry_add_signal (binding_set, GDK_k, GDK_CONTROL_MASK,
732                                 "delete_from_cursor", 2,
733                                 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
734                                 GTK_TYPE_INT, 1);
735
736   gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK,
737                                 "delete_from_cursor", 2,
738                                 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPHS,
739                                 GTK_TYPE_INT, 1);
740
741   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
742                                 "delete_from_cursor", 2,
743                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
744                                 GTK_TYPE_INT, 1);
745   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
746                                 "delete_from_cursor", 2,
747                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
748                                 GTK_TYPE_INT, 1);
749   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
750                                 "insert_at_cursor", 1,
751                                 GTK_TYPE_STRING, " ");
752   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
753                                 "insert_at_cursor", 1,
754                                 GTK_TYPE_STRING, " ");
755   
756   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_MOD1_MASK,
757                                 "delete_from_cursor", 2,
758                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
759                                 GTK_TYPE_INT, 1);
760   
761   /* Cut/copy/paste */
762
763   gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
764                                 "cut_clipboard", 0);
765
766   gtk_binding_entry_add_signal (binding_set, GDK_w, GDK_CONTROL_MASK,
767                                 "cut_clipboard", 0);
768   
769   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
770                                 "copy_clipboard", 0);
771   
772   gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
773                                 "paste_clipboard", 0);
774
775   gtk_binding_entry_add_signal (binding_set, GDK_y, GDK_CONTROL_MASK,
776                                 "paste_clipboard", 0);
777
778   /* Overwrite */
779   gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
780                                 "toggle_overwrite", 0);
781   gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
782                                 "toggle_overwrite", 0);
783 }
784
785 static void
786 gtk_entry_editable_init (GtkEditableClass *iface)
787 {
788   iface->do_insert_text = gtk_entry_insert_text;
789   iface->do_delete_text = gtk_entry_delete_text;
790   iface->insert_text = gtk_entry_real_insert_text;
791   iface->delete_text = gtk_entry_real_delete_text;
792   iface->get_chars = gtk_entry_get_chars;
793   iface->set_selection_bounds = gtk_entry_set_selection_bounds;
794   iface->get_selection_bounds = gtk_entry_get_selection_bounds;
795   iface->set_position = gtk_entry_real_set_position;
796   iface->get_position = gtk_entry_get_position;
797 }
798
799 static void
800 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
801 {
802   iface->start_editing = gtk_entry_start_editing;
803 }
804
805 static void
806 gtk_entry_set_property (GObject         *object,
807                         guint            prop_id,
808                         const GValue    *value,
809                         GParamSpec      *pspec)
810 {
811   GtkEntry *entry = GTK_ENTRY (object);
812
813   switch (prop_id)
814     {
815     case PROP_EDITABLE:
816       {
817         gboolean new_value = g_value_get_boolean (value);
818
819         if (new_value != entry->editable)
820           {
821             entry->editable = new_value;
822             gtk_entry_queue_draw (entry);
823           }
824       }
825       break;
826
827     case PROP_MAX_LENGTH:
828       gtk_entry_set_max_length (entry, g_value_get_int (value));
829       break;
830       
831     case PROP_VISIBILITY:
832       gtk_entry_set_visibility (entry, g_value_get_boolean (value));
833       break;
834
835     case PROP_HAS_FRAME:
836       gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
837       break;
838
839     case PROP_INVISIBLE_CHAR:
840       gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
841       break;
842
843     case PROP_ACTIVATES_DEFAULT:
844       gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
845       break;
846
847     case PROP_WIDTH_CHARS:
848       gtk_entry_set_width_chars (entry, g_value_get_int (value));
849       break;
850
851     case PROP_TEXT:
852       gtk_entry_set_text (entry, g_value_get_string (value));
853       break;
854
855     case PROP_SCROLL_OFFSET:
856     case PROP_CURSOR_POSITION:
857     default:
858       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
859       break;
860     }
861 }
862
863 static void
864 gtk_entry_get_property (GObject         *object,
865                         guint            prop_id,
866                         GValue          *value,
867                         GParamSpec      *pspec)
868 {
869   GtkEntry *entry = GTK_ENTRY (object);
870
871   switch (prop_id)
872     {
873     case PROP_CURSOR_POSITION:
874       g_value_set_int (value, entry->current_pos);
875       break;
876     case PROP_SELECTION_BOUND:
877       g_value_set_int (value, entry->selection_bound);
878       break;
879     case PROP_EDITABLE:
880       g_value_set_boolean (value, entry->editable);
881       break;
882     case PROP_MAX_LENGTH:
883       g_value_set_int (value, entry->text_max_length); 
884       break;
885     case PROP_VISIBILITY:
886       g_value_set_boolean (value, entry->visible);
887       break;
888     case PROP_HAS_FRAME:
889       g_value_set_boolean (value, entry->has_frame);
890       break;
891     case PROP_INVISIBLE_CHAR:
892       g_value_set_uint (value, entry->invisible_char);
893       break;
894     case PROP_ACTIVATES_DEFAULT:
895       g_value_set_boolean (value, entry->activates_default);
896       break;
897     case PROP_WIDTH_CHARS:
898       g_value_set_int (value, entry->width_chars);
899       break;
900     case PROP_SCROLL_OFFSET:
901       g_value_set_int (value, entry->scroll_offset);
902       break;
903     case PROP_TEXT:
904       g_value_set_string (value, gtk_entry_get_text (entry));
905       break;
906       
907     default:
908       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
909       break;
910     }
911 }
912
913 static void
914 gtk_entry_init (GtkEntry *entry)
915 {
916   GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
917
918   entry->text_size = MIN_SIZE;
919   entry->text = g_malloc (entry->text_size);
920   entry->text[0] = '\0';
921
922   entry->editable = TRUE;
923   entry->visible = TRUE;
924   entry->invisible_char = '*';
925   entry->dnd_position = -1;
926   entry->width_chars = -1;
927   entry->is_cell_renderer = FALSE;
928   entry->editing_canceled = FALSE;
929   entry->has_frame = TRUE;
930
931   gtk_drag_dest_set (GTK_WIDGET (entry),
932                      GTK_DEST_DEFAULT_HIGHLIGHT,
933                      target_table, G_N_ELEMENTS (target_table),
934                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
935
936   /* This object is completely private. No external entity can gain a reference
937    * to it; so we create it here and destroy it in finalize().
938    */
939   entry->im_context = gtk_im_multicontext_new ();
940   
941   g_signal_connect (G_OBJECT (entry->im_context), "commit",
942                     G_CALLBACK (gtk_entry_commit_cb), entry);
943   g_signal_connect (G_OBJECT (entry->im_context), "preedit_changed",
944                     G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
945   g_signal_connect (G_OBJECT (entry->im_context), "retrieve_surrounding",
946                     G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
947   g_signal_connect (G_OBJECT (entry->im_context), "delete_surrounding",
948                     G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
949 }
950
951 static void
952 gtk_entry_finalize (GObject *object)
953 {
954   GtkEntry *entry = GTK_ENTRY (object);
955
956   if (entry->cached_layout)
957     g_object_unref (G_OBJECT (entry->cached_layout));
958
959   g_object_unref (G_OBJECT (entry->im_context));
960
961   if (entry->blink_timeout)
962     g_source_remove (entry->blink_timeout);
963
964   if (entry->recompute_idle)
965     g_source_remove (entry->recompute_idle);
966
967   entry->text_size = 0;
968
969   if (entry->text)
970     g_free (entry->text);
971   entry->text = NULL;
972
973   G_OBJECT_CLASS (parent_class)->finalize (object);
974 }
975
976 static void
977 gtk_entry_realize_cursor_gc (GtkEntry *entry)
978 {
979   GdkColor *cursor_color;
980   GdkColor red = {0, 0xffff, 0x0000, 0x0000};
981   
982   if (entry->cursor_gc)
983     gdk_gc_unref (entry->cursor_gc);
984
985   gtk_widget_style_get (GTK_WIDGET (entry), "cursor_color", &cursor_color, NULL);
986       entry->cursor_gc = gdk_gc_new (entry->text_area);
987   if (cursor_color)
988     gdk_gc_set_rgb_fg_color (entry->cursor_gc, cursor_color);
989   else
990     gdk_gc_set_rgb_fg_color (entry->cursor_gc, &red);
991 }
992
993 static void
994 gtk_entry_realize (GtkWidget *widget)
995 {
996   GtkEntry *entry;
997   GtkEditable *editable;
998   GdkWindowAttr attributes;
999   gint attributes_mask;
1000
1001   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1002   entry = GTK_ENTRY (widget);
1003   editable = GTK_EDITABLE (widget);
1004
1005   attributes.window_type = GDK_WINDOW_CHILD;
1006   
1007   get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1008
1009   attributes.wclass = GDK_INPUT_OUTPUT;
1010   attributes.visual = gtk_widget_get_visual (widget);
1011   attributes.colormap = gtk_widget_get_colormap (widget);
1012   attributes.event_mask = gtk_widget_get_events (widget);
1013   attributes.event_mask |= (GDK_EXPOSURE_MASK |
1014                             GDK_BUTTON_PRESS_MASK |
1015                             GDK_BUTTON_RELEASE_MASK |
1016                             GDK_BUTTON1_MOTION_MASK |
1017                             GDK_BUTTON3_MOTION_MASK |
1018                             GDK_POINTER_MOTION_HINT_MASK |
1019                             GDK_POINTER_MOTION_MASK |
1020                             GDK_ENTER_NOTIFY_MASK |
1021                             GDK_LEAVE_NOTIFY_MASK);
1022   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1023
1024   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1025   gdk_window_set_user_data (widget->window, entry);
1026
1027   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1028
1029   attributes.cursor = gdk_cursor_new (GDK_XTERM);
1030   attributes_mask |= GDK_WA_CURSOR;
1031
1032   entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1033   gdk_window_set_user_data (entry->text_area, entry);
1034
1035   gdk_cursor_unref (attributes.cursor);
1036
1037   widget->style = gtk_style_attach (widget->style, widget->window);
1038
1039   gtk_entry_realize_cursor_gc (entry);
1040
1041   gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1042   gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1043
1044   gdk_window_show (entry->text_area);
1045
1046   gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1047
1048   gtk_entry_adjust_scroll (entry);
1049 }
1050
1051 static void
1052 gtk_entry_unrealize (GtkWidget *widget)
1053 {
1054   GtkEntry *entry = GTK_ENTRY (widget);
1055
1056   gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1057   
1058   if (entry->text_area)
1059     {
1060       gdk_window_set_user_data (entry->text_area, NULL);
1061       gdk_window_destroy (entry->text_area);
1062       entry->text_area = NULL;
1063     }
1064
1065   if (entry->cursor_gc)
1066     {
1067       gdk_gc_unref (entry->cursor_gc);
1068       entry->cursor_gc = NULL;
1069     }
1070
1071   if (entry->popup_menu)
1072     {
1073       gtk_widget_destroy (entry->popup_menu);
1074       entry->popup_menu = NULL;
1075     }
1076
1077   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1078     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1079 }
1080
1081 static void
1082 get_borders (GtkEntry *entry,
1083              gint     *xborder,
1084              gint     *yborder)
1085 {
1086   GtkWidget *widget = GTK_WIDGET (entry);
1087   gint focus_width;
1088   gboolean interior_focus;
1089
1090   gtk_widget_style_get (widget,
1091                         "interior-focus", &interior_focus,
1092                         "focus-line-width", &focus_width,
1093                         NULL);
1094
1095   if (entry->has_frame)
1096     {
1097       *xborder = widget->style->xthickness;
1098       *yborder = widget->style->ythickness;
1099     }
1100   else
1101     {
1102       *xborder = 0;
1103       *yborder = 0;
1104     }
1105
1106   if (!interior_focus)
1107     {
1108       *xborder += focus_width;
1109       *yborder += focus_width;
1110     }
1111 }
1112
1113 static void
1114 gtk_entry_size_request (GtkWidget      *widget,
1115                         GtkRequisition *requisition)
1116 {
1117   GtkEntry *entry = GTK_ENTRY (widget);
1118   PangoFontMetrics *metrics;
1119   gint xborder, yborder;
1120   PangoContext *context;
1121   
1122   context = gtk_widget_get_pango_context (widget);
1123   metrics = pango_context_get_metrics (context,
1124                                        widget->style->font_desc,
1125                                        pango_context_get_language (context));
1126
1127   entry->ascent = pango_font_metrics_get_ascent (metrics);
1128   entry->descent = pango_font_metrics_get_descent (metrics);
1129
1130   get_borders (entry, &xborder, &yborder);
1131   
1132   xborder += INNER_BORDER;
1133   yborder += INNER_BORDER;
1134   
1135   if (entry->width_chars < 0)
1136     requisition->width = MIN_ENTRY_WIDTH + xborder * 2;
1137   else
1138     {
1139       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1140       requisition->width = PANGO_PIXELS (char_width) * entry->width_chars + xborder * 2;
1141     }
1142     
1143   requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2;
1144
1145   pango_font_metrics_unref (metrics);
1146 }
1147
1148 static void
1149 get_text_area_size (GtkEntry *entry,
1150                     gint     *x,
1151                     gint     *y,
1152                     gint     *width,
1153                     gint     *height)
1154 {
1155   gint xborder, yborder;
1156   GtkRequisition requisition;
1157   GtkWidget *widget = GTK_WIDGET (entry);
1158
1159   gtk_widget_get_child_requisition (widget, &requisition);
1160
1161   get_borders (entry, &xborder, &yborder);
1162
1163   if (x)
1164     *x = xborder;
1165
1166   if (y)
1167     *y = yborder;
1168   
1169   if (width)
1170     *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1171
1172   if (height)
1173     *height = requisition.height - yborder * 2;
1174 }
1175
1176 static void
1177 get_widget_window_size (GtkEntry *entry,
1178                         gint     *x,
1179                         gint     *y,
1180                         gint     *width,
1181                         gint     *height)
1182 {
1183   GtkRequisition requisition;
1184   GtkWidget *widget = GTK_WIDGET (entry);
1185       
1186   gtk_widget_get_child_requisition (widget, &requisition);
1187
1188   if (x)
1189     *x = widget->allocation.x;
1190
1191   if (y)
1192     {
1193       if (entry->is_cell_renderer)
1194         *y = widget->allocation.y;
1195       else
1196         *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1197     }
1198
1199   if (width)
1200     *width = widget->allocation.width;
1201
1202   if (height)
1203     {
1204       if (entry->is_cell_renderer)
1205         *height = widget->allocation.height;
1206       else
1207         *height = requisition.height;
1208     }
1209 }
1210
1211 static void
1212 gtk_entry_size_allocate (GtkWidget     *widget,
1213                          GtkAllocation *allocation)
1214 {
1215   GtkEntry *entry = GTK_ENTRY (widget);
1216   
1217   widget->allocation = *allocation;
1218   
1219   if (GTK_WIDGET_REALIZED (widget))
1220     {
1221       /* We call gtk_widget_get_child_requisition, since we want (for
1222        * backwards compatibility reasons) the realization here to
1223        * be affected by the usize of the entry, if set
1224        */
1225       gint x, y, width, height;
1226
1227       get_widget_window_size (entry, &x, &y, &width, &height);
1228       
1229       gdk_window_move_resize (widget->window,
1230                               x, y, width, height);   
1231
1232       get_text_area_size (entry, &x, &y, &width, &height);
1233       
1234       gdk_window_move_resize (entry->text_area,
1235                               x, y, width, height);
1236
1237       gtk_entry_recompute (entry);
1238     }
1239 }
1240
1241 static void
1242 gtk_entry_draw_frame (GtkWidget *widget)
1243 {
1244   gint x = 0, y = 0;
1245   gint width, height;
1246   gboolean interior_focus;
1247   gint focus_width;
1248   
1249   gtk_widget_style_get (widget,
1250                         "interior-focus", &interior_focus,
1251                         "focus-line-width", &focus_width,
1252                         NULL);
1253   
1254   gdk_window_get_size (widget->window, &width, &height);
1255   
1256   if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1257     {
1258       x += focus_width;
1259       y += focus_width;
1260       width -= 2 * focus_width;
1261       height -= 2 * focus_width;
1262     }
1263
1264   gtk_paint_shadow (widget->style, widget->window,
1265                     GTK_STATE_NORMAL, GTK_SHADOW_IN,
1266                     NULL, widget, "entry",
1267                     x, y, width, height);
1268
1269   if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1270     {
1271       x -= focus_width;
1272       y -= focus_width;
1273       width += 2 * focus_width;
1274       height += 2 * focus_width;
1275       
1276       gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
1277                        NULL, widget, "entry",
1278                        0, 0, width, height);
1279     }
1280 }
1281
1282 static gint
1283 gtk_entry_expose (GtkWidget      *widget,
1284                   GdkEventExpose *event)
1285 {
1286   GtkEntry *entry = GTK_ENTRY (widget);
1287
1288   if (widget->window == event->window)
1289     gtk_entry_draw_frame (widget);
1290   else if (entry->text_area == event->window)
1291     {
1292       gtk_entry_draw_text (GTK_ENTRY (widget));
1293
1294       if ((entry->visible || entry->invisible_char != 0) &&
1295           GTK_WIDGET_HAS_FOCUS (widget) &&
1296           entry->selection_bound == entry->current_pos && entry->cursor_visible)
1297         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1298
1299       if (entry->dnd_position != -1)
1300         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1301     }
1302
1303   return FALSE;
1304 }
1305
1306 static gint
1307 gtk_entry_button_press (GtkWidget      *widget,
1308                         GdkEventButton *event)
1309 {
1310   GtkEntry *entry = GTK_ENTRY (widget);
1311   GtkEditable *editable = GTK_EDITABLE (widget);
1312   gint tmp_pos;
1313   gint sel_start, sel_end;
1314
1315   if (event->window != entry->text_area ||
1316       (entry->button && event->button != entry->button))
1317     return FALSE;
1318
1319   entry->button = event->button;
1320   
1321   if (!GTK_WIDGET_HAS_FOCUS (widget))
1322     {
1323       entry->in_click = TRUE;
1324       gtk_widget_grab_focus (widget);
1325       entry->in_click = FALSE;
1326     }
1327   
1328   tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1329     
1330   if (event->button == 1)
1331     {
1332       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1333       
1334       if (event->state & GDK_SHIFT_MASK)
1335         {
1336           gtk_entry_reset_im_context (entry);
1337           
1338           if (!have_selection) /* select from the current position to the clicked position */
1339             sel_start = sel_end = entry->current_pos;
1340           
1341           if (tmp_pos > sel_start && tmp_pos < sel_end)
1342             {
1343               /* Truncate current selection */
1344               gtk_entry_set_positions (entry, tmp_pos, -1);
1345             }
1346           else
1347             {
1348               gboolean extend_to_left;
1349               gint start, end;
1350
1351               /* Figure out what click selects and extend current selection */
1352               switch (event->type)
1353                 {
1354                 case GDK_BUTTON_PRESS:
1355                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1356                   break;
1357                   
1358                 case GDK_2BUTTON_PRESS:
1359                   gtk_entry_select_word (entry);
1360                   break;
1361                   
1362                 case GDK_3BUTTON_PRESS:
1363                   gtk_entry_select_line (entry);
1364                   break;
1365
1366                 default:
1367                   break;
1368                 }
1369
1370               start = MIN (entry->current_pos, entry->selection_bound);
1371               start = MIN (sel_start, start);
1372               
1373               end = MAX (entry->current_pos, entry->selection_bound);
1374               end = MAX (sel_end, end);
1375
1376               if (tmp_pos == sel_start || tmp_pos == sel_end)
1377                 extend_to_left = (tmp_pos == start);
1378               else
1379                 extend_to_left = (end == sel_end);
1380               
1381               if (extend_to_left)
1382                 gtk_entry_set_positions (entry, start, end);
1383               else
1384                 gtk_entry_set_positions (entry, end, start);
1385             }
1386         }
1387       else /* no shift key */
1388         switch (event->type)
1389         {
1390         case GDK_BUTTON_PRESS:
1391           if (have_selection && tmp_pos >= sel_start && tmp_pos <= sel_end)
1392             {
1393               /* Click inside the selection - we'll either start a drag, or
1394                * clear the selection
1395                */
1396
1397               entry->in_drag = TRUE;
1398               entry->drag_start_x = event->x + entry->scroll_offset;
1399               entry->drag_start_y = event->y + entry->scroll_offset;
1400             }
1401           else
1402             gtk_editable_set_position (editable, tmp_pos);
1403           
1404           break;
1405
1406  
1407         case GDK_2BUTTON_PRESS:
1408           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
1409            * receiving a GDK_2BUTTON_PRESS so we need to reset
1410            * entry->in_drag which may have been set above
1411            */
1412           entry->in_drag = FALSE;
1413           gtk_entry_select_word (entry);
1414           break;
1415         
1416         case GDK_3BUTTON_PRESS:
1417           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1418            * receiving a GDK_3BUTTON_PRESS so we need to reset
1419            * entry->in_drag which may have been set above
1420            */
1421           entry->in_drag = FALSE;
1422           gtk_entry_select_line (entry);
1423           break;
1424
1425         default:
1426           break;
1427         }
1428
1429       return TRUE;
1430     }
1431   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS && entry->editable)
1432     {
1433       gtk_editable_select_region (editable, tmp_pos, tmp_pos);
1434       gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1435
1436       return TRUE;
1437     }
1438   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1439     {
1440       gtk_entry_do_popup (entry, event);
1441       entry->button = 0;        /* Don't wait for release, since the menu will gtk_grab_add */
1442
1443       return TRUE;
1444     }
1445
1446   return FALSE;
1447 }
1448
1449 static gint
1450 gtk_entry_button_release (GtkWidget      *widget,
1451                           GdkEventButton *event)
1452 {
1453   GtkEntry *entry = GTK_ENTRY (widget);
1454
1455   if (event->window != entry->text_area || entry->button != event->button)
1456     return FALSE;
1457
1458   if (entry->in_drag)
1459     {
1460       gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1461
1462       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1463
1464       entry->in_drag = 0;
1465     }
1466   
1467   entry->button = 0;
1468   
1469   gtk_entry_update_primary_selection (entry);
1470               
1471   return TRUE;
1472 }
1473
1474 static gint
1475 gtk_entry_motion_notify (GtkWidget      *widget,
1476                          GdkEventMotion *event)
1477 {
1478   GtkEntry *entry = GTK_ENTRY (widget);
1479   gint tmp_pos;
1480
1481   if (entry->mouse_cursor_obscured)
1482     {
1483       GdkCursor *cursor;
1484       
1485       cursor = gdk_cursor_new (GDK_XTERM);
1486       gdk_window_set_cursor (entry->text_area, cursor);
1487       gdk_cursor_unref (cursor);
1488       entry->mouse_cursor_obscured = FALSE;
1489     }
1490
1491   if (event->window != entry->text_area || entry->button !=1)
1492     return FALSE;
1493
1494   if (event->is_hint || (entry->text_area != event->window))
1495     gdk_window_get_pointer (entry->text_area, NULL, NULL, NULL);
1496
1497   if (entry->in_drag)
1498     {
1499       if (gtk_drag_check_threshold (widget,
1500                                     entry->drag_start_x, entry->drag_start_y,
1501                                     event->x + entry->scroll_offset, event->y))
1502         {
1503           GdkDragContext *context;
1504           GtkTargetList *target_list = gtk_target_list_new (target_table, G_N_ELEMENTS (target_table));
1505           guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1506           
1507           context = gtk_drag_begin (widget, target_list, actions,
1508                           entry->button, (GdkEvent *)event);
1509
1510           
1511           entry->in_drag = FALSE;
1512           entry->button = 0;
1513           
1514           gtk_target_list_unref (target_list);
1515           gtk_drag_set_icon_default (context);
1516         }
1517     }
1518   else
1519     {
1520       gint height;
1521       gdk_window_get_size (entry->text_area, NULL, &height);
1522
1523       if (event->y < 0)
1524         tmp_pos = 0;
1525       else if (event->y >= height)
1526         tmp_pos = entry->text_length;
1527       else
1528         tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1529       
1530       gtk_entry_set_positions (entry, tmp_pos, -1);
1531     }
1532       
1533   return TRUE;
1534 }
1535
1536 static void
1537 set_invisible_cursor (GdkWindow *window)
1538 {
1539   GdkBitmap *empty_bitmap;
1540   GdkCursor *cursor;
1541   GdkColor useless;
1542   char invisible_cursor_bits[] = { 0x0 };       
1543         
1544   useless.red = useless.green = useless.blue = 0;
1545   useless.pixel = 0;
1546   
1547   empty_bitmap = gdk_bitmap_create_from_data (window,
1548                                               invisible_cursor_bits,
1549                                               1, 1);
1550   
1551   cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
1552                                        empty_bitmap,
1553                                        &useless,
1554                                        &useless, 0, 0);
1555   
1556   gdk_window_set_cursor (window, cursor);
1557   
1558   gdk_cursor_unref (cursor);
1559   
1560   g_object_unref (empty_bitmap);
1561 }
1562
1563 static void
1564 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
1565 {
1566   if (entry->mouse_cursor_obscured)
1567     return;
1568
1569   set_invisible_cursor (entry->text_area);
1570   
1571   entry->mouse_cursor_obscured = TRUE;  
1572 }
1573
1574 static gint
1575 gtk_entry_key_press (GtkWidget   *widget,
1576                      GdkEventKey *event)
1577 {
1578   GtkEntry *entry = GTK_ENTRY (widget);
1579
1580   if (!entry->editable)
1581     return FALSE;
1582
1583   gtk_entry_pend_cursor_blink (entry);
1584   
1585   if (gtk_im_context_filter_keypress (entry->im_context, event))
1586     {
1587       gtk_entry_obscure_mouse_cursor (entry);
1588       entry->need_im_reset = TRUE;
1589       return TRUE;
1590     }
1591   else if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1592     /* Activate key bindings
1593      */
1594     return TRUE;
1595
1596   return FALSE;
1597 }
1598
1599 static gint
1600 gtk_entry_key_release (GtkWidget   *widget,
1601                        GdkEventKey *event)
1602 {
1603   GtkEntry *entry = GTK_ENTRY (widget);
1604
1605   if (!entry->editable)
1606     return FALSE;
1607
1608   if (gtk_im_context_filter_keypress (entry->im_context, event))
1609     {
1610       entry->need_im_reset = TRUE;
1611       return TRUE;
1612     }
1613   else
1614     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1615 }
1616
1617 static gint
1618 gtk_entry_focus_in (GtkWidget     *widget,
1619                     GdkEventFocus *event)
1620 {
1621   GtkEntry *entry = GTK_ENTRY (widget);
1622   
1623   GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1624   gtk_widget_queue_draw (widget);
1625   
1626   entry->need_im_reset = TRUE;
1627   gtk_im_context_focus_in (entry->im_context);
1628
1629   g_signal_connect (gdk_keymap_get_default (),
1630                     "direction_changed",
1631                     G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
1632
1633   gtk_entry_check_cursor_blink (entry);
1634
1635   return FALSE;
1636 }
1637
1638 static gint
1639 gtk_entry_focus_out (GtkWidget     *widget,
1640                      GdkEventFocus *event)
1641 {
1642   GtkEntry *entry = GTK_ENTRY (widget);
1643   
1644   GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
1645   gtk_widget_queue_draw (widget);
1646
1647   entry->need_im_reset = TRUE;
1648   gtk_im_context_focus_out (entry->im_context);
1649
1650   gtk_entry_check_cursor_blink (entry);
1651   
1652   g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
1653                                         gtk_entry_keymap_direction_changed,
1654                                         entry);
1655   
1656   return FALSE;
1657 }
1658
1659 static void
1660 gtk_entry_grab_focus (GtkWidget        *widget)
1661 {
1662    GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
1663
1664   if (!GTK_ENTRY (widget)->in_click)
1665     gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
1666 }
1667
1668 static void 
1669 gtk_entry_direction_changed (GtkWidget        *widget,
1670                              GtkTextDirection  previous_dir)
1671 {
1672   GtkEntry *entry = GTK_ENTRY (widget);
1673
1674   gtk_entry_recompute (entry);
1675       
1676   GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
1677 }
1678
1679 static void
1680 gtk_entry_state_changed (GtkWidget      *widget,
1681                          GtkStateType    previous_state)
1682 {
1683   if (GTK_WIDGET_REALIZED (widget))
1684     {
1685       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1686       gdk_window_set_background (GTK_ENTRY (widget)->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1687     }
1688
1689   gtk_widget_queue_clear (widget);
1690 }
1691
1692 /* GtkEditable method implementations
1693  */
1694 static void
1695 gtk_entry_insert_text (GtkEditable *editable,
1696                        const gchar *new_text,
1697                        gint         new_text_length,
1698                        gint        *position)
1699 {
1700   GtkEntry *entry = GTK_ENTRY (editable);
1701   gchar buf[64];
1702   gchar *text;
1703
1704   if (*position < 0 || *position > entry->text_length)
1705     *position = entry->text_length;
1706   
1707   g_object_ref (G_OBJECT (editable));
1708   
1709   if (new_text_length <= 63)
1710     text = buf;
1711   else
1712     text = g_new (gchar, new_text_length + 1);
1713
1714   text[new_text_length] = '\0';
1715   strncpy (text, new_text, new_text_length);
1716   
1717   g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
1718   g_signal_emit_by_name (editable, "changed");
1719   g_object_notify (G_OBJECT (editable), "text");
1720
1721   if (new_text_length > 63)
1722     g_free (text);
1723
1724   g_object_unref (G_OBJECT (editable));
1725 }
1726
1727 static void
1728 gtk_entry_delete_text (GtkEditable *editable,
1729                        gint         start_pos,
1730                        gint         end_pos)
1731 {
1732   GtkEntry *entry = GTK_ENTRY (editable);
1733
1734   if (end_pos < 0 || end_pos > entry->text_length)
1735     end_pos = entry->text_length;
1736   if (start_pos < 0)
1737     start_pos = 0;
1738   if (start_pos > end_pos)
1739     start_pos = end_pos;
1740   
1741   g_object_ref (G_OBJECT (editable));
1742
1743   g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
1744   g_signal_emit_by_name (editable, "changed");
1745   g_object_notify (G_OBJECT (editable), "text");
1746
1747   g_object_unref (G_OBJECT (editable));
1748 }
1749
1750 static gchar *    
1751 gtk_entry_get_chars      (GtkEditable   *editable,
1752                           gint           start_pos,
1753                           gint           end_pos)
1754 {
1755   GtkEntry *entry = GTK_ENTRY (editable);
1756   gint start_index, end_index;
1757   
1758   if (end_pos < 0)
1759     end_pos = entry->text_length;
1760
1761   start_pos = MIN (entry->text_length, start_pos);
1762   end_pos = MIN (entry->text_length, end_pos);
1763
1764   start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
1765   end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
1766
1767   return g_strndup (entry->text + start_index, end_index - start_index);
1768 }
1769
1770 static void
1771 gtk_entry_real_set_position (GtkEditable *editable,
1772                              gint         position)
1773 {
1774   GtkEntry *entry = GTK_ENTRY (editable);
1775   
1776   if (position < 0 || position > entry->text_length)
1777     position = entry->text_length;
1778
1779   if (position != entry->current_pos ||
1780       position != entry->selection_bound)
1781     {
1782       gtk_entry_reset_im_context (entry);
1783       gtk_entry_set_positions (entry, position, position);
1784     }
1785 }
1786
1787 static gint
1788 gtk_entry_get_position (GtkEditable *editable)
1789 {
1790   return GTK_ENTRY (editable)->current_pos;
1791 }
1792
1793 static void
1794 gtk_entry_set_selection_bounds (GtkEditable *editable,
1795                                 gint         start,
1796                                 gint         end)
1797 {
1798   GtkEntry *entry = GTK_ENTRY (editable);
1799
1800   if (start < 0)
1801     start = entry->text_length;
1802   if (end < 0)
1803     end = entry->text_length;
1804   
1805   gtk_entry_reset_im_context (entry);
1806
1807   gtk_entry_set_positions (entry,
1808                            MIN (end, entry->text_length),
1809                            MIN (start, entry->text_length));
1810
1811   gtk_entry_update_primary_selection (entry);
1812 }
1813
1814 static gboolean
1815 gtk_entry_get_selection_bounds (GtkEditable *editable,
1816                                 gint        *start,
1817                                 gint        *end)
1818 {
1819   GtkEntry *entry = GTK_ENTRY (editable);
1820
1821   *start = entry->selection_bound;
1822   *end = entry->current_pos;
1823
1824   return (entry->selection_bound != entry->current_pos);
1825 }
1826
1827 static void 
1828 gtk_entry_style_set     (GtkWidget      *widget,
1829                          GtkStyle       *previous_style)
1830 {
1831   GtkEntry *entry = GTK_ENTRY (widget);
1832
1833   if (previous_style && GTK_WIDGET_REALIZED (widget))
1834     {
1835       gtk_entry_recompute (entry);
1836
1837       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1838       gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1839
1840       gtk_entry_realize_cursor_gc (entry);
1841     }
1842 }
1843
1844 /* GtkCellEditable method implementations
1845  */
1846 static void
1847 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
1848 {
1849   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1850   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1851 }
1852
1853 static gboolean
1854 gtk_cell_editable_key_press_event (GtkEntry    *entry,
1855                                    GdkEventKey *key_event,
1856                                    gpointer     data)
1857 {
1858     if (key_event->keyval == GDK_Escape)
1859       {
1860         entry->editing_canceled = TRUE;
1861         gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1862         gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1863
1864         return TRUE;
1865       }
1866
1867     return FALSE;
1868 }
1869
1870 static void
1871 gtk_entry_start_editing (GtkCellEditable *cell_editable,
1872                          GdkEvent        *event)
1873 {
1874   GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
1875
1876   g_signal_connect (G_OBJECT (cell_editable), "activate",
1877                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
1878   g_signal_connect (G_OBJECT (cell_editable), "key_press_event",
1879                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
1880 }
1881
1882 /* Default signal handlers
1883  */
1884 static void
1885 gtk_entry_real_insert_text (GtkEditable *editable,
1886                             const gchar *new_text,
1887                             gint         new_text_length,
1888                             gint        *position)
1889 {
1890   gint index;
1891   gint n_chars;
1892
1893   GtkEntry *entry = GTK_ENTRY (editable);
1894
1895   if (new_text_length < 0)
1896     new_text_length = strlen (new_text);
1897
1898   n_chars = g_utf8_strlen (new_text, new_text_length);
1899   if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
1900     {
1901       gdk_beep ();
1902       n_chars = entry->text_max_length - entry->text_length;
1903       new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
1904     }
1905
1906   if (new_text_length + entry->n_bytes + 1 > entry->text_size)
1907     {
1908       while (new_text_length + entry->n_bytes + 1 > entry->text_size)
1909         {
1910           if (entry->text_size == 0)
1911             entry->text_size = MIN_SIZE;
1912           else
1913             {
1914               if (2 * (guint)entry->text_size < MAX_SIZE &&
1915                   2 * (guint)entry->text_size > entry->text_size)
1916                 entry->text_size *= 2;
1917               else
1918                 {
1919                   entry->text_size = MAX_SIZE;
1920                   new_text_length = entry->text_size - entry->n_bytes - 1;
1921                   break;
1922                 }
1923             }
1924         }
1925
1926       entry->text = g_realloc (entry->text, entry->text_size);
1927     }
1928
1929   index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
1930
1931   g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
1932   memcpy (entry->text + index, new_text, new_text_length);
1933
1934   entry->n_bytes += new_text_length;
1935   entry->text_length += n_chars;
1936
1937   /* NUL terminate for safety and convenience */
1938   entry->text[entry->n_bytes] = '\0';
1939   
1940   if (entry->current_pos > *position)
1941     entry->current_pos += n_chars;
1942   
1943   if (entry->selection_bound > *position)
1944     entry->selection_bound += n_chars;
1945
1946   *position += n_chars;
1947
1948   gtk_entry_recompute (entry);
1949 }
1950
1951 static void
1952 gtk_entry_real_delete_text (GtkEditable *editable,
1953                             gint         start_pos,
1954                             gint         end_pos)
1955 {
1956   GtkEntry *entry = GTK_ENTRY (editable);
1957
1958   if (start_pos < 0)
1959     start_pos = 0;
1960   if (end_pos < 0 || end_pos > entry->text_length)
1961     end_pos = entry->text_length;
1962   
1963   if (start_pos < end_pos)
1964     {
1965       gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
1966       gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
1967
1968       g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
1969       entry->text_length -= (end_pos - start_pos);
1970       entry->n_bytes -= (end_index - start_index);
1971       
1972       if (entry->current_pos > start_pos)
1973         entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
1974
1975       if (entry->selection_bound > start_pos)
1976         entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;
1977     }
1978
1979   /* We might have deleted the selection
1980    */
1981   gtk_entry_update_primary_selection (entry);
1982
1983   gtk_entry_recompute (entry);
1984 }
1985
1986 /* Compute the X position for an offset that corresponds to the "more important
1987  * cursor position for that offset. We use this when trying to guess to which
1988  * end of the selection we should go to when the user hits the left or
1989  * right arrow key.
1990  */
1991 static gint
1992 get_better_cursor_x (GtkEntry *entry,
1993                      gint      offset)
1994 {
1995   GtkTextDirection keymap_direction =
1996     (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1997     GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1998   GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
1999   gboolean split_cursor;
2000   
2001   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2002   gint index = g_utf8_offset_to_pointer (entry->text, offset) - entry->text;
2003   
2004   PangoRectangle strong_pos, weak_pos;
2005   
2006   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2007                 "gtk-split-cursor", &split_cursor,
2008                 NULL);
2009
2010   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2011
2012   if (split_cursor)
2013     return strong_pos.x / PANGO_SCALE;
2014   else
2015     return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2016 }
2017
2018 static void
2019 gtk_entry_move_cursor (GtkEntry       *entry,
2020                        GtkMovementStep step,
2021                        gint            count,
2022                        gboolean        extend_selection)
2023 {
2024   gint new_pos = entry->current_pos;
2025
2026   gtk_entry_reset_im_context (entry);
2027
2028   if (entry->current_pos != entry->selection_bound && !extend_selection)
2029     {
2030       /* If we have a current selection and aren't extending it, move to the
2031        * start/or end of the selection as appropriate
2032        */
2033       switch (step)
2034         {
2035         case GTK_MOVEMENT_VISUAL_POSITIONS:
2036           {
2037             gint current_x = get_better_cursor_x (entry, entry->current_pos);
2038             gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2039
2040             if (count < 0)
2041               new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2042             else
2043               new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2044
2045             break;
2046           }
2047         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2048         case GTK_MOVEMENT_WORDS:
2049           if (count < 0)
2050             new_pos = MIN (entry->current_pos, entry->selection_bound);
2051           else
2052             new_pos = MAX (entry->current_pos, entry->selection_bound);
2053           break;
2054         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2055         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2056         case GTK_MOVEMENT_BUFFER_ENDS:
2057           new_pos = count < 0 ? 0 : entry->text_length;
2058           break;
2059         case GTK_MOVEMENT_DISPLAY_LINES:
2060         case GTK_MOVEMENT_PARAGRAPHS:
2061         case GTK_MOVEMENT_PAGES:
2062           break;
2063         }
2064     }
2065   else
2066     {
2067       switch (step)
2068         {
2069         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2070           new_pos = gtk_entry_move_logically (entry, new_pos, count);
2071           break;
2072         case GTK_MOVEMENT_VISUAL_POSITIONS:
2073           new_pos = gtk_entry_move_visually (entry, new_pos, count);
2074           break;
2075         case GTK_MOVEMENT_WORDS:
2076           while (count > 0)
2077             {
2078               new_pos = gtk_entry_move_forward_word (entry, new_pos);
2079               count--;
2080             }
2081           while (count < 0)
2082             {
2083               new_pos = gtk_entry_move_backward_word (entry, new_pos);
2084               count++;
2085             }
2086           break;
2087         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2088         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2089         case GTK_MOVEMENT_BUFFER_ENDS:
2090           new_pos = count < 0 ? 0 : entry->text_length;
2091           break;
2092         case GTK_MOVEMENT_DISPLAY_LINES:
2093         case GTK_MOVEMENT_PARAGRAPHS:
2094         case GTK_MOVEMENT_PAGES:
2095           break;
2096         }
2097     }
2098
2099   if (extend_selection)
2100     gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2101   else
2102     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2103   
2104   gtk_entry_pend_cursor_blink (entry);
2105 }
2106
2107 static void
2108 gtk_entry_insert_at_cursor (GtkEntry    *entry,
2109                             const gchar *str)
2110 {
2111   GtkEditable *editable = GTK_EDITABLE (entry);
2112   gint pos = entry->current_pos;
2113
2114   gtk_entry_reset_im_context (entry);
2115
2116   gtk_editable_insert_text (editable, str, -1, &pos);
2117   gtk_editable_set_position (editable, pos);
2118 }
2119
2120 static void
2121 gtk_entry_delete_from_cursor (GtkEntry       *entry,
2122                               GtkDeleteType   type,
2123                               gint            count)
2124 {
2125   GtkEditable *editable = GTK_EDITABLE (entry);
2126   gint start_pos = entry->current_pos;
2127   gint end_pos = entry->current_pos;
2128   
2129   gtk_entry_reset_im_context (entry);
2130
2131   if (!entry->editable)
2132     return;
2133
2134   if (entry->selection_bound != entry->current_pos)
2135     {
2136       gtk_editable_delete_selection (editable);
2137       return;
2138     }
2139   
2140   switch (type)
2141     {
2142     case GTK_DELETE_CHARS:
2143       end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2144       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2145       break;
2146     case GTK_DELETE_WORDS:
2147       if (count < 0)
2148         {
2149           /* Move to end of current word, or if not on a word, end of previous word */
2150           end_pos = gtk_entry_move_backward_word (entry, end_pos);
2151           end_pos = gtk_entry_move_forward_word (entry, end_pos);
2152         }
2153       else if (count > 0)
2154         {
2155           /* Move to beginning of current word, or if not on a word, begining of next word */
2156           start_pos = gtk_entry_move_forward_word (entry, start_pos);
2157           start_pos = gtk_entry_move_backward_word (entry, start_pos);
2158         }
2159         
2160       /* Fall through */
2161     case GTK_DELETE_WORD_ENDS:
2162       while (count < 0)
2163         {
2164           start_pos = gtk_entry_move_backward_word (entry, start_pos);
2165           count++;
2166         }
2167       while (count > 0)
2168         {
2169           end_pos = gtk_entry_move_forward_word (entry, end_pos);
2170           count--;
2171         }
2172       gtk_editable_delete_text (editable, start_pos, end_pos);
2173       break;
2174     case GTK_DELETE_DISPLAY_LINE_ENDS:
2175     case GTK_DELETE_PARAGRAPH_ENDS:
2176       if (count < 0)
2177         gtk_editable_delete_text (editable, 0, entry->current_pos);
2178       else
2179         gtk_editable_delete_text (editable, entry->current_pos, -1);
2180       break;
2181     case GTK_DELETE_DISPLAY_LINES:
2182     case GTK_DELETE_PARAGRAPHS:
2183       gtk_editable_delete_text (editable, 0, -1);  
2184       break;
2185     case GTK_DELETE_WHITESPACE:
2186       gtk_entry_delete_whitespace (entry);
2187       break;
2188     }
2189   
2190   gtk_entry_pend_cursor_blink (entry);
2191 }
2192
2193 static void
2194 gtk_entry_copy_clipboard (GtkEntry *entry)
2195 {
2196   GtkEditable *editable = GTK_EDITABLE (entry);
2197   gint start, end;
2198
2199   if (gtk_editable_get_selection_bounds (editable, &start, &end))
2200     {
2201       gchar *str = gtk_entry_get_public_chars (entry, start, end);
2202       gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), str, -1);
2203       g_free (str);
2204     }
2205 }
2206
2207 static void
2208 gtk_entry_cut_clipboard (GtkEntry *entry)
2209 {
2210   GtkEditable *editable = GTK_EDITABLE (entry);
2211   gint start, end;
2212
2213   gtk_entry_copy_clipboard (entry);
2214   if (gtk_editable_get_selection_bounds (editable, &start, &end))
2215     gtk_editable_delete_text (editable, start, end);
2216 }
2217
2218 static void
2219 gtk_entry_paste_clipboard (GtkEntry *entry)
2220 {
2221   gtk_entry_paste (entry, GDK_NONE);
2222 }
2223
2224 static void
2225 gtk_entry_toggle_overwrite (GtkEntry *entry)
2226 {
2227   entry->overwrite_mode = !entry->overwrite_mode;
2228 }
2229
2230 static void
2231 gtk_entry_select_all (GtkEntry *entry)
2232 {
2233   gtk_entry_select_line (entry);
2234 }
2235
2236 static void
2237 gtk_entry_real_activate (GtkEntry *entry)
2238 {
2239   GtkWindow *window;
2240   GtkWidget *toplevel;
2241   GtkWidget *widget;
2242
2243   widget = GTK_WIDGET (entry);
2244
2245   if (entry->activates_default)
2246     {
2247       toplevel = gtk_widget_get_toplevel (widget);
2248       if (GTK_IS_WINDOW (toplevel))
2249         {
2250           window = GTK_WINDOW (toplevel);
2251       
2252           if (window &&
2253               widget != window->default_widget &&
2254               !(!window->default_widget && widget == window->focus_widget))
2255             gtk_window_activate_default (window);
2256         }
2257     }
2258 }
2259
2260 static void
2261 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
2262                                     GtkEntry  *entry)
2263 {
2264   gtk_entry_queue_draw (entry);
2265 }
2266
2267 /* IM Context Callbacks
2268  */
2269
2270 static void
2271 gtk_entry_commit_cb (GtkIMContext *context,
2272                      const gchar  *str,
2273                      GtkEntry     *entry)
2274 {
2275   gtk_entry_enter_text (entry, str);
2276 }
2277
2278 static void 
2279 gtk_entry_preedit_changed_cb (GtkIMContext *context,
2280                               GtkEntry     *entry)
2281 {
2282   gchar *preedit_string;
2283   gint cursor_pos;
2284   
2285   gtk_im_context_get_preedit_string (entry->im_context,
2286                                      &preedit_string, NULL,
2287                                      &cursor_pos);
2288   entry->preedit_length = strlen (preedit_string);
2289   cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
2290   entry->preedit_cursor = cursor_pos;
2291   g_free (preedit_string);
2292
2293   gtk_entry_recompute (entry);
2294 }
2295
2296 static gboolean
2297 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
2298                                GtkEntry     *entry)
2299 {
2300   gtk_im_context_set_surrounding (context,
2301                                   entry->text,
2302                                   entry->n_bytes,
2303                                   g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
2304
2305   return TRUE;
2306 }
2307
2308 static gboolean
2309 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
2310                                  gint          offset,
2311                                  gint          n_chars,
2312                                  GtkEntry     *entry)
2313 {
2314   gtk_editable_delete_text (GTK_EDITABLE (entry),
2315                             entry->current_pos + offset,
2316                             entry->current_pos + offset + n_chars);
2317
2318   return TRUE;
2319 }
2320
2321 /* Internal functions
2322  */
2323
2324 /* Used for im_commit_cb and inserting Unicode chars */
2325 static void
2326 gtk_entry_enter_text (GtkEntry       *entry,
2327                       const gchar    *str)
2328 {
2329   GtkEditable *editable = GTK_EDITABLE (entry);
2330   gint tmp_pos = entry->current_pos;
2331
2332   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
2333     gtk_editable_delete_selection (editable);
2334   else
2335     {
2336       if (entry->overwrite_mode)
2337         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
2338     }
2339
2340   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
2341   gtk_editable_set_position (editable, tmp_pos);
2342 }
2343
2344 /* All changes to entry->current_pos and entry->selection_bound
2345  * should go through this function.
2346  */
2347 static void
2348 gtk_entry_set_positions (GtkEntry *entry,
2349                          gint      current_pos,
2350                          gint      selection_bound)
2351 {
2352   gboolean changed = FALSE;
2353
2354   g_object_freeze_notify (G_OBJECT (entry));
2355   
2356   if (current_pos != -1 &&
2357       entry->current_pos != current_pos)
2358     {
2359       entry->current_pos = current_pos;
2360       changed = TRUE;
2361
2362       g_object_notify (G_OBJECT (entry), "cursor_position");
2363     }
2364
2365   if (selection_bound != -1 &&
2366       entry->selection_bound != selection_bound)
2367     {
2368       entry->selection_bound = selection_bound;
2369       changed = TRUE;
2370       
2371       g_object_notify (G_OBJECT (entry), "selection_bound");
2372     }
2373
2374   g_object_thaw_notify (G_OBJECT (entry));
2375
2376   if (changed)
2377     gtk_entry_recompute (entry);
2378 }
2379
2380 static void
2381 gtk_entry_reset_layout (GtkEntry *entry)
2382 {
2383   if (entry->cached_layout)
2384     {
2385       g_object_unref (G_OBJECT (entry->cached_layout));
2386       entry->cached_layout = NULL;
2387     }
2388 }
2389
2390 static void
2391 update_im_cursor_location (GtkEntry *entry)
2392 {
2393   GdkRectangle area;
2394   gint strong_x;
2395   gint strong_xoffset;
2396   gint x, y, area_width, area_height;
2397
2398   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
2399 ;
2400   get_text_area_size (entry, &x, &y, &area_width, &area_height);
2401
2402   strong_xoffset = strong_x - entry->scroll_offset;
2403   if (strong_xoffset < 0)
2404     {
2405       strong_xoffset = 0;
2406     }
2407   else if (strong_xoffset > area_width)
2408     {
2409       strong_xoffset = area_width;
2410     }
2411   area.x = x + strong_xoffset;
2412   area.y = y + area_height;
2413   area.width = area_width;
2414   area.height = area_height;
2415
2416   gtk_im_context_set_cursor_location (entry->im_context, &area);
2417 }
2418
2419 static gboolean
2420 recompute_idle_func (gpointer data)
2421 {
2422   GtkEntry *entry;
2423
2424   GDK_THREADS_ENTER ();
2425
2426   entry = GTK_ENTRY (data);
2427
2428   gtk_entry_adjust_scroll (entry);
2429   gtk_entry_queue_draw (entry);
2430
2431   entry->recompute_idle = FALSE;
2432   
2433   update_im_cursor_location (entry);
2434
2435   GDK_THREADS_LEAVE ();
2436
2437   return FALSE;
2438 }
2439
2440 static void
2441 gtk_entry_recompute (GtkEntry *entry)
2442 {
2443   gtk_entry_reset_layout (entry);
2444   gtk_entry_check_cursor_blink (entry);
2445   
2446   if (!entry->recompute_idle)
2447     {
2448       entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
2449                                                recompute_idle_func, entry, NULL); 
2450     }
2451 }
2452
2453 static void
2454 append_char (GString *str,
2455              gunichar ch,
2456              gint     count)
2457 {
2458   gint i;
2459   gint char_len;
2460   gchar buf[7];
2461   
2462   char_len = g_unichar_to_utf8 (ch, buf);
2463   
2464   i = 0;
2465   while (i < count)
2466     {
2467       g_string_append_len (str, buf, char_len);
2468       ++i;
2469     }
2470 }
2471      
2472 static PangoLayout *
2473 gtk_entry_create_layout (GtkEntry *entry,
2474                          gboolean  include_preedit)
2475 {
2476   PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
2477   PangoAttrList *tmp_attrs = pango_attr_list_new ();
2478   
2479   gchar *preedit_string = NULL;
2480   gint preedit_length = 0;
2481   PangoAttrList *preedit_attrs = NULL;
2482
2483   pango_layout_set_single_paragraph_mode (layout, TRUE);
2484   
2485   if (include_preedit)
2486     {
2487       gtk_im_context_get_preedit_string (entry->im_context,
2488                                          &preedit_string, &preedit_attrs, NULL);
2489       preedit_length = entry->preedit_length;
2490     }
2491
2492   if (preedit_length)
2493     {
2494       GString *tmp_string = g_string_new (NULL);
2495       
2496       gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
2497       
2498       if (entry->visible)
2499         {
2500           g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
2501           g_string_insert (tmp_string, cursor_index, preedit_string);
2502         }
2503       else
2504         {
2505           gint ch_len;
2506           gint preedit_len_chars;
2507           gunichar invisible_char;
2508           
2509           ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
2510           preedit_len_chars = g_utf8_strlen (preedit_string, -1);
2511           ch_len += preedit_len_chars;
2512
2513           if (entry->invisible_char != 0)
2514             invisible_char = entry->invisible_char;
2515           else
2516             invisible_char = ' '; /* just pick a char */
2517           
2518           append_char (tmp_string, invisible_char, ch_len);
2519           
2520           /* Fix cursor index to point to invisible char corresponding
2521            * to the preedit, fix preedit_length to be the length of
2522            * the invisible chars representing the preedit
2523            */
2524           cursor_index =
2525             g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
2526             tmp_string->str;
2527           preedit_length =
2528             preedit_len_chars *
2529             g_unichar_to_utf8 (invisible_char, NULL);
2530         }
2531       
2532       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
2533       
2534       pango_attr_list_splice (tmp_attrs, preedit_attrs,
2535                               cursor_index, preedit_length);
2536       
2537       g_string_free (tmp_string, TRUE);
2538     }
2539   else
2540     {
2541       if (entry->visible)
2542         {
2543           pango_layout_set_text (layout, entry->text, entry->n_bytes);
2544         }
2545       else
2546         {
2547           GString *str = g_string_new (NULL);
2548           gunichar invisible_char;
2549           
2550           if (entry->invisible_char != 0)
2551             invisible_char = entry->invisible_char;
2552           else
2553             invisible_char = ' '; /* just pick a char */
2554           
2555           append_char (str, invisible_char, entry->text_length);
2556           pango_layout_set_text (layout, str->str, str->len);
2557           g_string_free (str, TRUE);
2558         }
2559     }
2560       
2561   pango_layout_set_attributes (layout, tmp_attrs);
2562
2563   if (preedit_string)
2564     g_free (preedit_string);
2565   if (preedit_attrs)
2566     pango_attr_list_unref (preedit_attrs);
2567       
2568   pango_attr_list_unref (tmp_attrs);
2569
2570   return layout;
2571 }
2572
2573 static PangoLayout *
2574 gtk_entry_ensure_layout (GtkEntry *entry,
2575                          gboolean  include_preedit)
2576 {
2577   if (entry->preedit_length > 0 &&
2578       !include_preedit != !entry->cache_includes_preedit)
2579     gtk_entry_reset_layout (entry);
2580
2581   if (!entry->cached_layout)
2582     {
2583       entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
2584       entry->cache_includes_preedit = include_preedit;
2585     }
2586   
2587   return entry->cached_layout;
2588 }
2589
2590 static void
2591 get_layout_position (GtkEntry *entry,
2592                      gint     *x,
2593                      gint     *y)
2594 {
2595   PangoLayout *layout;
2596   PangoRectangle logical_rect;
2597   gint area_width, area_height;
2598   gint y_pos;
2599   PangoLayoutLine *line;
2600   
2601   layout = gtk_entry_ensure_layout (entry, TRUE);
2602
2603   get_text_area_size (entry, NULL, NULL, &area_width, &area_height);      
2604       
2605   area_height = PANGO_SCALE * (area_height - 2 * INNER_BORDER);
2606   
2607   line = pango_layout_get_lines (layout)->data;
2608   pango_layout_line_get_extents (line, NULL, &logical_rect);
2609   
2610   /* Align primarily for locale's ascent/descent */
2611   y_pos = ((area_height - entry->ascent - entry->descent) / 2 + 
2612            entry->ascent + logical_rect.y);
2613   
2614   /* Now see if we need to adjust to fit in actual drawn string */
2615   if (logical_rect.height > area_height)
2616     y_pos = (area_height - logical_rect.height) / 2;
2617   else if (y_pos < 0)
2618     y_pos = 0;
2619   else if (y_pos + logical_rect.height > area_height)
2620     y_pos = area_height - logical_rect.height;
2621   
2622   y_pos = INNER_BORDER + y_pos / PANGO_SCALE;
2623
2624   if (x)
2625     *x = INNER_BORDER - entry->scroll_offset;
2626
2627   if (y)
2628     *y = y_pos;
2629 }
2630
2631 static void
2632 gtk_entry_draw_text (GtkEntry *entry)
2633 {
2634   GtkWidget *widget;
2635   PangoLayoutLine *line;
2636   
2637   if (!entry->visible && entry->invisible_char == 0)
2638     return;
2639   
2640   if (GTK_WIDGET_DRAWABLE (entry))
2641     {
2642       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2643       gint area_width, area_height;
2644       gint x, y;
2645       gint start_pos, end_pos;
2646       
2647       widget = GTK_WIDGET (entry);
2648       
2649       get_layout_position (entry, &x, &y);
2650
2651       get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
2652
2653       gtk_paint_flat_box (widget->style, entry->text_area, 
2654                           GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
2655                           NULL, widget, "entry_bg", 
2656                           0, 0, area_width, area_height);
2657
2658       gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],       
2659                        x, y,
2660                        layout);
2661       
2662       if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
2663         {
2664           gint *ranges;
2665           gint n_ranges, i;
2666           PangoRectangle logical_rect;
2667           gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2668           gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2669           GdkRegion *clip_region = gdk_region_new ();
2670           GdkGC *text_gc;
2671           GdkGC *selection_gc;
2672
2673           line = pango_layout_get_lines (layout)->data;
2674           
2675           pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
2676
2677           pango_layout_get_extents (layout, NULL, &logical_rect);
2678           
2679           if (GTK_WIDGET_HAS_FOCUS (entry))
2680             {
2681               selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
2682               text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
2683             }
2684           else
2685             {
2686               selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
2687               text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
2688             }
2689           
2690           for (i=0; i < n_ranges; i++)
2691             {
2692               GdkRectangle rect;
2693
2694               rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
2695               rect.y = y;
2696               rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
2697               rect.height = logical_rect.height / PANGO_SCALE;
2698                 
2699               gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
2700                                   rect.x, rect.y, rect.width, rect.height);
2701
2702               gdk_region_union_with_rect (clip_region, &rect);
2703             }
2704
2705           gdk_gc_set_clip_region (text_gc, clip_region);
2706           gdk_draw_layout (entry->text_area, text_gc, 
2707                            x, y,
2708                            layout);
2709           gdk_gc_set_clip_region (text_gc, NULL);
2710           
2711           gdk_region_destroy (clip_region);
2712           g_free (ranges);
2713         }
2714     }
2715 }
2716
2717 static void
2718 gtk_entry_draw_cursor (GtkEntry  *entry,
2719                        CursorType type)
2720 {
2721   GtkTextDirection keymap_direction =
2722     (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
2723     GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
2724   GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
2725   
2726   if (GTK_WIDGET_DRAWABLE (entry))
2727     {
2728       GtkWidget *widget = GTK_WIDGET (entry);
2729       GdkRectangle cursor_location;
2730       gboolean split_cursor;
2731
2732       gint xoffset = INNER_BORDER - entry->scroll_offset;
2733       gint strong_x, weak_x;
2734       gint text_area_height;
2735       GdkGC *gc1 = NULL;
2736       GdkGC *gc2 = NULL;
2737       GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
2738       GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
2739       gint x1 = 0;
2740       gint x2 = 0;
2741
2742       gdk_window_get_size (entry->text_area, NULL, &text_area_height);
2743       
2744       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
2745
2746       g_object_get (gtk_widget_get_settings (widget),
2747                     "gtk-split-cursor", &split_cursor,
2748                     NULL);
2749
2750       if (split_cursor)
2751         {
2752           gc1 = entry->cursor_gc;
2753           x1 = strong_x;
2754
2755           if (weak_x != strong_x)
2756             {
2757               dir1 = widget_direction;
2758               dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
2759               
2760               gc2 = widget->style->text_gc[GTK_STATE_NORMAL];
2761               x2 = weak_x;
2762             }
2763         }
2764       else
2765         {
2766           gc1 = entry->cursor_gc;
2767           
2768           if (keymap_direction == widget_direction)
2769             x1 = strong_x;
2770           else
2771             x1 = weak_x;
2772         }
2773
2774       cursor_location.x = xoffset + x1;
2775       cursor_location.y = INNER_BORDER;
2776       cursor_location.width = 0;
2777       cursor_location.height = text_area_height - 2 * INNER_BORDER ;
2778       
2779       _gtk_draw_insertion_cursor (entry->text_area, gc1,
2780                                   &cursor_location, dir1);
2781       
2782       if (gc2)
2783         {
2784           cursor_location.x = xoffset + x2;
2785           _gtk_draw_insertion_cursor (entry->text_area, gc2,
2786                                       &cursor_location, dir2);
2787         }
2788     }
2789 }
2790
2791 static void
2792 gtk_entry_queue_draw (GtkEntry *entry)
2793 {
2794   if (GTK_WIDGET_REALIZED (entry))
2795     gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
2796 }
2797
2798 static void
2799 gtk_entry_reset_im_context (GtkEntry *entry)
2800 {
2801   if (entry->need_im_reset)
2802     {
2803       entry->need_im_reset = 0;
2804       gtk_im_context_reset (entry->im_context);
2805     }
2806 }
2807
2808 static gint
2809 gtk_entry_find_position (GtkEntry *entry,
2810                          gint      x)
2811 {
2812   PangoLayout *layout;
2813   PangoLayoutLine *line;
2814   gint index;
2815   gint pos;
2816   gboolean trailing;
2817   gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
2818   
2819   layout = gtk_entry_ensure_layout (entry, TRUE);
2820   
2821   line = pango_layout_get_lines (layout)->data;
2822   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
2823
2824   if (index >= cursor_index && entry->preedit_length)
2825     {
2826       if (index >= cursor_index + entry->preedit_length)
2827         index -= entry->preedit_length;
2828       else
2829         {
2830           index = cursor_index;
2831           trailing = 0;
2832         }
2833     }
2834
2835   pos = g_utf8_pointer_to_offset (entry->text, entry->text + index);
2836   pos += trailing;
2837
2838   return pos;
2839 }
2840
2841 static void
2842 gtk_entry_get_cursor_locations (GtkEntry   *entry,
2843                                 CursorType  type,
2844                                 gint       *strong_x,
2845                                 gint       *weak_x)
2846 {
2847   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2848   const gchar *text;
2849   PangoRectangle strong_pos, weak_pos;
2850   gint index;
2851   
2852   if (type == CURSOR_STANDARD)
2853     {
2854       text = pango_layout_get_text (layout);
2855       index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
2856     }
2857   else /* type == CURSOR_DND */
2858     {
2859       index = g_utf8_offset_to_pointer (entry->text, entry->dnd_position) - entry->text;
2860       if (entry->dnd_position > entry->current_pos)
2861         index += entry->preedit_length;
2862     }
2863       
2864   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2865
2866   if (strong_x)
2867     *strong_x = strong_pos.x / PANGO_SCALE;
2868
2869   if (weak_x)
2870     *weak_x = weak_pos.x / PANGO_SCALE;
2871 }
2872
2873 static void
2874 gtk_entry_adjust_scroll (GtkEntry *entry)
2875 {
2876   gint min_offset, max_offset;
2877   gint text_area_width;
2878   gint strong_x, weak_x;
2879   gint strong_xoffset, weak_xoffset;
2880   PangoLayout *layout;
2881   PangoLayoutLine *line;
2882   PangoRectangle logical_rect;
2883
2884   if (!GTK_WIDGET_REALIZED (entry))
2885     return;
2886   
2887   gdk_window_get_size (entry->text_area, &text_area_width, NULL);
2888   text_area_width -= 2 * INNER_BORDER;
2889
2890   layout = gtk_entry_ensure_layout (entry, TRUE);
2891   line = pango_layout_get_lines (layout)->data;
2892
2893   pango_layout_line_get_extents (line, NULL, &logical_rect);
2894
2895   /* Display as much text as we can */
2896
2897   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
2898     {
2899       min_offset = 0;
2900       max_offset = MAX (min_offset, logical_rect.width / PANGO_SCALE - text_area_width);
2901     }
2902   else
2903     {
2904       max_offset = logical_rect.width / PANGO_SCALE - text_area_width;
2905       min_offset = MIN (0, max_offset);
2906     }
2907
2908   entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
2909
2910   /* And make sure cursors are on screen. Note that the cursor is
2911    * actually drawn one pixel into the INNER_BORDER space on
2912    * the right, when the scroll is at the utmost right. This
2913    * looks better to to me than confining the cursor inside the
2914    * border entirely, though it means that the cursor gets one
2915    * pixel closer to the the edge of the widget on the right than
2916    * on the left. This might need changing if one changed
2917    * INNER_BORDER from 2 to 1, as one would do on a
2918    * small-screen-real-estate display.
2919    *
2920    * We always make sure that the strong cursor is on screen, and
2921    * put the weak cursor on screen if possible.
2922    */
2923
2924   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
2925   
2926   strong_xoffset = strong_x - entry->scroll_offset;
2927
2928   if (strong_xoffset < 0)
2929     {
2930       entry->scroll_offset += strong_xoffset;
2931       strong_xoffset = 0;
2932     }
2933   else if (strong_xoffset > text_area_width)
2934     {
2935       entry->scroll_offset += strong_xoffset - text_area_width;
2936       strong_xoffset = text_area_width;
2937     }
2938
2939   weak_xoffset = weak_x - entry->scroll_offset;
2940
2941   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
2942     {
2943       entry->scroll_offset += weak_xoffset;
2944     }
2945   else if (weak_xoffset > text_area_width &&
2946            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
2947     {
2948       entry->scroll_offset += weak_xoffset - text_area_width;
2949     }
2950
2951   g_object_notify (G_OBJECT (entry), "scroll_offset");
2952 }
2953
2954 static gint
2955 gtk_entry_move_visually (GtkEntry *entry,
2956                          gint      start,
2957                          gint      count)
2958 {
2959   gint index;
2960   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2961   const gchar *text;
2962
2963   text = pango_layout_get_text (layout);
2964   
2965   index = g_utf8_offset_to_pointer (text, start) - text;
2966
2967   while (count != 0)
2968     {
2969       int new_index, new_trailing;
2970       gboolean split_cursor;
2971       gboolean strong;
2972
2973       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2974                     "gtk-split-cursor", &split_cursor,
2975                     NULL);
2976
2977       if (split_cursor)
2978         strong = TRUE;
2979       else
2980         {
2981           GtkTextDirection keymap_direction =
2982             (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
2983             GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
2984
2985           strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
2986         }
2987       
2988       if (count > 0)
2989         {
2990           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
2991           count--;
2992         }
2993       else
2994         {
2995           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
2996           count++;
2997         }
2998
2999       if (new_index < 0 || new_index == G_MAXINT)
3000         break;
3001
3002       index = new_index;
3003       
3004       while (new_trailing--)
3005         index = g_utf8_next_char (entry->text + new_index) - entry->text;
3006     }
3007   
3008   return g_utf8_pointer_to_offset (text, text + index);
3009 }
3010
3011 static gint
3012 gtk_entry_move_logically (GtkEntry *entry,
3013                           gint      start,
3014                           gint      count)
3015 {
3016   gint new_pos = start;
3017
3018   /* Prevent any leak of information */
3019   if (!entry->visible)
3020     {
3021       new_pos = CLAMP (start + count, 0, entry->text_length);
3022     }
3023   else if (entry->text)
3024     {
3025       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3026       PangoLogAttr *log_attrs;
3027       gint n_attrs;
3028
3029       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3030
3031       while (count > 0 && new_pos < entry->text_length)
3032         {
3033           do
3034             new_pos++;
3035           while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3036           
3037           count--;
3038         }
3039       while (count < 0 && new_pos > 0)
3040         {
3041           do
3042             new_pos--;
3043           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3044           
3045           count++;
3046         }
3047       
3048       g_free (log_attrs);
3049     }
3050
3051   return new_pos;
3052 }
3053
3054 static gint
3055 gtk_entry_move_forward_word (GtkEntry *entry,
3056                              gint      start)
3057 {
3058   gint new_pos = start;
3059
3060   /* Prevent any leak of information */
3061   if (!entry->visible)
3062     {
3063       new_pos = entry->text_length;
3064     }
3065   else if (entry->text && (new_pos < entry->text_length))
3066     {
3067       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3068       PangoLogAttr *log_attrs;
3069       gint n_attrs;
3070
3071       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3072       
3073       /* Find the next word end */
3074       new_pos++;
3075       while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
3076         new_pos++;
3077
3078       g_free (log_attrs);
3079     }
3080
3081   return new_pos;
3082 }
3083
3084
3085 static gint
3086 gtk_entry_move_backward_word (GtkEntry *entry,
3087                               gint      start)
3088 {
3089   gint new_pos = start;
3090
3091   /* Prevent any leak of information */
3092   if (!entry->visible)
3093     {
3094       new_pos = 0;
3095     }
3096   else if (entry->text && start > 0)
3097     {
3098       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3099       PangoLogAttr *log_attrs;
3100       gint n_attrs;
3101
3102       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3103
3104       new_pos = start - 1;
3105
3106       /* Find the previous word beginning */
3107       while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
3108         new_pos--;
3109
3110       g_free (log_attrs);
3111     }
3112
3113   return new_pos;
3114 }
3115
3116 static void
3117 gtk_entry_delete_whitespace (GtkEntry *entry)
3118 {
3119   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3120   PangoLogAttr *log_attrs;
3121   gint n_attrs;
3122   gint start, end;
3123
3124   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3125
3126   start = end = entry->current_pos;
3127   
3128   while (start > 0 && log_attrs[start-1].is_white)
3129     start--;
3130
3131   while (end < n_attrs && log_attrs[start-1].is_white)
3132     end++;
3133
3134   g_free (log_attrs);
3135
3136   if (start != end)
3137     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
3138 }
3139
3140
3141 static void
3142 gtk_entry_select_word (GtkEntry *entry)
3143 {
3144   gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos);
3145   gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos);
3146
3147   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
3148 }
3149
3150 static void
3151 gtk_entry_select_line (GtkEntry *entry)
3152 {
3153   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
3154 }
3155
3156 /*
3157  * Like gtk_editable_get_chars, but if the editable is not
3158  * visible, return asterisks; also convert result to UTF-8.
3159  */
3160 static char *    
3161 gtk_entry_get_public_chars (GtkEntry *entry,
3162                             gint      start,
3163                             gint      end)
3164 {
3165   if (end < 0)
3166     end = entry->text_length;
3167   
3168   if (entry->visible)
3169     return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
3170   else
3171     {
3172       gchar *str;
3173       gint i;
3174       gint n_chars = end - start;
3175        
3176       str = g_malloc (n_chars + 1);
3177       for (i = 0; i < n_chars; i++)
3178         str[i] = '*';
3179       str[i] = '\0';
3180       
3181       return str;
3182     }
3183
3184 }
3185
3186 static void
3187 paste_received (GtkClipboard *clipboard,
3188                 const gchar  *text,
3189                 gpointer      data)
3190 {
3191   GtkEntry *entry = GTK_ENTRY (data);
3192   GtkEditable *editable = GTK_EDITABLE (entry);
3193       
3194   if (text)
3195     {
3196       gint pos, start, end;
3197       
3198       if (gtk_editable_get_selection_bounds (editable, &start, &end))
3199         gtk_editable_delete_text (editable, start, end);
3200
3201       pos = entry->current_pos;
3202       gtk_editable_insert_text (editable, text, -1, &pos);
3203       gtk_editable_set_position (editable, pos);
3204     }
3205
3206   g_object_unref (G_OBJECT (entry));
3207 }
3208
3209 static void
3210 gtk_entry_paste (GtkEntry *entry,
3211                  GdkAtom   selection)
3212 {
3213   g_object_ref (G_OBJECT (entry));
3214   gtk_clipboard_request_text (gtk_clipboard_get (selection),
3215                               paste_received, entry);
3216 }
3217
3218 static void
3219 primary_get_cb (GtkClipboard     *clipboard,
3220                 GtkSelectionData *selection_data,
3221                 guint             info,
3222                 gpointer          data)
3223 {
3224   GtkEntry *entry = GTK_ENTRY (data);
3225   gint start, end;
3226   
3227   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
3228     {
3229       gchar *str = gtk_entry_get_public_chars (entry, start, end);
3230       gtk_selection_data_set_text (selection_data, str, -1);
3231       g_free (str);
3232     }
3233 }
3234
3235 static void
3236 primary_clear_cb (GtkClipboard *clipboard,
3237                   gpointer      data)
3238 {
3239   GtkEntry *entry = GTK_ENTRY (data);
3240
3241   gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
3242 }
3243
3244 static void
3245 gtk_entry_update_primary_selection (GtkEntry *entry)
3246 {
3247   static const GtkTargetEntry targets[] = {
3248     { "UTF8_STRING", 0, 0 },
3249     { "STRING", 0, 0 },
3250     { "TEXT",   0, 0 }, 
3251     { "COMPOUND_TEXT", 0, 0 }
3252   };
3253   
3254   GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
3255   gint start, end;
3256   
3257   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
3258     {
3259       if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3260                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
3261         primary_clear_cb (clipboard, entry);
3262     }
3263   else
3264     {
3265       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
3266         gtk_clipboard_clear (clipboard);
3267     }
3268 }
3269
3270 /* Public API
3271  */
3272
3273 GtkWidget*
3274 gtk_entry_new (void)
3275 {
3276   return GTK_WIDGET (gtk_type_new (GTK_TYPE_ENTRY));
3277 }
3278
3279 GtkWidget*
3280 gtk_entry_new_with_max_length (gint max)
3281 {
3282   GtkEntry *entry;
3283
3284   entry = gtk_type_new (GTK_TYPE_ENTRY);
3285   entry->text_max_length = max;
3286
3287   return GTK_WIDGET (entry);
3288 }
3289
3290 void
3291 gtk_entry_set_text (GtkEntry *entry,
3292                     const gchar *text)
3293 {
3294   gint tmp_pos;
3295
3296   g_return_if_fail (GTK_IS_ENTRY (entry));
3297   g_return_if_fail (text != NULL);
3298   
3299   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
3300
3301   tmp_pos = 0;
3302   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
3303 }
3304
3305 void
3306 gtk_entry_append_text (GtkEntry *entry,
3307                        const gchar *text)
3308 {
3309   gint tmp_pos;
3310
3311   g_return_if_fail (GTK_IS_ENTRY (entry));
3312   g_return_if_fail (text != NULL);
3313
3314   tmp_pos = entry->text_length;
3315   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
3316 }
3317
3318 void
3319 gtk_entry_prepend_text (GtkEntry *entry,
3320                         const gchar *text)
3321 {
3322   gint tmp_pos;
3323
3324   g_return_if_fail (GTK_IS_ENTRY (entry));
3325   g_return_if_fail (text != NULL);
3326
3327   tmp_pos = 0;
3328   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
3329 }
3330
3331 void
3332 gtk_entry_set_position (GtkEntry *entry,
3333                         gint       position)
3334 {
3335   g_return_if_fail (GTK_IS_ENTRY (entry));
3336
3337   gtk_editable_set_position (GTK_EDITABLE (entry), position);
3338 }
3339
3340 void
3341 gtk_entry_set_visibility (GtkEntry *entry,
3342                           gboolean visible)
3343 {
3344   g_return_if_fail (GTK_IS_ENTRY (entry));
3345
3346   entry->visible = visible ? TRUE : FALSE;
3347   g_object_notify (G_OBJECT (entry), "visibility");
3348   gtk_entry_recompute (entry);
3349 }
3350
3351 /**
3352  * gtk_entry_get_visibility:
3353  * @entry: a #GtkEntry
3354  *
3355  * Retrieves whether the text in @entry is visible. See
3356  * gtk_entry_set_visibility().
3357  *
3358  * Return value: %TRUE if the text is currently visible
3359  **/
3360 gboolean
3361 gtk_entry_get_visibility (GtkEntry *entry)
3362 {
3363   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3364
3365   return entry->visible;
3366 }
3367
3368 /**
3369  * gtk_entry_set_invisible_char:
3370  * @entry: a #GtkEntry
3371  * @ch: a Unicode character
3372  * 
3373  * Sets the character to use in place of the actual text when
3374  * gtk_entry_set_visibility() has been called to set text visibility
3375  * to %FALSE. i.e. this is the character used in "password mode" to
3376  * show the user how many characters have been typed. The default
3377  * invisible char is an asterisk ('*').  If you set the invisible char
3378  * to 0, then the user will get no feedback at all; there will be
3379  * no text on the screen as they type.
3380  * 
3381  **/
3382 void
3383 gtk_entry_set_invisible_char (GtkEntry *entry,
3384                               gunichar  ch)
3385 {
3386   g_return_if_fail (GTK_IS_ENTRY (entry));
3387
3388   if (ch == entry->invisible_char)
3389     return;
3390
3391   entry->invisible_char = ch;
3392   g_object_notify (G_OBJECT (entry), "invisible_char");
3393   gtk_entry_recompute (entry);  
3394 }
3395
3396 /**
3397  * gtk_entry_get_invisible_char:
3398  * @entry: a #GtkEntry
3399  *
3400  * Retrieves the character displayed in place of the real characters
3401  * for entries with visisbility set to false. See gtk_entry_set_invisible_char().
3402  *
3403  * Return value: the current invisible char, or 0, if the entry does not
3404  *               show invisible text at all. 
3405  **/
3406 gunichar
3407 gtk_entry_get_invisible_char (GtkEntry *entry)
3408 {
3409   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3410
3411   return entry->invisible_char;
3412 }
3413
3414 void
3415 gtk_entry_set_editable (GtkEntry *entry,
3416                         gboolean  editable)
3417 {
3418   g_return_if_fail (GTK_IS_ENTRY (entry));
3419
3420   gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
3421 }
3422
3423 G_CONST_RETURN gchar*
3424 gtk_entry_get_text (GtkEntry *entry)
3425 {
3426   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
3427
3428   return entry->text;
3429 }
3430
3431 void       
3432 gtk_entry_select_region  (GtkEntry       *entry,
3433                           gint            start,
3434                           gint            end)
3435 {
3436   gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
3437 }
3438
3439 void
3440 gtk_entry_set_max_length (GtkEntry     *entry,
3441                           gint          max)
3442 {
3443   g_return_if_fail (GTK_IS_ENTRY (entry));
3444
3445   if (max > 0 && entry->text_length > max)
3446     gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
3447   
3448   entry->text_max_length = max;
3449   g_object_notify (G_OBJECT (entry), "max_length");
3450 }
3451
3452 /**
3453  * gtk_entry_get_max_length:
3454  * @entry: a #GtkEntry
3455  *
3456  * Retrieves the maximum allowed length of the text in
3457  * @entry. See gtk_entry_set_max_length().
3458  *
3459  * Return value: the maximum allowed number of characters
3460  *               in #GtkEntry, or 0 if there is no maximum.
3461  **/
3462 gint
3463 gtk_entry_get_max_length (GtkEntry *entry)
3464 {
3465   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3466
3467   return entry->text_max_length;
3468 }
3469
3470 /**
3471  * gtk_entry_set_activates_default:
3472  * @entry: a #GtkEntry
3473  * @setting: %TRUE to activate window's default widget on Enter keypress
3474  *
3475  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
3476  * widget for the window containing the entry. This usually means that
3477  * the dialog box containing the entry will be closed, since the default
3478  * widget is usually one of the dialog buttons.
3479  *
3480  * (For experts: if @setting is %TRUE, the entry calls
3481  * gtk_window_activate_default() on the window containing the entry, in
3482  * the default handler for the "activate" signal.)
3483  * 
3484  **/
3485 void
3486 gtk_entry_set_activates_default (GtkEntry *entry,
3487                                  gboolean  setting)
3488 {
3489   g_return_if_fail (GTK_IS_ENTRY (entry));
3490   setting = setting != FALSE;
3491
3492   if (setting != entry->activates_default)
3493     {
3494       entry->activates_default = setting;
3495       g_object_notify (G_OBJECT (entry), "activates_default");
3496     }
3497 }
3498
3499 /**
3500  * gtk_entry_get_activates_default:
3501  * @entry: a #GtkEntry
3502  * 
3503  * Retrieves the value set by gtk_entry_set_activates_default().
3504  * 
3505  * Return value: %TRUE if the entry will activate the default widget
3506  **/
3507 gboolean
3508 gtk_entry_get_activates_default (GtkEntry *entry)
3509 {
3510   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3511
3512   return entry->activates_default;
3513 }
3514
3515 /**
3516  * gtk_entry_set_width_chars:
3517  * @entry: a #GtkEntry
3518  * @n_chars: width in chars
3519  *
3520  * Changes the size request of the entry to be about the right size
3521  * for @n_chars characters. Note that it changes the size
3522  * <emphasis>request</emphasis>, the size can still be affected by
3523  * how you pack the widget into containers. If @n_chars is -1, the
3524  * size reverts to the default entry size.
3525  * 
3526  **/
3527 void
3528 gtk_entry_set_width_chars (GtkEntry *entry,
3529                            gint      n_chars)
3530 {
3531   g_return_if_fail (GTK_IS_ENTRY (entry));
3532
3533   if (entry->width_chars != n_chars)
3534     {
3535       entry->width_chars = n_chars;
3536       g_object_notify (G_OBJECT (entry), "width_chars");
3537       gtk_widget_queue_resize (GTK_WIDGET (entry));
3538     }
3539 }
3540
3541 /**
3542  * gtk_entry_get_width_chars:
3543  * @entry: a #GtkEntry
3544  * 
3545  * Gets the value set by gtk_entry_set_width_chars().
3546  * 
3547  * Return value: number of chars to request space for, or negative if unset
3548  **/
3549 gint
3550 gtk_entry_get_width_chars (GtkEntry *entry)
3551 {
3552   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3553
3554   return entry->width_chars;
3555 }
3556
3557 /**
3558  * gtk_entry_set_has_frame:
3559  * @entry: a #GtkEntry
3560  * @setting: new value
3561  * 
3562  * Sets whether the entry has a beveled frame around it.
3563  **/
3564 void
3565 gtk_entry_set_has_frame (GtkEntry *entry,
3566                          gboolean  setting)
3567 {
3568   g_return_if_fail (GTK_IS_ENTRY (entry));
3569
3570   setting = (setting != FALSE);
3571
3572   if (entry->has_frame == setting)
3573     return;
3574
3575   gtk_widget_queue_resize (GTK_WIDGET (entry));
3576   entry->has_frame = setting;
3577   g_object_notify (G_OBJECT (entry), "has_frame");
3578 }
3579
3580 /**
3581  * gtk_entry_get_has_frame:
3582  * @entry: a #GtkEntry
3583  * 
3584  * Gets the value set by gtk_entry_set_has_frame().
3585  * 
3586  * Return value: whether the entry has a beveled frame
3587  **/
3588 gboolean
3589 gtk_entry_get_has_frame (GtkEntry *entry)
3590 {
3591   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3592
3593   return entry->has_frame;
3594 }
3595
3596
3597 /**
3598  * gtk_entry_get_layout:
3599  * @entry: a #GtkEntry
3600  * 
3601  * Gets the #PangoLayout used to display the entry.
3602  * The layout is useful to e.g. convert text positions to
3603  * pixel positions, in combination with gtk_entry_get_layout_offsets().
3604  * The returned layout is owned by the entry so need not be
3605  * freed by the caller.
3606  *
3607  * Keep in mind that the layout text may contain a preedit string, so
3608  * gtk_entry_layout_index_to_text_index() and
3609  * gtk_entry_text_index_to_layout_index() are needed to convert byte
3610  * indices in the layout to byte indices in the entry contents.
3611  * 
3612  * Return value: the #PangoLayout for this entry
3613  **/
3614 PangoLayout*
3615 gtk_entry_get_layout (GtkEntry *entry)
3616 {
3617   PangoLayout *layout;
3618   
3619   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
3620
3621   layout = gtk_entry_ensure_layout (entry, TRUE);
3622
3623   return layout;
3624 }
3625
3626
3627 /**
3628  * gtk_entry_layout_index_to_text_index:
3629  * @entry: a #GtkEntry
3630  * @layout_index: byte index into the entry layout text
3631  * 
3632  * Converts from a position in the entry contents (returned
3633  * by gtk_entry_get_text()) to a position in the
3634  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
3635  * with text retrieved via pango_layout_get_text()).
3636  * 
3637  * Return value: byte index into the entry contents
3638  **/
3639 gint
3640 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
3641                                       gint      layout_index)
3642 {
3643   gint cursor_index;
3644   
3645   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3646
3647   cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3648   
3649   if (layout_index >= cursor_index && entry->preedit_length)
3650     {
3651       if (layout_index >= cursor_index + entry->preedit_length)
3652         layout_index -= entry->preedit_length;
3653       else
3654         layout_index = cursor_index;
3655     }
3656
3657   return layout_index;
3658 }
3659
3660 /**
3661  * gtk_entry_text_index_to_layout_index:
3662  * @entry: a #GtkEntry
3663  * @text_index: byte index into the entry contents
3664  * 
3665  * Converts from a position in the entry's #PangoLayout(returned by
3666  * gtk_entry_get_layout()) to a position in the entry contents
3667  * (returned by gtk_entry_get_text()).
3668  * 
3669  * Return value: byte index into the entry layout text
3670  **/
3671 gint
3672 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
3673                                       gint      text_index)
3674 {
3675   gint cursor_index;
3676   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3677
3678   cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3679   
3680   if (text_index > cursor_index)
3681     text_index += entry->preedit_length;
3682
3683   return text_index;
3684 }
3685
3686 /**
3687  * gtk_entry_get_layout_offsets:
3688  * @entry: a #GtkEntry
3689  * @x: location to store X offset of layout, or %NULL
3690  * @y: location to store Y offset of layout, or %NULL
3691  *
3692  *
3693  * Obtains the position of the #PangoLayout used to render text
3694  * in the entry, in widget coordinates. Useful if you want to line
3695  * up the text in an entry with some other text, e.g. when using the
3696  * entry to implement editable cells in a sheet widget.
3697  *
3698  * Also useful to convert mouse events into coordinates inside the
3699  * #PangoLayout, e.g. to take some action if some part of the entry text
3700  * is clicked.
3701  * 
3702  * Note that as the user scrolls around in the entry the offsets will
3703  * change; you'll need to connect to the "notify::scroll_offset"
3704  * signal to track this. Remember when using the #PangoLayout
3705  * functions you need to convert to and from pixels using
3706  * PANGO_PIXELS() or #PANGO_SCALE.
3707  *
3708  * Keep in mind that the layout text may contain a preedit string, so
3709  * gtk_entry_layout_index_to_text_index() and
3710  * gtk_entry_text_index_to_layout_index() are needed to convert byte
3711  * indices in the layout to byte indices in the entry contents.
3712  * 
3713  **/
3714 void
3715 gtk_entry_get_layout_offsets (GtkEntry *entry,
3716                               gint     *x,
3717                               gint     *y)
3718 {
3719   gint text_area_x, text_area_y;
3720   
3721   g_return_if_fail (GTK_IS_ENTRY (entry));
3722
3723   /* this gets coords relative to text area */
3724   get_layout_position (entry, x, y);
3725
3726   /* convert to widget coords */
3727   get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
3728   
3729   if (x)
3730     *x += text_area_x;
3731
3732   if (y)
3733     *y += text_area_y;
3734 }
3735
3736 /* Quick hack of a popup menu
3737  */
3738 static void
3739 activate_cb (GtkWidget *menuitem,
3740              GtkEntry  *entry)
3741 {
3742   const gchar *signal = gtk_object_get_data (GTK_OBJECT (menuitem), "gtk-signal");
3743   gtk_signal_emit_by_name (GTK_OBJECT (entry), signal);
3744 }
3745
3746
3747 static gboolean
3748 gtk_entry_mnemonic_activate (GtkWidget *widget,
3749                              gboolean   group_cycling)
3750 {
3751   gtk_widget_grab_focus (widget);
3752   return TRUE;
3753 }
3754
3755 static void
3756 append_action_signal (GtkEntry     *entry,
3757                       GtkWidget    *menu,
3758                       const gchar  *stock_id,
3759                       const gchar  *signal,
3760                       gboolean      sensitive)
3761 {
3762   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
3763
3764   gtk_object_set_data (GTK_OBJECT (menuitem), "gtk-signal", (char *)signal);
3765   gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
3766                       GTK_SIGNAL_FUNC (activate_cb), entry);
3767
3768   gtk_widget_set_sensitive (menuitem, sensitive);
3769   
3770   gtk_widget_show (menuitem);
3771   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
3772 }
3773         
3774 static void
3775 popup_menu_detach (GtkWidget *attach_widget,
3776                    GtkMenu   *menu)
3777 {
3778   GTK_ENTRY (attach_widget)->popup_menu = NULL;
3779 }
3780
3781 static void
3782 popup_position_func (GtkMenu   *menu,
3783                      gint      *x,
3784                      gint      *y,
3785                      gboolean  *push_in,
3786                      gpointer   user_data)
3787 {
3788   GtkEntry *entry = GTK_ENTRY (user_data);
3789   GtkWidget *widget = GTK_WIDGET (entry);
3790   GtkRequisition req;
3791   
3792   g_return_if_fail (GTK_WIDGET_REALIZED (entry));
3793
3794   gdk_window_get_origin (widget->window, x, y);      
3795
3796   gtk_widget_size_request (entry->popup_menu, &req);
3797   
3798   *x += widget->allocation.width / 2;
3799   *y += widget->allocation.height;
3800
3801   *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
3802   *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
3803 }
3804
3805
3806 static void
3807 unichar_chosen_func (const char *text,
3808                      gpointer    data)
3809 {
3810   GtkEntry *entry = GTK_ENTRY (data);
3811
3812   gtk_entry_enter_text (entry, text);
3813 }
3814
3815 static void
3816 menu_select_first_child (GtkMenu *menu)
3817 {
3818   GList *children = gtk_container_get_children (GTK_CONTAINER (menu));
3819
3820   GList *tmp_list = children;
3821   while (tmp_list)
3822     {
3823       GtkWidget *child = tmp_list->data;
3824       
3825       if (GTK_WIDGET_VISIBLE (child))
3826         {
3827           gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), child);
3828           break;
3829         }
3830       
3831       tmp_list = tmp_list->next;
3832     }
3833
3834   g_list_free (children);
3835 }
3836
3837 typedef struct
3838 {
3839   GtkEntry *entry;
3840   gint button;
3841   guint time;
3842 } PopupInfo;
3843
3844 static void
3845 popup_targets_received (GtkClipboard     *clipboard,
3846                         GtkSelectionData *data,
3847                         gpointer          user_data)
3848 {
3849   PopupInfo *info = user_data;
3850   GtkEntry *entry = info->entry;
3851   
3852   if (GTK_WIDGET_REALIZED (entry))
3853     {
3854       gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
3855       GtkWidget *menuitem;
3856       GtkWidget *submenu;
3857       
3858       if (entry->popup_menu)
3859         gtk_widget_destroy (entry->popup_menu);
3860       
3861       entry->popup_menu = gtk_menu_new ();
3862       
3863       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
3864                                  GTK_WIDGET (entry),
3865                                  popup_menu_detach);
3866       
3867       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
3868                             entry->editable && entry->current_pos != entry->selection_bound);
3869       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
3870                             entry->current_pos != entry->selection_bound);
3871       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
3872                             entry->editable && clipboard_contains_text);
3873       
3874       menuitem = gtk_menu_item_new_with_label (_("Select All"));
3875       gtk_signal_connect_object (GTK_OBJECT (menuitem), "activate",
3876                                  GTK_SIGNAL_FUNC (gtk_entry_select_all), entry);
3877       gtk_widget_show (menuitem);
3878       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3879       
3880       menuitem = gtk_separator_menu_item_new ();
3881       gtk_widget_show (menuitem);
3882       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3883       
3884       menuitem = gtk_menu_item_new_with_label (_("Input Methods"));
3885       gtk_widget_show (menuitem);
3886       submenu = gtk_menu_new ();
3887       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
3888       
3889       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3890       
3891       gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
3892                                             GTK_MENU_SHELL (submenu));
3893       
3894       menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
3895       gtk_widget_show (menuitem);
3896       
3897       submenu = gtk_menu_new ();
3898       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
3899       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
3900
3901       _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
3902                                                     unichar_chosen_func,
3903                                                     entry);
3904       if (!entry->editable)
3905         gtk_widget_set_sensitive (menuitem, FALSE);
3906       
3907       gtk_signal_emit (GTK_OBJECT (entry),
3908                        signals[POPULATE_POPUP],
3909                        entry->popup_menu);
3910   
3911
3912       if (info->button)
3913         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
3914                         NULL, NULL,
3915                         info->button, info->time);
3916       else
3917         {
3918           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
3919                           popup_position_func, entry,
3920                           info->button, info->time);
3921           menu_select_first_child (GTK_MENU (entry->popup_menu));
3922         }
3923     }
3924
3925   g_object_unref (entry);
3926   g_free (info);
3927 }
3928                         
3929 static void
3930 gtk_entry_do_popup (GtkEntry       *entry,
3931                     GdkEventButton *event)
3932 {
3933   PopupInfo *info = g_new (PopupInfo, 1);
3934
3935   /* In order to know what entries we should make sensitive, we
3936    * ask for the current targets of the clipboard, and when
3937    * we get them, then we actually pop up the menu.
3938    */
3939   info->entry = g_object_ref (entry);
3940   
3941   if (event)
3942     {
3943       info->button = event->button;
3944       info->time = event->time;
3945     }
3946   else
3947     {
3948       info->button = 0;
3949       info->time = gtk_get_current_event_time ();
3950     }
3951
3952   gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
3953                                   gdk_atom_intern ("TARGETS", FALSE),
3954                                   popup_targets_received,
3955                                   info);
3956 }
3957
3958 static void
3959 gtk_entry_popup_menu (GtkWidget *widget)
3960 {
3961   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
3962 }
3963
3964 static void
3965 gtk_entry_drag_leave (GtkWidget        *widget,
3966                       GdkDragContext   *context,
3967                       guint             time)
3968 {
3969   GtkEntry *entry = GTK_ENTRY (widget);
3970
3971   entry->dnd_position = -1;
3972   gtk_widget_queue_draw (widget);
3973 }
3974
3975 static gboolean
3976 gtk_entry_drag_drop  (GtkWidget        *widget,
3977                       GdkDragContext   *context,
3978                       gint              x,
3979                       gint              y,
3980                       guint             time)
3981 {
3982   GtkEntry *entry = GTK_ENTRY (widget);
3983   GdkAtom target = GDK_NONE;
3984   
3985   if (entry->editable)
3986     target = gtk_drag_dest_find_target (widget, context, NULL);
3987
3988   if (target != GDK_NONE)
3989     gtk_drag_get_data (widget, context, target, time);
3990   else
3991     gtk_drag_finish (context, FALSE, FALSE, time);
3992   
3993   return TRUE;
3994 }
3995
3996 static gboolean
3997 gtk_entry_drag_motion (GtkWidget        *widget,
3998                        GdkDragContext   *context,
3999                        gint              x,
4000                        gint              y,
4001                        guint             time)
4002 {
4003   GtkEntry *entry = GTK_ENTRY (widget);
4004   GtkWidget *source_widget;
4005   GdkDragAction suggested_action;
4006   gint new_position, old_position;
4007   gint sel1, sel2;
4008   
4009   x -= widget->style->xthickness;
4010   y -= widget->style->ythickness;
4011   
4012   old_position = entry->dnd_position;
4013   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
4014
4015   if (entry->editable)
4016     {
4017       source_widget = gtk_drag_get_source_widget (context);
4018       suggested_action = context->suggested_action;
4019
4020       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
4021           new_position < sel1 || new_position > sel2)
4022         {
4023           if (source_widget == widget)
4024             {
4025               /* Default to MOVE, unless the user has
4026                * pressed ctrl or alt to affect available actions
4027                */
4028               if ((context->actions & GDK_ACTION_MOVE) != 0)
4029                 suggested_action = GDK_ACTION_MOVE;
4030             }
4031               
4032           entry->dnd_position = new_position;
4033         }
4034       else
4035         {
4036           if (source_widget == widget)
4037             suggested_action = 0;       /* Can't drop in selection where drag started */
4038           
4039           entry->dnd_position = -1;
4040         }
4041     }
4042   else
4043     {
4044       /* Entry not editable */
4045       suggested_action = 0;
4046       entry->dnd_position = -1;
4047     }
4048   
4049   gdk_drag_status (context, suggested_action, time);
4050   
4051   if (entry->dnd_position != old_position)
4052     gtk_widget_queue_draw (widget);
4053
4054   return TRUE;
4055 }
4056
4057 static void
4058 gtk_entry_drag_data_received (GtkWidget        *widget,
4059                               GdkDragContext   *context,
4060                               gint              x,
4061                               gint              y,
4062                               GtkSelectionData *selection_data,
4063                               guint             info,
4064                               guint             time)
4065 {
4066   GtkEntry *entry = GTK_ENTRY (widget);
4067   GtkEditable *editable = GTK_EDITABLE (widget);
4068   gchar *str;
4069
4070   str = gtk_selection_data_get_text (selection_data);
4071
4072   if (str && entry->editable)
4073     {
4074       gint new_position;
4075       gint sel1, sel2;
4076
4077       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
4078
4079       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
4080           new_position < sel1 || new_position > sel2)
4081         {
4082           gtk_editable_insert_text (editable, str, -1, &new_position);
4083         }
4084       else
4085         {
4086           /* Replacing selection */
4087           gtk_editable_delete_text (editable, sel1, sel2);
4088           gtk_editable_insert_text (editable, str, -1, &sel1);
4089         }
4090       
4091       g_free (str);
4092       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
4093     }
4094   else
4095     {
4096       /* Drag and drop didn't happen! */
4097       gtk_drag_finish (context, FALSE, FALSE, time);
4098     }
4099 }
4100
4101 static void
4102 gtk_entry_drag_data_get (GtkWidget        *widget,
4103                          GdkDragContext   *context,
4104                          GtkSelectionData *selection_data,
4105                          guint             info,
4106                          guint             time)
4107 {
4108   gint sel_start, sel_end;
4109
4110   GtkEditable *editable = GTK_EDITABLE (widget);
4111   
4112   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
4113     {
4114       gchar *str = gtk_editable_get_chars (editable, sel_start, sel_end);
4115
4116       gtk_selection_data_set_text (selection_data, str, -1);
4117       
4118       g_free (str);
4119     }
4120
4121 }
4122
4123 static void
4124 gtk_entry_drag_data_delete (GtkWidget      *widget,
4125                             GdkDragContext *context)
4126 {
4127   gint sel_start, sel_end;
4128
4129   GtkEditable *editable = GTK_EDITABLE (widget);
4130   
4131   if (GTK_ENTRY (widget)->editable &&
4132       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
4133     gtk_editable_delete_text (editable, sel_start, sel_end);
4134 }
4135
4136 /* We display the cursor when
4137  *
4138  *  - the selection is empty, AND
4139  *  - the widget has focus
4140  */
4141
4142 #define CURSOR_ON_MULTIPLIER 0.66
4143 #define CURSOR_OFF_MULTIPLIER 0.34
4144 #define CURSOR_PEND_MULTIPLIER 1.0
4145
4146 static gboolean
4147 cursor_blinks (GtkEntry *entry)
4148 {
4149   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
4150   gboolean blink;
4151
4152   if (GTK_WIDGET_HAS_FOCUS (entry) &&
4153       entry->selection_bound == entry->current_pos)
4154     {
4155       g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4156       return blink;
4157     }
4158   else
4159     return FALSE;
4160 }
4161
4162 static gint
4163 get_cursor_time (GtkEntry *entry)
4164 {
4165   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
4166   gint time;
4167
4168   g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4169
4170   return time;
4171 }
4172
4173 static void
4174 show_cursor (GtkEntry *entry)
4175 {
4176   if (!entry->cursor_visible)
4177     {
4178       entry->cursor_visible = TRUE;
4179
4180       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
4181         gtk_widget_queue_draw (GTK_WIDGET (entry));
4182     }
4183 }
4184
4185 static void
4186 hide_cursor (GtkEntry *entry)
4187 {
4188   if (entry->cursor_visible)
4189     {
4190       entry->cursor_visible = FALSE;
4191
4192       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
4193         gtk_widget_queue_draw (GTK_WIDGET (entry));
4194     }
4195 }
4196
4197 /*
4198  * Blink!
4199  */
4200 static gint
4201 blink_cb (gpointer data)
4202 {
4203   GtkEntry *entry;
4204
4205   GDK_THREADS_ENTER ();
4206
4207   entry = GTK_ENTRY (data);
4208   
4209   g_assert (GTK_WIDGET_HAS_FOCUS (entry));
4210   g_assert (entry->selection_bound == entry->current_pos);
4211
4212   if (entry->cursor_visible)
4213     {
4214       hide_cursor (entry);
4215       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
4216                                               blink_cb,
4217                                               entry);
4218     }
4219   else
4220     {
4221       show_cursor (entry);
4222       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
4223                                               blink_cb,
4224                                               entry);
4225     }
4226
4227   GDK_THREADS_LEAVE ();
4228
4229   /* Remove ourselves */
4230   return FALSE;
4231 }
4232
4233 static void
4234 gtk_entry_check_cursor_blink (GtkEntry *entry)
4235 {
4236   if (cursor_blinks (entry))
4237     {
4238       if (!entry->blink_timeout)
4239         {
4240           entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
4241                                                   blink_cb,
4242                                                   entry);
4243           show_cursor (entry);
4244         }
4245     }
4246   else
4247     {
4248       if (entry->blink_timeout)  
4249         { 
4250           gtk_timeout_remove (entry->blink_timeout);
4251           entry->blink_timeout = 0;
4252         }
4253       
4254       entry->cursor_visible = TRUE;
4255     }
4256   
4257 }
4258
4259 static void
4260 gtk_entry_pend_cursor_blink (GtkEntry *entry)
4261 {
4262   if (cursor_blinks (entry))
4263     {
4264       if (entry->blink_timeout != 0)
4265         gtk_timeout_remove (entry->blink_timeout);
4266       
4267       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
4268                                               blink_cb,
4269                                               entry);
4270       show_cursor (entry);
4271     }
4272 }