]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
Call gtk_entry_set_position_internal() that takes a new "reset_IM"
[~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 const 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_set_position_internal (GtkEntry    *entry,
1755                                  gint         position,
1756                                  gboolean     reset_im)
1757 {
1758   if (position < 0 || position > entry->text_length)
1759     position = entry->text_length;
1760
1761   if (position != entry->current_pos ||
1762       position != entry->selection_bound)
1763     {
1764       if (reset_im)
1765         gtk_entry_reset_im_context (entry);
1766       gtk_entry_set_positions (entry, position, position);
1767     }
1768 }
1769
1770 static void
1771 gtk_entry_real_set_position (GtkEditable *editable,
1772                              gint         position)
1773 {
1774   gtk_entry_set_position_internal (GTK_ENTRY (editable), position, TRUE);
1775 }
1776
1777 static gint
1778 gtk_entry_get_position (GtkEditable *editable)
1779 {
1780   return GTK_ENTRY (editable)->current_pos;
1781 }
1782
1783 static void
1784 gtk_entry_set_selection_bounds (GtkEditable *editable,
1785                                 gint         start,
1786                                 gint         end)
1787 {
1788   GtkEntry *entry = GTK_ENTRY (editable);
1789
1790   if (start < 0)
1791     start = entry->text_length;
1792   if (end < 0)
1793     end = entry->text_length;
1794   
1795   gtk_entry_reset_im_context (entry);
1796
1797   gtk_entry_set_positions (entry,
1798                            MIN (end, entry->text_length),
1799                            MIN (start, entry->text_length));
1800
1801   gtk_entry_update_primary_selection (entry);
1802 }
1803
1804 static gboolean
1805 gtk_entry_get_selection_bounds (GtkEditable *editable,
1806                                 gint        *start,
1807                                 gint        *end)
1808 {
1809   GtkEntry *entry = GTK_ENTRY (editable);
1810
1811   *start = entry->selection_bound;
1812   *end = entry->current_pos;
1813
1814   return (entry->selection_bound != entry->current_pos);
1815 }
1816
1817 static void 
1818 gtk_entry_style_set     (GtkWidget      *widget,
1819                          GtkStyle       *previous_style)
1820 {
1821   GtkEntry *entry = GTK_ENTRY (widget);
1822
1823   if (previous_style && GTK_WIDGET_REALIZED (widget))
1824     {
1825       gtk_entry_recompute (entry);
1826
1827       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1828       gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1829     }
1830 }
1831
1832 /* GtkCellEditable method implementations
1833  */
1834 static void
1835 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
1836 {
1837   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1838   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1839 }
1840
1841 static gboolean
1842 gtk_cell_editable_key_press_event (GtkEntry    *entry,
1843                                    GdkEventKey *key_event,
1844                                    gpointer     data)
1845 {
1846   if (key_event->keyval == GDK_Escape)
1847     {
1848       entry->editing_canceled = TRUE;
1849       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1850       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1851
1852       return TRUE;
1853     }
1854
1855   /* override focus */
1856   if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
1857     {
1858       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1859       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1860
1861       return TRUE;
1862     }
1863
1864   return FALSE;
1865 }
1866
1867 static void
1868 gtk_entry_start_editing (GtkCellEditable *cell_editable,
1869                          GdkEvent        *event)
1870 {
1871   GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
1872
1873   g_signal_connect (cell_editable, "activate",
1874                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
1875   g_signal_connect (cell_editable, "key_press_event",
1876                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
1877 }
1878
1879 /* Default signal handlers
1880  */
1881 static void
1882 gtk_entry_real_insert_text (GtkEditable *editable,
1883                             const gchar *new_text,
1884                             gint         new_text_length,
1885                             gint        *position)
1886 {
1887   gint index;
1888   gint n_chars;
1889
1890   GtkEntry *entry = GTK_ENTRY (editable);
1891
1892   if (new_text_length < 0)
1893     new_text_length = strlen (new_text);
1894
1895   n_chars = g_utf8_strlen (new_text, new_text_length);
1896   if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
1897     {
1898       gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (entry)));
1899       n_chars = entry->text_max_length - entry->text_length;
1900       new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
1901     }
1902
1903   if (new_text_length + entry->n_bytes + 1 > entry->text_size)
1904     {
1905       while (new_text_length + entry->n_bytes + 1 > entry->text_size)
1906         {
1907           if (entry->text_size == 0)
1908             entry->text_size = MIN_SIZE;
1909           else
1910             {
1911               if (2 * (guint)entry->text_size < MAX_SIZE &&
1912                   2 * (guint)entry->text_size > entry->text_size)
1913                 entry->text_size *= 2;
1914               else
1915                 {
1916                   entry->text_size = MAX_SIZE;
1917                   if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
1918                     {
1919                       new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
1920                       new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
1921                       n_chars = g_utf8_strlen (new_text, new_text_length);
1922                     }
1923                   break;
1924                 }
1925             }
1926         }
1927
1928       entry->text = g_realloc (entry->text, entry->text_size);
1929     }
1930
1931   index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
1932
1933   g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
1934   memcpy (entry->text + index, new_text, new_text_length);
1935
1936   entry->n_bytes += new_text_length;
1937   entry->text_length += n_chars;
1938
1939   /* NUL terminate for safety and convenience */
1940   entry->text[entry->n_bytes] = '\0';
1941   
1942   if (entry->current_pos > *position)
1943     entry->current_pos += n_chars;
1944   
1945   if (entry->selection_bound > *position)
1946     entry->selection_bound += n_chars;
1947
1948   *position += n_chars;
1949
1950   gtk_entry_recompute (entry);
1951
1952   g_signal_emit_by_name (editable, "changed");
1953   g_object_notify (G_OBJECT (editable), "text");
1954 }
1955
1956 static void
1957 gtk_entry_real_delete_text (GtkEditable *editable,
1958                             gint         start_pos,
1959                             gint         end_pos)
1960 {
1961   GtkEntry *entry = GTK_ENTRY (editable);
1962
1963   if (start_pos < 0)
1964     start_pos = 0;
1965   if (end_pos < 0 || end_pos > entry->text_length)
1966     end_pos = entry->text_length;
1967   
1968   if (start_pos < end_pos)
1969     {
1970       gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
1971       gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
1972       gint current_pos;
1973       gint selection_bound;
1974
1975       g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
1976       entry->text_length -= (end_pos - start_pos);
1977       entry->n_bytes -= (end_index - start_index);
1978       
1979       current_pos = entry->current_pos;
1980       if (current_pos > start_pos)
1981         current_pos -= MIN (current_pos, end_pos) - start_pos;
1982
1983       selection_bound = entry->selection_bound;
1984       if (selection_bound > start_pos)
1985         selection_bound -= MIN (selection_bound, end_pos) - start_pos;
1986
1987       gtk_entry_set_positions (entry, current_pos, selection_bound);
1988
1989       /* We might have deleted the selection
1990        */
1991       gtk_entry_update_primary_selection (entry);
1992       
1993       gtk_entry_recompute (entry);
1994
1995       g_signal_emit_by_name (editable, "changed");
1996       g_object_notify (G_OBJECT (editable), "text");
1997     }
1998 }
1999
2000 /* Compute the X position for an offset that corresponds to the "more important
2001  * cursor position for that offset. We use this when trying to guess to which
2002  * end of the selection we should go to when the user hits the left or
2003  * right arrow key.
2004  */
2005 static gint
2006 get_better_cursor_x (GtkEntry *entry,
2007                      gint      offset)
2008 {
2009   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2010   GtkTextDirection keymap_direction =
2011     (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
2012     GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
2013   GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
2014   gboolean split_cursor;
2015   
2016   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2017   const gchar *text = pango_layout_get_text (layout);
2018   gint index = g_utf8_offset_to_pointer (text, offset) - text;
2019   
2020   PangoRectangle strong_pos, weak_pos;
2021   
2022   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2023                 "gtk-split-cursor", &split_cursor,
2024                 NULL);
2025
2026   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2027
2028   if (split_cursor)
2029     return strong_pos.x / PANGO_SCALE;
2030   else
2031     return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2032 }
2033
2034 static void
2035 gtk_entry_move_cursor (GtkEntry       *entry,
2036                        GtkMovementStep step,
2037                        gint            count,
2038                        gboolean        extend_selection)
2039 {
2040   gint new_pos = entry->current_pos;
2041
2042   gtk_entry_reset_im_context (entry);
2043
2044   if (entry->current_pos != entry->selection_bound && !extend_selection)
2045     {
2046       /* If we have a current selection and aren't extending it, move to the
2047        * start/or end of the selection as appropriate
2048        */
2049       switch (step)
2050         {
2051         case GTK_MOVEMENT_VISUAL_POSITIONS:
2052           {
2053             gint current_x = get_better_cursor_x (entry, entry->current_pos);
2054             gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2055
2056             if (count < 0)
2057               new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2058             else
2059               new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2060
2061             break;
2062           }
2063         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2064         case GTK_MOVEMENT_WORDS:
2065           if (count < 0)
2066             new_pos = MIN (entry->current_pos, entry->selection_bound);
2067           else
2068             new_pos = MAX (entry->current_pos, entry->selection_bound);
2069           break;
2070         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2071         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2072         case GTK_MOVEMENT_BUFFER_ENDS:
2073           new_pos = count < 0 ? 0 : entry->text_length;
2074           break;
2075         case GTK_MOVEMENT_DISPLAY_LINES:
2076         case GTK_MOVEMENT_PARAGRAPHS:
2077         case GTK_MOVEMENT_PAGES:
2078           break;
2079         }
2080     }
2081   else
2082     {
2083       switch (step)
2084         {
2085         case GTK_MOVEMENT_LOGICAL_POSITIONS:
2086           new_pos = gtk_entry_move_logically (entry, new_pos, count);
2087           break;
2088         case GTK_MOVEMENT_VISUAL_POSITIONS:
2089           new_pos = gtk_entry_move_visually (entry, new_pos, count);
2090           break;
2091         case GTK_MOVEMENT_WORDS:
2092           while (count > 0)
2093             {
2094               new_pos = gtk_entry_move_forward_word (entry, new_pos);
2095               count--;
2096             }
2097           while (count < 0)
2098             {
2099               new_pos = gtk_entry_move_backward_word (entry, new_pos);
2100               count++;
2101             }
2102           break;
2103         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2104         case GTK_MOVEMENT_PARAGRAPH_ENDS:
2105         case GTK_MOVEMENT_BUFFER_ENDS:
2106           new_pos = count < 0 ? 0 : entry->text_length;
2107           break;
2108         case GTK_MOVEMENT_DISPLAY_LINES:
2109         case GTK_MOVEMENT_PARAGRAPHS:
2110         case GTK_MOVEMENT_PAGES:
2111           break;
2112         }
2113     }
2114
2115   if (extend_selection)
2116     gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2117   else
2118     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2119   
2120   gtk_entry_pend_cursor_blink (entry);
2121 }
2122
2123 static void
2124 gtk_entry_insert_at_cursor (GtkEntry    *entry,
2125                             const gchar *str)
2126 {
2127   GtkEditable *editable = GTK_EDITABLE (entry);
2128   gint pos = entry->current_pos;
2129
2130   if (entry->editable)
2131     {
2132       gtk_entry_reset_im_context (entry);
2133
2134       gtk_editable_insert_text (editable, str, -1, &pos);
2135       gtk_editable_set_position (editable, pos);
2136     }
2137 }
2138
2139 static void
2140 gtk_entry_delete_from_cursor (GtkEntry       *entry,
2141                               GtkDeleteType   type,
2142                               gint            count)
2143 {
2144   GtkEditable *editable = GTK_EDITABLE (entry);
2145   gint start_pos = entry->current_pos;
2146   gint end_pos = entry->current_pos;
2147   
2148   gtk_entry_reset_im_context (entry);
2149
2150   if (!entry->editable)
2151     return;
2152
2153   if (entry->selection_bound != entry->current_pos)
2154     {
2155       gtk_editable_delete_selection (editable);
2156       return;
2157     }
2158   
2159   switch (type)
2160     {
2161     case GTK_DELETE_CHARS:
2162       end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2163       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2164       break;
2165     case GTK_DELETE_WORDS:
2166       if (count < 0)
2167         {
2168           /* Move to end of current word, or if not on a word, end of previous word */
2169           end_pos = gtk_entry_move_backward_word (entry, end_pos);
2170           end_pos = gtk_entry_move_forward_word (entry, end_pos);
2171         }
2172       else if (count > 0)
2173         {
2174           /* Move to beginning of current word, or if not on a word, begining of next word */
2175           start_pos = gtk_entry_move_forward_word (entry, start_pos);
2176           start_pos = gtk_entry_move_backward_word (entry, start_pos);
2177         }
2178         
2179       /* Fall through */
2180     case GTK_DELETE_WORD_ENDS:
2181       while (count < 0)
2182         {
2183           start_pos = gtk_entry_move_backward_word (entry, start_pos);
2184           count++;
2185         }
2186       while (count > 0)
2187         {
2188           end_pos = gtk_entry_move_forward_word (entry, end_pos);
2189           count--;
2190         }
2191       gtk_editable_delete_text (editable, start_pos, end_pos);
2192       break;
2193     case GTK_DELETE_DISPLAY_LINE_ENDS:
2194     case GTK_DELETE_PARAGRAPH_ENDS:
2195       if (count < 0)
2196         gtk_editable_delete_text (editable, 0, entry->current_pos);
2197       else
2198         gtk_editable_delete_text (editable, entry->current_pos, -1);
2199       break;
2200     case GTK_DELETE_DISPLAY_LINES:
2201     case GTK_DELETE_PARAGRAPHS:
2202       gtk_editable_delete_text (editable, 0, -1);  
2203       break;
2204     case GTK_DELETE_WHITESPACE:
2205       gtk_entry_delete_whitespace (entry);
2206       break;
2207     }
2208   
2209   gtk_entry_pend_cursor_blink (entry);
2210 }
2211
2212 static void
2213 gtk_entry_copy_clipboard (GtkEntry *entry)
2214 {
2215   GtkEditable *editable = GTK_EDITABLE (entry);
2216   gint start, end;
2217
2218   if (gtk_editable_get_selection_bounds (editable, &start, &end))
2219     {
2220       gchar *str = gtk_entry_get_public_chars (entry, start, end);
2221       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2222                                                         GDK_SELECTION_CLIPBOARD),
2223                               str, -1);
2224       g_free (str);
2225     }
2226 }
2227
2228 static void
2229 gtk_entry_cut_clipboard (GtkEntry *entry)
2230 {
2231   GtkEditable *editable = GTK_EDITABLE (entry);
2232   gint start, end;
2233
2234   gtk_entry_copy_clipboard (entry);
2235
2236   if (entry->editable)
2237     {
2238       if (gtk_editable_get_selection_bounds (editable, &start, &end))
2239         gtk_editable_delete_text (editable, start, end);
2240     }
2241 }
2242
2243 static void
2244 gtk_entry_paste_clipboard (GtkEntry *entry)
2245 {
2246   if (entry->editable)
2247     gtk_entry_paste (entry, GDK_NONE);
2248 }
2249
2250 static void
2251 gtk_entry_toggle_overwrite (GtkEntry *entry)
2252 {
2253   entry->overwrite_mode = !entry->overwrite_mode;
2254 }
2255
2256 static void
2257 gtk_entry_select_all (GtkEntry *entry)
2258 {
2259   gtk_entry_select_line (entry);
2260 }
2261
2262 static void
2263 gtk_entry_real_activate (GtkEntry *entry)
2264 {
2265   GtkWindow *window;
2266   GtkWidget *toplevel;
2267   GtkWidget *widget;
2268
2269   widget = GTK_WIDGET (entry);
2270
2271   if (entry->activates_default)
2272     {
2273       toplevel = gtk_widget_get_toplevel (widget);
2274       if (GTK_IS_WINDOW (toplevel))
2275         {
2276           window = GTK_WINDOW (toplevel);
2277       
2278           if (window &&
2279               widget != window->default_widget &&
2280               !(widget == window->focus_widget &&
2281                 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2282             gtk_window_activate_default (window);
2283         }
2284     }
2285 }
2286
2287 static void
2288 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
2289                                     GtkEntry  *entry)
2290 {
2291   gtk_entry_queue_draw (entry);
2292 }
2293
2294 /* IM Context Callbacks
2295  */
2296
2297 static void
2298 gtk_entry_commit_cb (GtkIMContext *context,
2299                      const gchar  *str,
2300                      GtkEntry     *entry)
2301 {
2302   gtk_entry_enter_text (entry, str);
2303 }
2304
2305 static void 
2306 gtk_entry_preedit_changed_cb (GtkIMContext *context,
2307                               GtkEntry     *entry)
2308 {
2309   gchar *preedit_string;
2310   gint cursor_pos;
2311   
2312   gtk_im_context_get_preedit_string (entry->im_context,
2313                                      &preedit_string, NULL,
2314                                      &cursor_pos);
2315   entry->preedit_length = strlen (preedit_string);
2316   cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
2317   entry->preedit_cursor = cursor_pos;
2318   g_free (preedit_string);
2319
2320   gtk_entry_recompute (entry);
2321 }
2322
2323 static gboolean
2324 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
2325                                GtkEntry     *entry)
2326 {
2327   gtk_im_context_set_surrounding (context,
2328                                   entry->text,
2329                                   entry->n_bytes,
2330                                   g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
2331
2332   return TRUE;
2333 }
2334
2335 static gboolean
2336 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
2337                                  gint          offset,
2338                                  gint          n_chars,
2339                                  GtkEntry     *entry)
2340 {
2341   gtk_editable_delete_text (GTK_EDITABLE (entry),
2342                             entry->current_pos + offset,
2343                             entry->current_pos + offset + n_chars);
2344
2345   return TRUE;
2346 }
2347
2348 /* Internal functions
2349  */
2350
2351 /* Used for im_commit_cb and inserting Unicode chars */
2352 static void
2353 gtk_entry_enter_text (GtkEntry       *entry,
2354                       const gchar    *str)
2355 {
2356   GtkEditable *editable = GTK_EDITABLE (entry);
2357   gint tmp_pos;
2358
2359   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
2360     gtk_editable_delete_selection (editable);
2361   else
2362     {
2363       if (entry->overwrite_mode)
2364         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
2365     }
2366
2367   tmp_pos = entry->current_pos;
2368   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
2369   gtk_entry_set_position_internal (entry, tmp_pos, FALSE);
2370 }
2371
2372 /* All changes to entry->current_pos and entry->selection_bound
2373  * should go through this function.
2374  */
2375 static void
2376 gtk_entry_set_positions (GtkEntry *entry,
2377                          gint      current_pos,
2378                          gint      selection_bound)
2379 {
2380   gboolean changed = FALSE;
2381
2382   g_object_freeze_notify (G_OBJECT (entry));
2383   
2384   if (current_pos != -1 &&
2385       entry->current_pos != current_pos)
2386     {
2387       entry->current_pos = current_pos;
2388       changed = TRUE;
2389
2390       g_object_notify (G_OBJECT (entry), "cursor_position");
2391     }
2392
2393   if (selection_bound != -1 &&
2394       entry->selection_bound != selection_bound)
2395     {
2396       entry->selection_bound = selection_bound;
2397       changed = TRUE;
2398       
2399       g_object_notify (G_OBJECT (entry), "selection_bound");
2400     }
2401
2402   g_object_thaw_notify (G_OBJECT (entry));
2403
2404   if (changed)
2405     gtk_entry_recompute (entry);
2406 }
2407
2408 static void
2409 gtk_entry_reset_layout (GtkEntry *entry)
2410 {
2411   if (entry->cached_layout)
2412     {
2413       g_object_unref (entry->cached_layout);
2414       entry->cached_layout = NULL;
2415     }
2416 }
2417
2418 static void
2419 update_im_cursor_location (GtkEntry *entry)
2420 {
2421   GdkRectangle area;
2422   gint strong_x;
2423   gint strong_xoffset;
2424   gint area_width, area_height;
2425
2426   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
2427 ;
2428   get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
2429
2430   strong_xoffset = strong_x - entry->scroll_offset;
2431   if (strong_xoffset < 0)
2432     {
2433       strong_xoffset = 0;
2434     }
2435   else if (strong_xoffset > area_width)
2436     {
2437       strong_xoffset = area_width;
2438     }
2439   area.x = strong_xoffset;
2440   area.y = 0;
2441   area.width = 0;
2442   area.height = area_height;
2443
2444   gtk_im_context_set_cursor_location (entry->im_context, &area);
2445 }
2446
2447 static gboolean
2448 recompute_idle_func (gpointer data)
2449 {
2450   GtkEntry *entry;
2451
2452   GDK_THREADS_ENTER ();
2453
2454   entry = GTK_ENTRY (data);
2455
2456   gtk_entry_adjust_scroll (entry);
2457   gtk_entry_queue_draw (entry);
2458
2459   entry->recompute_idle = FALSE;
2460   
2461   update_im_cursor_location (entry);
2462
2463   GDK_THREADS_LEAVE ();
2464
2465   return FALSE;
2466 }
2467
2468 static void
2469 gtk_entry_recompute (GtkEntry *entry)
2470 {
2471   gtk_entry_reset_layout (entry);
2472   gtk_entry_check_cursor_blink (entry);
2473   
2474   if (!entry->recompute_idle)
2475     {
2476       entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
2477                                                recompute_idle_func, entry, NULL); 
2478     }
2479 }
2480
2481 static void
2482 append_char (GString *str,
2483              gunichar ch,
2484              gint     count)
2485 {
2486   gint i;
2487   gint char_len;
2488   gchar buf[7];
2489   
2490   char_len = g_unichar_to_utf8 (ch, buf);
2491   
2492   i = 0;
2493   while (i < count)
2494     {
2495       g_string_append_len (str, buf, char_len);
2496       ++i;
2497     }
2498 }
2499      
2500 static PangoLayout *
2501 gtk_entry_create_layout (GtkEntry *entry,
2502                          gboolean  include_preedit)
2503 {
2504   PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
2505   PangoAttrList *tmp_attrs = pango_attr_list_new ();
2506   
2507   gchar *preedit_string = NULL;
2508   gint preedit_length = 0;
2509   PangoAttrList *preedit_attrs = NULL;
2510
2511   pango_layout_set_single_paragraph_mode (layout, TRUE);
2512   
2513   if (include_preedit)
2514     {
2515       gtk_im_context_get_preedit_string (entry->im_context,
2516                                          &preedit_string, &preedit_attrs, NULL);
2517       preedit_length = entry->preedit_length;
2518     }
2519
2520   if (preedit_length)
2521     {
2522       GString *tmp_string = g_string_new (NULL);
2523       
2524       gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
2525       
2526       if (entry->visible)
2527         {
2528           g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
2529           g_string_insert (tmp_string, cursor_index, preedit_string);
2530         }
2531       else
2532         {
2533           gint ch_len;
2534           gint preedit_len_chars;
2535           gunichar invisible_char;
2536           
2537           ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
2538           preedit_len_chars = g_utf8_strlen (preedit_string, -1);
2539           ch_len += preedit_len_chars;
2540
2541           if (entry->invisible_char != 0)
2542             invisible_char = entry->invisible_char;
2543           else
2544             invisible_char = ' '; /* just pick a char */
2545           
2546           append_char (tmp_string, invisible_char, ch_len);
2547           
2548           /* Fix cursor index to point to invisible char corresponding
2549            * to the preedit, fix preedit_length to be the length of
2550            * the invisible chars representing the preedit
2551            */
2552           cursor_index =
2553             g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
2554             tmp_string->str;
2555           preedit_length =
2556             preedit_len_chars *
2557             g_unichar_to_utf8 (invisible_char, NULL);
2558         }
2559       
2560       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
2561       
2562       pango_attr_list_splice (tmp_attrs, preedit_attrs,
2563                               cursor_index, preedit_length);
2564       
2565       g_string_free (tmp_string, TRUE);
2566     }
2567   else
2568     {
2569       if (entry->visible)
2570         {
2571           pango_layout_set_text (layout, entry->text, entry->n_bytes);
2572         }
2573       else
2574         {
2575           GString *str = g_string_new (NULL);
2576           gunichar invisible_char;
2577           
2578           if (entry->invisible_char != 0)
2579             invisible_char = entry->invisible_char;
2580           else
2581             invisible_char = ' '; /* just pick a char */
2582           
2583           append_char (str, invisible_char, entry->text_length);
2584           pango_layout_set_text (layout, str->str, str->len);
2585           g_string_free (str, TRUE);
2586         }
2587     }
2588       
2589   pango_layout_set_attributes (layout, tmp_attrs);
2590
2591   if (preedit_string)
2592     g_free (preedit_string);
2593   if (preedit_attrs)
2594     pango_attr_list_unref (preedit_attrs);
2595       
2596   pango_attr_list_unref (tmp_attrs);
2597
2598   return layout;
2599 }
2600
2601 static PangoLayout *
2602 gtk_entry_ensure_layout (GtkEntry *entry,
2603                          gboolean  include_preedit)
2604 {
2605   if (entry->preedit_length > 0 &&
2606       !include_preedit != !entry->cache_includes_preedit)
2607     gtk_entry_reset_layout (entry);
2608
2609   if (!entry->cached_layout)
2610     {
2611       entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
2612       entry->cache_includes_preedit = include_preedit;
2613     }
2614   
2615   return entry->cached_layout;
2616 }
2617
2618 static void
2619 get_layout_position (GtkEntry *entry,
2620                      gint     *x,
2621                      gint     *y)
2622 {
2623   PangoLayout *layout;
2624   PangoRectangle logical_rect;
2625   gint area_width, area_height;
2626   gint y_pos;
2627   PangoLayoutLine *line;
2628   
2629   layout = gtk_entry_ensure_layout (entry, TRUE);
2630
2631   get_text_area_size (entry, NULL, NULL, &area_width, &area_height);      
2632       
2633   area_height = PANGO_SCALE * (area_height - 2 * INNER_BORDER);
2634   
2635   line = pango_layout_get_lines (layout)->data;
2636   pango_layout_line_get_extents (line, NULL, &logical_rect);
2637   
2638   /* Align primarily for locale's ascent/descent */
2639   y_pos = ((area_height - entry->ascent - entry->descent) / 2 + 
2640            entry->ascent + logical_rect.y);
2641   
2642   /* Now see if we need to adjust to fit in actual drawn string */
2643   if (logical_rect.height > area_height)
2644     y_pos = (area_height - logical_rect.height) / 2;
2645   else if (y_pos < 0)
2646     y_pos = 0;
2647   else if (y_pos + logical_rect.height > area_height)
2648     y_pos = area_height - logical_rect.height;
2649   
2650   y_pos = INNER_BORDER + y_pos / PANGO_SCALE;
2651
2652   if (x)
2653     *x = INNER_BORDER - entry->scroll_offset;
2654
2655   if (y)
2656     *y = y_pos;
2657 }
2658
2659 static void
2660 gtk_entry_draw_text (GtkEntry *entry)
2661 {
2662   GtkWidget *widget;
2663   PangoLayoutLine *line;
2664   
2665   if (!entry->visible && entry->invisible_char == 0)
2666     return;
2667   
2668   if (GTK_WIDGET_DRAWABLE (entry))
2669     {
2670       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2671       gint x, y;
2672       gint start_pos, end_pos;
2673       
2674       widget = GTK_WIDGET (entry);
2675       
2676       get_layout_position (entry, &x, &y);
2677
2678       gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],       
2679                        x, y,
2680                        layout);
2681       
2682       if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
2683         {
2684           gint *ranges;
2685           gint n_ranges, i;
2686           PangoRectangle logical_rect;
2687           const gchar *text = pango_layout_get_text (layout);
2688           gint start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
2689           gint end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
2690           GdkRegion *clip_region = gdk_region_new ();
2691           GdkGC *text_gc;
2692           GdkGC *selection_gc;
2693
2694           line = pango_layout_get_lines (layout)->data;
2695           
2696           pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
2697
2698           pango_layout_get_extents (layout, NULL, &logical_rect);
2699           
2700           if (GTK_WIDGET_HAS_FOCUS (entry))
2701             {
2702               selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
2703               text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
2704             }
2705           else
2706             {
2707               selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
2708               text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
2709             }
2710           
2711           for (i=0; i < n_ranges; i++)
2712             {
2713               GdkRectangle rect;
2714
2715               rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
2716               rect.y = y;
2717               rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
2718               rect.height = logical_rect.height / PANGO_SCALE;
2719                 
2720               gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
2721                                   rect.x, rect.y, rect.width, rect.height);
2722
2723               gdk_region_union_with_rect (clip_region, &rect);
2724             }
2725
2726           gdk_gc_set_clip_region (text_gc, clip_region);
2727           gdk_draw_layout (entry->text_area, text_gc, 
2728                            x, y,
2729                            layout);
2730           gdk_gc_set_clip_region (text_gc, NULL);
2731           
2732           gdk_region_destroy (clip_region);
2733           g_free (ranges);
2734         }
2735     }
2736 }
2737
2738 static void
2739 gtk_entry_draw_cursor (GtkEntry  *entry,
2740                        CursorType type)
2741 {
2742   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2743   GtkTextDirection keymap_direction =
2744     (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
2745     GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
2746   GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
2747   
2748   if (GTK_WIDGET_DRAWABLE (entry))
2749     {
2750       GtkWidget *widget = GTK_WIDGET (entry);
2751       GdkRectangle cursor_location;
2752       gboolean split_cursor;
2753
2754       gint xoffset = INNER_BORDER - entry->scroll_offset;
2755       gint strong_x, weak_x;
2756       gint text_area_height;
2757       GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
2758       GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
2759       gint x1 = 0;
2760       gint x2 = 0;
2761       GdkGC *gc;
2762
2763       gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
2764       
2765       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
2766
2767       g_object_get (gtk_widget_get_settings (widget),
2768                     "gtk-split-cursor", &split_cursor,
2769                     NULL);
2770
2771       dir1 = widget_direction;
2772       
2773       if (split_cursor)
2774         {
2775           x1 = strong_x;
2776
2777           if (weak_x != strong_x)
2778             {
2779               dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
2780               x2 = weak_x;
2781             }
2782         }
2783       else
2784         {
2785           if (keymap_direction == widget_direction)
2786             x1 = strong_x;
2787           else
2788             x1 = weak_x;
2789         }
2790
2791       cursor_location.x = xoffset + x1;
2792       cursor_location.y = INNER_BORDER;
2793       cursor_location.width = 0;
2794       cursor_location.height = text_area_height - 2 * INNER_BORDER ;
2795
2796       gc = _gtk_get_insertion_cursor_gc (widget, TRUE);
2797       _gtk_draw_insertion_cursor (widget, entry->text_area, gc,
2798                                   &cursor_location, dir1,
2799                                   dir2 != GTK_TEXT_DIR_NONE);
2800       g_object_unref (gc);
2801       
2802       if (dir2 != GTK_TEXT_DIR_NONE)
2803         {
2804           cursor_location.x = xoffset + x2;
2805           gc = _gtk_get_insertion_cursor_gc (widget, FALSE);
2806           _gtk_draw_insertion_cursor (widget, entry->text_area, gc,
2807                                       &cursor_location, dir2,
2808                                       TRUE);
2809           g_object_unref (gc);
2810         }
2811     }
2812 }
2813
2814 static void
2815 gtk_entry_queue_draw (GtkEntry *entry)
2816 {
2817   if (GTK_WIDGET_REALIZED (entry))
2818     gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
2819 }
2820
2821 static void
2822 gtk_entry_reset_im_context (GtkEntry *entry)
2823 {
2824   if (entry->need_im_reset)
2825     {
2826       entry->need_im_reset = 0;
2827       gtk_im_context_reset (entry->im_context);
2828     }
2829 }
2830
2831 static gint
2832 gtk_entry_find_position (GtkEntry *entry,
2833                          gint      x)
2834 {
2835   PangoLayout *layout;
2836   PangoLayoutLine *line;
2837   gint index;
2838   gint pos;
2839   gboolean trailing;
2840   const gchar *text;
2841   gint cursor_index;
2842   
2843   layout = gtk_entry_ensure_layout (entry, TRUE);
2844   text = pango_layout_get_text (layout);
2845   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
2846   
2847   line = pango_layout_get_lines (layout)->data;
2848   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
2849
2850   if (index >= cursor_index && entry->preedit_length)
2851     {
2852       if (index >= cursor_index + entry->preedit_length)
2853         index -= entry->preedit_length;
2854       else
2855         {
2856           index = cursor_index;
2857           trailing = 0;
2858         }
2859     }
2860
2861   pos = g_utf8_pointer_to_offset (text, text + index);
2862   pos += trailing;
2863
2864   return pos;
2865 }
2866
2867 static void
2868 gtk_entry_get_cursor_locations (GtkEntry   *entry,
2869                                 CursorType  type,
2870                                 gint       *strong_x,
2871                                 gint       *weak_x)
2872 {
2873   if (!entry->visible && !entry->invisible_char)
2874     {
2875       if (strong_x)
2876         *strong_x = 0;
2877       
2878       if (weak_x)
2879         *weak_x = 0;
2880     }
2881   else
2882     {
2883       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2884       const gchar *text = pango_layout_get_text (layout);
2885       PangoRectangle strong_pos, weak_pos;
2886       gint index;
2887   
2888       if (type == CURSOR_STANDARD)
2889         {
2890           index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
2891         }
2892       else /* type == CURSOR_DND */
2893         {
2894           index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
2895
2896           if (entry->dnd_position > entry->current_pos)
2897             {
2898               if (entry->visible)
2899                 index += entry->preedit_length;
2900               else
2901                 {
2902                   gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
2903                   index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
2904                 }
2905             }
2906         }
2907       
2908       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2909       
2910       if (strong_x)
2911         *strong_x = strong_pos.x / PANGO_SCALE;
2912       
2913       if (weak_x)
2914         *weak_x = weak_pos.x / PANGO_SCALE;
2915     }
2916 }
2917
2918 static void
2919 gtk_entry_adjust_scroll (GtkEntry *entry)
2920 {
2921   gint min_offset, max_offset;
2922   gint text_area_width;
2923   gint strong_x, weak_x;
2924   gint strong_xoffset, weak_xoffset;
2925   PangoLayout *layout;
2926   PangoLayoutLine *line;
2927   PangoRectangle logical_rect;
2928
2929   if (!GTK_WIDGET_REALIZED (entry))
2930     return;
2931   
2932   gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
2933   text_area_width -= 2 * INNER_BORDER;
2934
2935   layout = gtk_entry_ensure_layout (entry, TRUE);
2936   line = pango_layout_get_lines (layout)->data;
2937
2938   pango_layout_line_get_extents (line, NULL, &logical_rect);
2939
2940   /* Display as much text as we can */
2941
2942   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
2943     {
2944       min_offset = 0;
2945       max_offset = MAX (min_offset, logical_rect.width / PANGO_SCALE - text_area_width);
2946     }
2947   else
2948     {
2949       max_offset = logical_rect.width / PANGO_SCALE - text_area_width;
2950       min_offset = MIN (0, max_offset);
2951     }
2952
2953   entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
2954
2955   /* And make sure cursors are on screen. Note that the cursor is
2956    * actually drawn one pixel into the INNER_BORDER space on
2957    * the right, when the scroll is at the utmost right. This
2958    * looks better to to me than confining the cursor inside the
2959    * border entirely, though it means that the cursor gets one
2960    * pixel closer to the the edge of the widget on the right than
2961    * on the left. This might need changing if one changed
2962    * INNER_BORDER from 2 to 1, as one would do on a
2963    * small-screen-real-estate display.
2964    *
2965    * We always make sure that the strong cursor is on screen, and
2966    * put the weak cursor on screen if possible.
2967    */
2968
2969   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
2970   
2971   strong_xoffset = strong_x - entry->scroll_offset;
2972
2973   if (strong_xoffset < 0)
2974     {
2975       entry->scroll_offset += strong_xoffset;
2976       strong_xoffset = 0;
2977     }
2978   else if (strong_xoffset > text_area_width)
2979     {
2980       entry->scroll_offset += strong_xoffset - text_area_width;
2981       strong_xoffset = text_area_width;
2982     }
2983
2984   weak_xoffset = weak_x - entry->scroll_offset;
2985
2986   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
2987     {
2988       entry->scroll_offset += weak_xoffset;
2989     }
2990   else if (weak_xoffset > text_area_width &&
2991            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
2992     {
2993       entry->scroll_offset += weak_xoffset - text_area_width;
2994     }
2995
2996   g_object_notify (G_OBJECT (entry), "scroll_offset");
2997 }
2998
2999 static gint
3000 gtk_entry_move_visually (GtkEntry *entry,
3001                          gint      start,
3002                          gint      count)
3003 {
3004   gint index;
3005   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3006   const gchar *text;
3007
3008   text = pango_layout_get_text (layout);
3009   
3010   index = g_utf8_offset_to_pointer (text, start) - text;
3011
3012   while (count != 0)
3013     {
3014       int new_index, new_trailing;
3015       gboolean split_cursor;
3016       gboolean strong;
3017
3018       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3019                     "gtk-split-cursor", &split_cursor,
3020                     NULL);
3021
3022       if (split_cursor)
3023         strong = TRUE;
3024       else
3025         {
3026           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3027           GtkTextDirection keymap_direction =
3028             (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
3029             GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
3030
3031           strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
3032         }
3033       
3034       if (count > 0)
3035         {
3036           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3037           count--;
3038         }
3039       else
3040         {
3041           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3042           count++;
3043         }
3044
3045       if (new_index < 0 || new_index == G_MAXINT)
3046         break;
3047
3048       index = new_index;
3049       
3050       while (new_trailing--)
3051         index = g_utf8_next_char (text + new_index) - text;
3052     }
3053   
3054   return g_utf8_pointer_to_offset (text, text + index);
3055 }
3056
3057 static gint
3058 gtk_entry_move_logically (GtkEntry *entry,
3059                           gint      start,
3060                           gint      count)
3061 {
3062   gint new_pos = start;
3063
3064   /* Prevent any leak of information */
3065   if (!entry->visible)
3066     {
3067       new_pos = CLAMP (start + count, 0, entry->text_length);
3068     }
3069   else if (entry->text)
3070     {
3071       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3072       PangoLogAttr *log_attrs;
3073       gint n_attrs;
3074
3075       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3076
3077       while (count > 0 && new_pos < entry->text_length)
3078         {
3079           do
3080             new_pos++;
3081           while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3082           
3083           count--;
3084         }
3085       while (count < 0 && new_pos > 0)
3086         {
3087           do
3088             new_pos--;
3089           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3090           
3091           count++;
3092         }
3093       
3094       g_free (log_attrs);
3095     }
3096
3097   return new_pos;
3098 }
3099
3100 static gint
3101 gtk_entry_move_forward_word (GtkEntry *entry,
3102                              gint      start)
3103 {
3104   gint new_pos = start;
3105
3106   /* Prevent any leak of information */
3107   if (!entry->visible)
3108     {
3109       new_pos = entry->text_length;
3110     }
3111   else if (entry->text && (new_pos < entry->text_length))
3112     {
3113       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3114       PangoLogAttr *log_attrs;
3115       gint n_attrs;
3116
3117       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3118       
3119       /* Find the next word end */
3120       new_pos++;
3121       while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
3122         new_pos++;
3123
3124       g_free (log_attrs);
3125     }
3126
3127   return new_pos;
3128 }
3129
3130
3131 static gint
3132 gtk_entry_move_backward_word (GtkEntry *entry,
3133                               gint      start)
3134 {
3135   gint new_pos = start;
3136
3137   /* Prevent any leak of information */
3138   if (!entry->visible)
3139     {
3140       new_pos = 0;
3141     }
3142   else if (entry->text && start > 0)
3143     {
3144       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3145       PangoLogAttr *log_attrs;
3146       gint n_attrs;
3147
3148       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3149
3150       new_pos = start - 1;
3151
3152       /* Find the previous word beginning */
3153       while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
3154         new_pos--;
3155
3156       g_free (log_attrs);
3157     }
3158
3159   return new_pos;
3160 }
3161
3162 static void
3163 gtk_entry_delete_whitespace (GtkEntry *entry)
3164 {
3165   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3166   PangoLogAttr *log_attrs;
3167   gint n_attrs;
3168   gint start, end;
3169
3170   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3171
3172   start = end = entry->current_pos;
3173   
3174   while (start > 0 && log_attrs[start-1].is_white)
3175     start--;
3176
3177   while (end < n_attrs && log_attrs[end].is_white)
3178     end++;
3179
3180   g_free (log_attrs);
3181
3182   if (start != end)
3183     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
3184 }
3185
3186
3187 static void
3188 gtk_entry_select_word (GtkEntry *entry)
3189 {
3190   gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos);
3191   gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos);
3192
3193   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
3194 }
3195
3196 static void
3197 gtk_entry_select_line (GtkEntry *entry)
3198 {
3199   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
3200 }
3201
3202 /*
3203  * Like gtk_editable_get_chars, but handle not-visible entries
3204  * correctly.
3205  */
3206 static char *    
3207 gtk_entry_get_public_chars (GtkEntry *entry,
3208                             gint      start,
3209                             gint      end)
3210 {
3211   if (end < 0)
3212     end = entry->text_length;
3213   
3214   if (entry->visible)
3215     return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
3216   else if (!entry->invisible_char)
3217     return g_strdup ("");
3218   else
3219     {
3220       GString *str = g_string_new (NULL);
3221       append_char (str, entry->invisible_char, end - start);
3222       return g_string_free (str, FALSE);
3223     }
3224 }
3225
3226 static void
3227 paste_received (GtkClipboard *clipboard,
3228                 const gchar  *text,
3229                 gpointer      data)
3230 {
3231   GtkEntry *entry = GTK_ENTRY (data);
3232   GtkEditable *editable = GTK_EDITABLE (entry);
3233       
3234   if (text)
3235     {
3236       gint pos, start, end;
3237       
3238       if (gtk_editable_get_selection_bounds (editable, &start, &end))
3239         gtk_editable_delete_text (editable, start, end);
3240
3241       pos = entry->current_pos;
3242       gtk_editable_insert_text (editable, text, -1, &pos);
3243       gtk_editable_set_position (editable, pos);
3244     }
3245
3246   g_object_unref (entry);
3247 }
3248
3249 static void
3250 gtk_entry_paste (GtkEntry *entry,
3251                  GdkAtom   selection)
3252 {
3253   g_object_ref (entry);
3254   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
3255                               paste_received, entry);
3256 }
3257
3258 static void
3259 primary_get_cb (GtkClipboard     *clipboard,
3260                 GtkSelectionData *selection_data,
3261                 guint             info,
3262                 gpointer          data)
3263 {
3264   GtkEntry *entry = GTK_ENTRY (data);
3265   gint start, end;
3266   
3267   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
3268     {
3269       gchar *str = gtk_entry_get_public_chars (entry, start, end);
3270       gtk_selection_data_set_text (selection_data, str, -1);
3271       g_free (str);
3272     }
3273 }
3274
3275 static void
3276 primary_clear_cb (GtkClipboard *clipboard,
3277                   gpointer      data)
3278 {
3279   GtkEntry *entry = GTK_ENTRY (data);
3280
3281   gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
3282 }
3283
3284 static void
3285 gtk_entry_update_primary_selection (GtkEntry *entry)
3286 {
3287   static const GtkTargetEntry targets[] = {
3288     { "UTF8_STRING", 0, 0 },
3289     { "STRING", 0, 0 },
3290     { "TEXT",   0, 0 }, 
3291     { "COMPOUND_TEXT", 0, 0 }
3292   };
3293   
3294   GtkClipboard *clipboard;
3295   gint start, end;
3296
3297   if (!GTK_WIDGET_REALIZED (entry))
3298     return;
3299
3300   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
3301   
3302   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
3303     {
3304       if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3305                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
3306         primary_clear_cb (clipboard, entry);
3307     }
3308   else
3309     {
3310       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
3311         gtk_clipboard_clear (clipboard);
3312     }
3313 }
3314
3315 /* Public API
3316  */
3317
3318 GtkWidget*
3319 gtk_entry_new (void)
3320 {
3321   return g_object_new (GTK_TYPE_ENTRY, NULL);
3322 }
3323
3324 /**
3325  * gtk_entry_new_with_max_length:
3326  * @max: the maximum length of the entry, or 0 for no maximum.
3327  *   (other than the maximum length of entries.) The value passed in will
3328  *   be clamped to the range 0-65536.
3329  *
3330  * Creates a new #GtkEntry widget with the given maximum length.
3331  * 
3332  * Note: the existance of this function is inconsistent
3333  * with the rest of the GTK+ API. The normal setup would
3334  * be to just require the user to make an extra call
3335  * to gtk_entry_set_max_length() instead. It is not
3336  * expected that this function will be removed, but
3337  * it would be better practice not to use it.
3338  * 
3339  * Return value: a new #GtkEntry.
3340  **/
3341 GtkWidget*
3342 gtk_entry_new_with_max_length (gint max)
3343 {
3344   GtkEntry *entry;
3345
3346   max = CLAMP (max, 0, MAX_SIZE);
3347
3348   entry = g_object_new (GTK_TYPE_ENTRY, NULL);
3349   entry->text_max_length = max;
3350
3351   return GTK_WIDGET (entry);
3352 }
3353
3354 void
3355 gtk_entry_set_text (GtkEntry    *entry,
3356                     const gchar *text)
3357 {
3358   gint tmp_pos;
3359
3360   g_return_if_fail (GTK_IS_ENTRY (entry));
3361   g_return_if_fail (text != NULL);
3362
3363   /* Actually setting the text will affect the cursor and selection;
3364    * if the contents don't actually change, this will look odd to the user.
3365    */
3366   if (strcmp (entry->text, text) == 0)
3367     return;
3368
3369   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
3370
3371   tmp_pos = 0;
3372   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
3373 }
3374
3375 void
3376 gtk_entry_append_text (GtkEntry *entry,
3377                        const gchar *text)
3378 {
3379   gint tmp_pos;
3380
3381   g_return_if_fail (GTK_IS_ENTRY (entry));
3382   g_return_if_fail (text != NULL);
3383
3384   tmp_pos = entry->text_length;
3385   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
3386 }
3387
3388 void
3389 gtk_entry_prepend_text (GtkEntry *entry,
3390                         const gchar *text)
3391 {
3392   gint tmp_pos;
3393
3394   g_return_if_fail (GTK_IS_ENTRY (entry));
3395   g_return_if_fail (text != NULL);
3396
3397   tmp_pos = 0;
3398   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
3399 }
3400
3401 void
3402 gtk_entry_set_position (GtkEntry *entry,
3403                         gint       position)
3404 {
3405   g_return_if_fail (GTK_IS_ENTRY (entry));
3406
3407   gtk_editable_set_position (GTK_EDITABLE (entry), position);
3408 }
3409
3410 void
3411 gtk_entry_set_visibility (GtkEntry *entry,
3412                           gboolean visible)
3413 {
3414   g_return_if_fail (GTK_IS_ENTRY (entry));
3415
3416   entry->visible = visible ? TRUE : FALSE;
3417   g_object_notify (G_OBJECT (entry), "visibility");
3418   gtk_entry_recompute (entry);
3419 }
3420
3421 /**
3422  * gtk_entry_get_visibility:
3423  * @entry: a #GtkEntry
3424  *
3425  * Retrieves whether the text in @entry is visible. See
3426  * gtk_entry_set_visibility().
3427  *
3428  * Return value: %TRUE if the text is currently visible
3429  **/
3430 gboolean
3431 gtk_entry_get_visibility (GtkEntry *entry)
3432 {
3433   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3434
3435   return entry->visible;
3436 }
3437
3438 /**
3439  * gtk_entry_set_invisible_char:
3440  * @entry: a #GtkEntry
3441  * @ch: a Unicode character
3442  * 
3443  * Sets the character to use in place of the actual text when
3444  * gtk_entry_set_visibility() has been called to set text visibility
3445  * to %FALSE. i.e. this is the character used in "password mode" to
3446  * show the user how many characters have been typed. The default
3447  * invisible char is an asterisk ('*').  If you set the invisible char
3448  * to 0, then the user will get no feedback at all; there will be
3449  * no text on the screen as they type.
3450  * 
3451  **/
3452 void
3453 gtk_entry_set_invisible_char (GtkEntry *entry,
3454                               gunichar  ch)
3455 {
3456   g_return_if_fail (GTK_IS_ENTRY (entry));
3457
3458   if (ch == entry->invisible_char)
3459     return;
3460
3461   entry->invisible_char = ch;
3462   g_object_notify (G_OBJECT (entry), "invisible_char");
3463   gtk_entry_recompute (entry);  
3464 }
3465
3466 /**
3467  * gtk_entry_get_invisible_char:
3468  * @entry: a #GtkEntry
3469  *
3470  * Retrieves the character displayed in place of the real characters
3471  * for entries with visisbility set to false. See gtk_entry_set_invisible_char().
3472  *
3473  * Return value: the current invisible char, or 0, if the entry does not
3474  *               show invisible text at all. 
3475  **/
3476 gunichar
3477 gtk_entry_get_invisible_char (GtkEntry *entry)
3478 {
3479   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3480
3481   return entry->invisible_char;
3482 }
3483
3484 void
3485 gtk_entry_set_editable (GtkEntry *entry,
3486                         gboolean  editable)
3487 {
3488   g_return_if_fail (GTK_IS_ENTRY (entry));
3489
3490   gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
3491 }
3492
3493 /**
3494  * gtk_entry_get_text:
3495  * @entry: a #GtkEntry
3496  *
3497  * Retrieves the contents of the entry widget.
3498  * See also gtk_editable_get_chars().
3499  *
3500  * Return value: a pointer to the contents of the widget as a
3501  *      string.  This string points to internally allocated
3502  *      storage in the widget and must not be freed, modified or
3503  *      stored.
3504  **/
3505 G_CONST_RETURN gchar*
3506 gtk_entry_get_text (GtkEntry *entry)
3507 {
3508   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
3509
3510   return entry->text;
3511 }
3512
3513 void       
3514 gtk_entry_select_region  (GtkEntry       *entry,
3515                           gint            start,
3516                           gint            end)
3517 {
3518   gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
3519 }
3520
3521 /**
3522  * gtk_entry_set_max_length:
3523  * @entry: a #GtkEntry.
3524  * @max: the maximum length of the entry, or 0 for no maximum.
3525  *   (other than the maximum length of entries.) The value passed in will
3526  *   be clamped to the range 0-65536.
3527  * 
3528  * Sets the maximum allowed length of the contents of the widget. If
3529  * the current contents are longer than the given length, then they
3530  * will be truncated to fit.
3531  **/
3532 void
3533 gtk_entry_set_max_length (GtkEntry     *entry,
3534                           gint          max)
3535 {
3536   g_return_if_fail (GTK_IS_ENTRY (entry));
3537
3538   max = CLAMP (max, 0, MAX_SIZE);
3539
3540   if (max > 0 && entry->text_length > max)
3541     gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
3542   
3543   entry->text_max_length = max;
3544   g_object_notify (G_OBJECT (entry), "max_length");
3545 }
3546
3547 /**
3548  * gtk_entry_get_max_length:
3549  * @entry: a #GtkEntry
3550  *
3551  * Retrieves the maximum allowed length of the text in
3552  * @entry. See gtk_entry_set_max_length().
3553  *
3554  * Return value: the maximum allowed number of characters
3555  *               in #GtkEntry, or 0 if there is no maximum.
3556  **/
3557 gint
3558 gtk_entry_get_max_length (GtkEntry *entry)
3559 {
3560   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3561
3562   return entry->text_max_length;
3563 }
3564
3565 /**
3566  * gtk_entry_set_activates_default:
3567  * @entry: a #GtkEntry
3568  * @setting: %TRUE to activate window's default widget on Enter keypress
3569  *
3570  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
3571  * widget for the window containing the entry. This usually means that
3572  * the dialog box containing the entry will be closed, since the default
3573  * widget is usually one of the dialog buttons.
3574  *
3575  * (For experts: if @setting is %TRUE, the entry calls
3576  * gtk_window_activate_default() on the window containing the entry, in
3577  * the default handler for the "activate" signal.)
3578  * 
3579  **/
3580 void
3581 gtk_entry_set_activates_default (GtkEntry *entry,
3582                                  gboolean  setting)
3583 {
3584   g_return_if_fail (GTK_IS_ENTRY (entry));
3585   setting = setting != FALSE;
3586
3587   if (setting != entry->activates_default)
3588     {
3589       entry->activates_default = setting;
3590       g_object_notify (G_OBJECT (entry), "activates_default");
3591     }
3592 }
3593
3594 /**
3595  * gtk_entry_get_activates_default:
3596  * @entry: a #GtkEntry
3597  * 
3598  * Retrieves the value set by gtk_entry_set_activates_default().
3599  * 
3600  * Return value: %TRUE if the entry will activate the default widget
3601  **/
3602 gboolean
3603 gtk_entry_get_activates_default (GtkEntry *entry)
3604 {
3605   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3606
3607   return entry->activates_default;
3608 }
3609
3610 /**
3611  * gtk_entry_set_width_chars:
3612  * @entry: a #GtkEntry
3613  * @n_chars: width in chars
3614  *
3615  * Changes the size request of the entry to be about the right size
3616  * for @n_chars characters. Note that it changes the size
3617  * <emphasis>request</emphasis>, the size can still be affected by
3618  * how you pack the widget into containers. If @n_chars is -1, the
3619  * size reverts to the default entry size.
3620  * 
3621  **/
3622 void
3623 gtk_entry_set_width_chars (GtkEntry *entry,
3624                            gint      n_chars)
3625 {
3626   g_return_if_fail (GTK_IS_ENTRY (entry));
3627
3628   if (entry->width_chars != n_chars)
3629     {
3630       entry->width_chars = n_chars;
3631       g_object_notify (G_OBJECT (entry), "width_chars");
3632       gtk_widget_queue_resize (GTK_WIDGET (entry));
3633     }
3634 }
3635
3636 /**
3637  * gtk_entry_get_width_chars:
3638  * @entry: a #GtkEntry
3639  * 
3640  * Gets the value set by gtk_entry_set_width_chars().
3641  * 
3642  * Return value: number of chars to request space for, or negative if unset
3643  **/
3644 gint
3645 gtk_entry_get_width_chars (GtkEntry *entry)
3646 {
3647   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3648
3649   return entry->width_chars;
3650 }
3651
3652 /**
3653  * gtk_entry_set_has_frame:
3654  * @entry: a #GtkEntry
3655  * @setting: new value
3656  * 
3657  * Sets whether the entry has a beveled frame around it.
3658  **/
3659 void
3660 gtk_entry_set_has_frame (GtkEntry *entry,
3661                          gboolean  setting)
3662 {
3663   g_return_if_fail (GTK_IS_ENTRY (entry));
3664
3665   setting = (setting != FALSE);
3666
3667   if (entry->has_frame == setting)
3668     return;
3669
3670   gtk_widget_queue_resize (GTK_WIDGET (entry));
3671   entry->has_frame = setting;
3672   g_object_notify (G_OBJECT (entry), "has_frame");
3673 }
3674
3675 /**
3676  * gtk_entry_get_has_frame:
3677  * @entry: a #GtkEntry
3678  * 
3679  * Gets the value set by gtk_entry_set_has_frame().
3680  * 
3681  * Return value: whether the entry has a beveled frame
3682  **/
3683 gboolean
3684 gtk_entry_get_has_frame (GtkEntry *entry)
3685 {
3686   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
3687
3688   return entry->has_frame;
3689 }
3690
3691
3692 /**
3693  * gtk_entry_get_layout:
3694  * @entry: a #GtkEntry
3695  * 
3696  * Gets the #PangoLayout used to display the entry.
3697  * The layout is useful to e.g. convert text positions to
3698  * pixel positions, in combination with gtk_entry_get_layout_offsets().
3699  * The returned layout is owned by the entry so need not be
3700  * freed by the caller.
3701  *
3702  * Keep in mind that the layout text may contain a preedit string, so
3703  * gtk_entry_layout_index_to_text_index() and
3704  * gtk_entry_text_index_to_layout_index() are needed to convert byte
3705  * indices in the layout to byte indices in the entry contents.
3706  * 
3707  * Return value: the #PangoLayout for this entry
3708  **/
3709 PangoLayout*
3710 gtk_entry_get_layout (GtkEntry *entry)
3711 {
3712   PangoLayout *layout;
3713   
3714   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
3715
3716   layout = gtk_entry_ensure_layout (entry, TRUE);
3717
3718   return layout;
3719 }
3720
3721
3722 /**
3723  * gtk_entry_layout_index_to_text_index:
3724  * @entry: a #GtkEntry
3725  * @layout_index: byte index into the entry layout text
3726  * 
3727  * Converts from a position in the entry contents (returned
3728  * by gtk_entry_get_text()) to a position in the
3729  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
3730  * with text retrieved via pango_layout_get_text()).
3731  * 
3732  * Return value: byte index into the entry contents
3733  **/
3734 gint
3735 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
3736                                       gint      layout_index)
3737 {
3738   PangoLayout *layout;
3739   const gchar *text;
3740   gint cursor_index;
3741   
3742   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3743
3744   layout = gtk_entry_ensure_layout (entry, TRUE);
3745   text = pango_layout_get_text (layout);
3746   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3747   
3748   if (layout_index >= cursor_index && entry->preedit_length)
3749     {
3750       if (layout_index >= cursor_index + entry->preedit_length)
3751         layout_index -= entry->preedit_length;
3752       else
3753         layout_index = cursor_index;
3754     }
3755
3756   return layout_index;
3757 }
3758
3759 /**
3760  * gtk_entry_text_index_to_layout_index:
3761  * @entry: a #GtkEntry
3762  * @text_index: byte index into the entry contents
3763  * 
3764  * Converts from a position in the entry's #PangoLayout(returned by
3765  * gtk_entry_get_layout()) to a position in the entry contents
3766  * (returned by gtk_entry_get_text()).
3767  * 
3768  * Return value: byte index into the entry layout text
3769  **/
3770 gint
3771 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
3772                                       gint      text_index)
3773 {
3774   PangoLayout *layout;
3775   const gchar *text;
3776   gint cursor_index;
3777   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
3778
3779   layout = gtk_entry_ensure_layout (entry, TRUE);
3780   text = pango_layout_get_text (layout);
3781   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3782   
3783   if (text_index > cursor_index)
3784     text_index += entry->preedit_length;
3785
3786   return text_index;
3787 }
3788
3789 /**
3790  * gtk_entry_get_layout_offsets:
3791  * @entry: a #GtkEntry
3792  * @x: location to store X offset of layout, or %NULL
3793  * @y: location to store Y offset of layout, or %NULL
3794  *
3795  *
3796  * Obtains the position of the #PangoLayout used to render text
3797  * in the entry, in widget coordinates. Useful if you want to line
3798  * up the text in an entry with some other text, e.g. when using the
3799  * entry to implement editable cells in a sheet widget.
3800  *
3801  * Also useful to convert mouse events into coordinates inside the
3802  * #PangoLayout, e.g. to take some action if some part of the entry text
3803  * is clicked.
3804  * 
3805  * Note that as the user scrolls around in the entry the offsets will
3806  * change; you'll need to connect to the "notify::scroll_offset"
3807  * signal to track this. Remember when using the #PangoLayout
3808  * functions you need to convert to and from pixels using
3809  * PANGO_PIXELS() or #PANGO_SCALE.
3810  *
3811  * Keep in mind that the layout text may contain a preedit string, so
3812  * gtk_entry_layout_index_to_text_index() and
3813  * gtk_entry_text_index_to_layout_index() are needed to convert byte
3814  * indices in the layout to byte indices in the entry contents.
3815  * 
3816  **/
3817 void
3818 gtk_entry_get_layout_offsets (GtkEntry *entry,
3819                               gint     *x,
3820                               gint     *y)
3821 {
3822   gint text_area_x, text_area_y;
3823   
3824   g_return_if_fail (GTK_IS_ENTRY (entry));
3825
3826   /* this gets coords relative to text area */
3827   get_layout_position (entry, x, y);
3828
3829   /* convert to widget coords */
3830   get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
3831   
3832   if (x)
3833     *x += text_area_x;
3834
3835   if (y)
3836     *y += text_area_y;
3837 }
3838
3839 /* Quick hack of a popup menu
3840  */
3841 static void
3842 activate_cb (GtkWidget *menuitem,
3843              GtkEntry  *entry)
3844 {
3845   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
3846   g_signal_emit_by_name (entry, signal);
3847 }
3848
3849
3850 static gboolean
3851 gtk_entry_mnemonic_activate (GtkWidget *widget,
3852                              gboolean   group_cycling)
3853 {
3854   gtk_widget_grab_focus (widget);
3855   return TRUE;
3856 }
3857
3858 static void
3859 append_action_signal (GtkEntry     *entry,
3860                       GtkWidget    *menu,
3861                       const gchar  *stock_id,
3862                       const gchar  *signal,
3863                       gboolean      sensitive)
3864 {
3865   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
3866
3867   g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
3868   g_signal_connect (menuitem, "activate",
3869                     G_CALLBACK (activate_cb), entry);
3870
3871   gtk_widget_set_sensitive (menuitem, sensitive);
3872   
3873   gtk_widget_show (menuitem);
3874   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
3875 }
3876         
3877 static void
3878 popup_menu_detach (GtkWidget *attach_widget,
3879                    GtkMenu   *menu)
3880 {
3881   GTK_ENTRY (attach_widget)->popup_menu = NULL;
3882 }
3883
3884 static void
3885 popup_position_func (GtkMenu   *menu,
3886                      gint      *x,
3887                      gint      *y,
3888                      gboolean  *push_in,
3889                      gpointer   user_data)
3890 {
3891   GtkEntry *entry = GTK_ENTRY (user_data);
3892   GtkWidget *widget = GTK_WIDGET (entry);
3893   GdkScreen *screen = gtk_widget_get_screen (widget);
3894   GtkRequisition req;
3895   
3896   g_return_if_fail (GTK_WIDGET_REALIZED (entry));
3897
3898   gdk_window_get_origin (widget->window, x, y);      
3899
3900   gtk_widget_size_request (entry->popup_menu, &req);
3901   
3902   *x += widget->allocation.width / 2;
3903   *y += widget->allocation.height;
3904
3905   *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
3906   *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
3907 }
3908
3909
3910 static void
3911 unichar_chosen_func (const char *text,
3912                      gpointer    data)
3913 {
3914   GtkEntry *entry = GTK_ENTRY (data);
3915
3916   if (entry->editable)
3917     gtk_entry_enter_text (entry, text);
3918 }
3919
3920 typedef struct
3921 {
3922   GtkEntry *entry;
3923   gint button;
3924   guint time;
3925 } PopupInfo;
3926
3927 static void
3928 popup_targets_received (GtkClipboard     *clipboard,
3929                         GtkSelectionData *data,
3930                         gpointer          user_data)
3931 {
3932   PopupInfo *info = user_data;
3933   GtkEntry *entry = info->entry;
3934   
3935   if (GTK_WIDGET_REALIZED (entry))
3936     {
3937       gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
3938       GtkWidget *menuitem;
3939       GtkWidget *submenu;
3940       
3941       if (entry->popup_menu)
3942         gtk_widget_destroy (entry->popup_menu);
3943       
3944       entry->popup_menu = gtk_menu_new ();
3945       
3946       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
3947                                  GTK_WIDGET (entry),
3948                                  popup_menu_detach);
3949       
3950       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
3951                             entry->editable && entry->current_pos != entry->selection_bound);
3952       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
3953                             entry->current_pos != entry->selection_bound);
3954       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
3955                             entry->editable && clipboard_contains_text);
3956       
3957       menuitem = gtk_menu_item_new_with_label (_("Select All"));
3958       g_signal_connect_swapped (menuitem, "activate",
3959                                 G_CALLBACK (gtk_entry_select_all), entry);
3960       gtk_widget_show (menuitem);
3961       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3962       
3963       menuitem = gtk_separator_menu_item_new ();
3964       gtk_widget_show (menuitem);
3965       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3966       
3967       menuitem = gtk_menu_item_new_with_label (_("Input Methods"));
3968       gtk_widget_show (menuitem);
3969       submenu = gtk_menu_new ();
3970       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
3971       
3972       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
3973       
3974       gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
3975                                             GTK_MENU_SHELL (submenu));
3976       
3977       menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
3978       gtk_widget_show (menuitem);
3979       
3980       submenu = gtk_menu_new ();
3981       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
3982       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
3983
3984       _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
3985                                                     unichar_chosen_func,
3986                                                     entry);
3987       if (!entry->editable)
3988         gtk_widget_set_sensitive (menuitem, FALSE);
3989       
3990       g_signal_emit (entry,
3991                      signals[POPULATE_POPUP],
3992                      0,
3993                      entry->popup_menu);
3994   
3995
3996       if (info->button)
3997         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
3998                         NULL, NULL,
3999                         info->button, info->time);
4000       else
4001         {
4002           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
4003                           popup_position_func, entry,
4004                           info->button, info->time);
4005           gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
4006         }
4007     }
4008
4009   g_object_unref (entry);
4010   g_free (info);
4011 }
4012                         
4013 static void
4014 gtk_entry_do_popup (GtkEntry       *entry,
4015                     GdkEventButton *event)
4016 {
4017   PopupInfo *info = g_new (PopupInfo, 1);
4018
4019   /* In order to know what entries we should make sensitive, we
4020    * ask for the current targets of the clipboard, and when
4021    * we get them, then we actually pop up the menu.
4022    */
4023   info->entry = g_object_ref (entry);
4024   
4025   if (event)
4026     {
4027       info->button = event->button;
4028       info->time = event->time;
4029     }
4030   else
4031     {
4032       info->button = 0;
4033       info->time = gtk_get_current_event_time ();
4034     }
4035
4036   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
4037                                   gdk_atom_intern ("TARGETS", FALSE),
4038                                   popup_targets_received,
4039                                   info);
4040 }
4041
4042 static gboolean
4043 gtk_entry_popup_menu (GtkWidget *widget)
4044 {
4045   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
4046   return TRUE;
4047 }
4048
4049 static void
4050 gtk_entry_drag_leave (GtkWidget        *widget,
4051                       GdkDragContext   *context,
4052                       guint             time)
4053 {
4054   GtkEntry *entry = GTK_ENTRY (widget);
4055
4056   entry->dnd_position = -1;
4057   gtk_widget_queue_draw (widget);
4058 }
4059
4060 static gboolean
4061 gtk_entry_drag_drop  (GtkWidget        *widget,
4062                       GdkDragContext   *context,
4063                       gint              x,
4064                       gint              y,
4065                       guint             time)
4066 {
4067   GtkEntry *entry = GTK_ENTRY (widget);
4068   GdkAtom target = GDK_NONE;
4069   
4070   if (entry->editable)
4071     target = gtk_drag_dest_find_target (widget, context, NULL);
4072
4073   if (target != GDK_NONE)
4074     gtk_drag_get_data (widget, context, target, time);
4075   else
4076     gtk_drag_finish (context, FALSE, FALSE, time);
4077   
4078   return TRUE;
4079 }
4080
4081 static gboolean
4082 gtk_entry_drag_motion (GtkWidget        *widget,
4083                        GdkDragContext   *context,
4084                        gint              x,
4085                        gint              y,
4086                        guint             time)
4087 {
4088   GtkEntry *entry = GTK_ENTRY (widget);
4089   GtkWidget *source_widget;
4090   GdkDragAction suggested_action;
4091   gint new_position, old_position;
4092   gint sel1, sel2;
4093   
4094   x -= widget->style->xthickness;
4095   y -= widget->style->ythickness;
4096   
4097   old_position = entry->dnd_position;
4098   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
4099
4100   if (entry->editable &&
4101       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
4102     {
4103       source_widget = gtk_drag_get_source_widget (context);
4104       suggested_action = context->suggested_action;
4105
4106       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
4107           new_position < sel1 || new_position > sel2)
4108         {
4109           if (source_widget == widget)
4110             {
4111               /* Default to MOVE, unless the user has
4112                * pressed ctrl or alt to affect available actions
4113                */
4114               if ((context->actions & GDK_ACTION_MOVE) != 0)
4115                 suggested_action = GDK_ACTION_MOVE;
4116             }
4117               
4118           entry->dnd_position = new_position;
4119         }
4120       else
4121         {
4122           if (source_widget == widget)
4123             suggested_action = 0;       /* Can't drop in selection where drag started */
4124           
4125           entry->dnd_position = -1;
4126         }
4127     }
4128   else
4129     {
4130       /* Entry not editable, or no text */
4131       suggested_action = 0;
4132       entry->dnd_position = -1;
4133     }
4134   
4135   gdk_drag_status (context, suggested_action, time);
4136   
4137   if (entry->dnd_position != old_position)
4138     gtk_widget_queue_draw (widget);
4139
4140   return TRUE;
4141 }
4142
4143 static void
4144 gtk_entry_drag_data_received (GtkWidget        *widget,
4145                               GdkDragContext   *context,
4146                               gint              x,
4147                               gint              y,
4148                               GtkSelectionData *selection_data,
4149                               guint             info,
4150                               guint             time)
4151 {
4152   GtkEntry *entry = GTK_ENTRY (widget);
4153   GtkEditable *editable = GTK_EDITABLE (widget);
4154   gchar *str;
4155
4156   str = gtk_selection_data_get_text (selection_data);
4157
4158   if (str && entry->editable)
4159     {
4160       gint new_position;
4161       gint sel1, sel2;
4162
4163       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
4164
4165       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
4166           new_position < sel1 || new_position > sel2)
4167         {
4168           gtk_editable_insert_text (editable, str, -1, &new_position);
4169         }
4170       else
4171         {
4172           /* Replacing selection */
4173           gtk_editable_delete_text (editable, sel1, sel2);
4174           gtk_editable_insert_text (editable, str, -1, &sel1);
4175         }
4176       
4177       g_free (str);
4178       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
4179     }
4180   else
4181     {
4182       /* Drag and drop didn't happen! */
4183       gtk_drag_finish (context, FALSE, FALSE, time);
4184     }
4185 }
4186
4187 static void
4188 gtk_entry_drag_data_get (GtkWidget        *widget,
4189                          GdkDragContext   *context,
4190                          GtkSelectionData *selection_data,
4191                          guint             info,
4192                          guint             time)
4193 {
4194   gint sel_start, sel_end;
4195
4196   GtkEditable *editable = GTK_EDITABLE (widget);
4197   
4198   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
4199     {
4200       gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
4201
4202       gtk_selection_data_set_text (selection_data, str, -1);
4203       
4204       g_free (str);
4205     }
4206
4207 }
4208
4209 static void
4210 gtk_entry_drag_data_delete (GtkWidget      *widget,
4211                             GdkDragContext *context)
4212 {
4213   gint sel_start, sel_end;
4214
4215   GtkEditable *editable = GTK_EDITABLE (widget);
4216   
4217   if (GTK_ENTRY (widget)->editable &&
4218       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
4219     gtk_editable_delete_text (editable, sel_start, sel_end);
4220 }
4221
4222 /* We display the cursor when
4223  *
4224  *  - the selection is empty, AND
4225  *  - the widget has focus
4226  */
4227
4228 #define CURSOR_ON_MULTIPLIER 0.66
4229 #define CURSOR_OFF_MULTIPLIER 0.34
4230 #define CURSOR_PEND_MULTIPLIER 1.0
4231
4232 static gboolean
4233 cursor_blinks (GtkEntry *entry)
4234 {
4235   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
4236   gboolean blink;
4237
4238   if (GTK_WIDGET_HAS_FOCUS (entry) &&
4239       entry->selection_bound == entry->current_pos)
4240     {
4241       g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4242       return blink;
4243     }
4244   else
4245     return FALSE;
4246 }
4247
4248 static gint
4249 get_cursor_time (GtkEntry *entry)
4250 {
4251   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
4252   gint time;
4253
4254   g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4255
4256   return time;
4257 }
4258
4259 static void
4260 show_cursor (GtkEntry *entry)
4261 {
4262   if (!entry->cursor_visible)
4263     {
4264       entry->cursor_visible = TRUE;
4265
4266       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
4267         gtk_widget_queue_draw (GTK_WIDGET (entry));
4268     }
4269 }
4270
4271 static void
4272 hide_cursor (GtkEntry *entry)
4273 {
4274   if (entry->cursor_visible)
4275     {
4276       entry->cursor_visible = FALSE;
4277
4278       if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
4279         gtk_widget_queue_draw (GTK_WIDGET (entry));
4280     }
4281 }
4282
4283 /*
4284  * Blink!
4285  */
4286 static gint
4287 blink_cb (gpointer data)
4288 {
4289   GtkEntry *entry;
4290
4291   GDK_THREADS_ENTER ();
4292
4293   entry = GTK_ENTRY (data);
4294
4295   if (!GTK_WIDGET_HAS_FOCUS (entry))
4296     {
4297       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
4298                  "connect a handler to this signal, it must return\n"
4299                  "FALSE so the entry gets the event as well");
4300     }
4301   
4302   g_assert (GTK_WIDGET_HAS_FOCUS (entry));
4303   g_assert (entry->selection_bound == entry->current_pos);
4304
4305   if (entry->cursor_visible)
4306     {
4307       hide_cursor (entry);
4308       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
4309                                               blink_cb,
4310                                               entry);
4311     }
4312   else
4313     {
4314       show_cursor (entry);
4315       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
4316                                               blink_cb,
4317                                               entry);
4318     }
4319
4320   GDK_THREADS_LEAVE ();
4321
4322   /* Remove ourselves */
4323   return FALSE;
4324 }
4325
4326 static void
4327 gtk_entry_check_cursor_blink (GtkEntry *entry)
4328 {
4329   if (cursor_blinks (entry))
4330     {
4331       if (!entry->blink_timeout)
4332         {
4333           entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
4334                                                   blink_cb,
4335                                                   entry);
4336           show_cursor (entry);
4337         }
4338     }
4339   else
4340     {
4341       if (entry->blink_timeout)  
4342         { 
4343           gtk_timeout_remove (entry->blink_timeout);
4344           entry->blink_timeout = 0;
4345         }
4346       
4347       entry->cursor_visible = TRUE;
4348     }
4349   
4350 }
4351
4352 static void
4353 gtk_entry_pend_cursor_blink (GtkEntry *entry)
4354 {
4355   if (cursor_blinks (entry))
4356     {
4357       if (entry->blink_timeout != 0)
4358         gtk_timeout_remove (entry->blink_timeout);
4359       
4360       entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
4361                                               blink_cb,
4362                                               entry);
4363       show_cursor (entry);
4364     }
4365 }