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