]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
entry: No, you can not haz ununused variables
[~andy/gtk] / gtk / gtkentry.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  * Copyright (C) 2004-2006 Christian Hammond
5  * Copyright (C) 2008 Cody Russell
6  * Copyright (C) 2008 Red Hat, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GTK+ Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29  */
30
31 #include "config.h"
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkalignment.h"
38 #include "gtkbindings.h"
39 #include "gtkcelleditable.h"
40 #include "gtkclipboard.h"
41 #include "gtkdnd.h"
42 #include "gtkentry.h"
43 #include "gtkentrybuffer.h"
44 #include "gtkimagemenuitem.h"
45 #include "gtkimcontextsimple.h"
46 #include "gtkimmulticontext.h"
47 #include "gtkintl.h"
48 #include "gtklabel.h"
49 #include "gtkmain.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmenu.h"
52 #include "gtkmenuitem.h"
53 #include "gtkseparatormenuitem.h"
54 #include "gtkselection.h"
55 #include "gtksettings.h"
56 #include "gtkspinbutton.h"
57 #include "gtkstock.h"
58 #include "gtktextutil.h"
59 #include "gtkwindow.h"
60 #include "gtktreeview.h"
61 #include "gtktreeselection.h"
62 #include "gtkprivate.h"
63 #include "gtkentryprivate.h"
64 #include "gtkcelllayout.h"
65 #include "gtktooltip.h"
66 #include "gtkiconfactory.h"
67 #include "gtkicontheme.h"
68
69 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
70
71 #define MIN_ENTRY_WIDTH  150
72 #define DRAW_TIMEOUT     20
73 #define COMPLETION_TIMEOUT 300
74 #define PASSWORD_HINT_MAX 8
75
76 #define MAX_ICONS 2
77
78 #define IS_VALID_ICON_POSITION(pos)               \
79   ((pos) == GTK_ENTRY_ICON_PRIMARY ||                   \
80    (pos) == GTK_ENTRY_ICON_SECONDARY)
81
82 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
83 static GQuark          quark_inner_border   = 0;
84 static GQuark          quark_password_hint  = 0;
85 static GQuark          quark_cursor_hadjustment = 0;
86 static GQuark          quark_capslock_feedback = 0;
87
88 typedef struct _GtkEntryPrivate GtkEntryPrivate;
89
90 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
91
92 typedef struct
93 {
94   GdkWindow *window;
95   gchar *tooltip;
96   guint insensitive    : 1;
97   guint nonactivatable : 1;
98   guint prelight       : 1;
99   guint in_drag        : 1;
100   guint pressed        : 1;
101
102   GtkImageType  storage_type;
103   GdkPixbuf    *pixbuf;
104   gchar        *stock_id;
105   gchar        *icon_name;
106   GIcon        *gicon;
107
108   GtkTargetList *target_list;
109   GdkDragAction actions;
110 } EntryIconInfo;
111
112 struct _GtkEntryPrivate 
113 {
114   GtkEntryBuffer* buffer;
115
116   gfloat xalign;
117   gint insert_pos;
118   guint blink_time;  /* time in msec the cursor has blinked since last user event */
119   guint interior_focus          : 1;
120   guint real_changed            : 1;
121   guint invisible_char_set      : 1;
122   guint caps_lock_warning       : 1;
123   guint caps_lock_warning_shown : 1;
124   guint change_count            : 8;
125   guint progress_pulse_mode     : 1;
126   guint progress_pulse_way_back : 1;
127
128   gint focus_width;
129   GtkShadowType shadow_type;
130
131   gdouble progress_fraction;
132   gdouble progress_pulse_fraction;
133   gdouble progress_pulse_current;
134
135   EntryIconInfo *icons[MAX_ICONS];
136   gint icon_margin;
137   gint start_x;
138   gint start_y;
139
140   gchar *im_module;
141
142   GdkDevice *completion_device;
143 };
144
145 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
146
147 struct _GtkEntryPasswordHint
148 {
149   gint position;      /* Position (in text) of the last password hint */
150   guint source_id;    /* Timeout source id */
151 };
152
153 typedef struct _GtkEntryCapslockFeedback GtkEntryCapslockFeedback;
154
155 struct _GtkEntryCapslockFeedback
156 {
157   GtkWidget *entry;
158   GtkWidget *window;
159   GtkWidget *label;
160 };
161
162 enum {
163   ACTIVATE,
164   POPULATE_POPUP,
165   MOVE_CURSOR,
166   INSERT_AT_CURSOR,
167   DELETE_FROM_CURSOR,
168   BACKSPACE,
169   CUT_CLIPBOARD,
170   COPY_CLIPBOARD,
171   PASTE_CLIPBOARD,
172   TOGGLE_OVERWRITE,
173   ICON_PRESS,
174   ICON_RELEASE,
175   PREEDIT_CHANGED,
176   LAST_SIGNAL
177 };
178
179 enum {
180   PROP_0,
181   PROP_BUFFER,
182   PROP_CURSOR_POSITION,
183   PROP_SELECTION_BOUND,
184   PROP_EDITABLE,
185   PROP_MAX_LENGTH,
186   PROP_VISIBILITY,
187   PROP_HAS_FRAME,
188   PROP_INNER_BORDER,
189   PROP_INVISIBLE_CHAR,
190   PROP_ACTIVATES_DEFAULT,
191   PROP_WIDTH_CHARS,
192   PROP_SCROLL_OFFSET,
193   PROP_TEXT,
194   PROP_XALIGN,
195   PROP_TRUNCATE_MULTILINE,
196   PROP_SHADOW_TYPE,
197   PROP_OVERWRITE_MODE,
198   PROP_TEXT_LENGTH,
199   PROP_INVISIBLE_CHAR_SET,
200   PROP_CAPS_LOCK_WARNING,
201   PROP_PROGRESS_FRACTION,
202   PROP_PROGRESS_PULSE_STEP,
203   PROP_PIXBUF_PRIMARY,
204   PROP_PIXBUF_SECONDARY,
205   PROP_STOCK_PRIMARY,
206   PROP_STOCK_SECONDARY,
207   PROP_ICON_NAME_PRIMARY,
208   PROP_ICON_NAME_SECONDARY,
209   PROP_GICON_PRIMARY,
210   PROP_GICON_SECONDARY,
211   PROP_STORAGE_TYPE_PRIMARY,
212   PROP_STORAGE_TYPE_SECONDARY,
213   PROP_ACTIVATABLE_PRIMARY,
214   PROP_ACTIVATABLE_SECONDARY,
215   PROP_SENSITIVE_PRIMARY,
216   PROP_SENSITIVE_SECONDARY,
217   PROP_TOOLTIP_TEXT_PRIMARY,
218   PROP_TOOLTIP_TEXT_SECONDARY,
219   PROP_TOOLTIP_MARKUP_PRIMARY,
220   PROP_TOOLTIP_MARKUP_SECONDARY,
221   PROP_IM_MODULE,
222   PROP_EDITING_CANCELED
223 };
224
225 static guint signals[LAST_SIGNAL] = { 0 };
226
227 typedef enum {
228   CURSOR_STANDARD,
229   CURSOR_DND
230 } CursorType;
231
232 typedef enum
233 {
234   DISPLAY_NORMAL,       /* The entry text is being shown */
235   DISPLAY_INVISIBLE,    /* In invisible mode, text replaced by (eg) bullets */
236   DISPLAY_BLANK         /* In invisible mode, nothing shown at all */
237 } DisplayMode;
238
239 /* GObject methods
240  */
241 static void   gtk_entry_editable_init        (GtkEditableInterface *iface);
242 static void   gtk_entry_cell_editable_init   (GtkCellEditableIface *iface);
243 static void   gtk_entry_set_property         (GObject          *object,
244                                               guint             prop_id,
245                                               const GValue     *value,
246                                               GParamSpec       *pspec);
247 static void   gtk_entry_get_property         (GObject          *object,
248                                               guint             prop_id,
249                                               GValue           *value,
250                                               GParamSpec       *pspec);
251 static void   gtk_entry_finalize             (GObject          *object);
252 static void   gtk_entry_dispose              (GObject          *object);
253
254 /* GtkWidget methods
255  */
256 static void   gtk_entry_destroy              (GtkWidget        *widget);
257 static void   gtk_entry_realize              (GtkWidget        *widget);
258 static void   gtk_entry_unrealize            (GtkWidget        *widget);
259 static void   gtk_entry_map                  (GtkWidget        *widget);
260 static void   gtk_entry_unmap                (GtkWidget        *widget);
261 static void   gtk_entry_size_request         (GtkWidget        *widget,
262                                               GtkRequisition   *requisition);
263 static void   gtk_entry_size_allocate        (GtkWidget        *widget,
264                                               GtkAllocation    *allocation);
265 static void   gtk_entry_draw_frame           (GtkWidget        *widget,
266                                               cairo_t          *cr);
267 static void   gtk_entry_draw_progress        (GtkWidget        *widget,
268                                               cairo_t          *cr);
269 static gint   gtk_entry_draw                 (GtkWidget        *widget,
270                                               cairo_t          *cr);
271 static gint   gtk_entry_button_press         (GtkWidget        *widget,
272                                               GdkEventButton   *event);
273 static gint   gtk_entry_button_release       (GtkWidget        *widget,
274                                               GdkEventButton   *event);
275 static gint   gtk_entry_enter_notify         (GtkWidget        *widget,
276                                               GdkEventCrossing *event);
277 static gint   gtk_entry_leave_notify         (GtkWidget        *widget,
278                                               GdkEventCrossing *event);
279 static gint   gtk_entry_motion_notify        (GtkWidget        *widget,
280                                               GdkEventMotion   *event);
281 static gint   gtk_entry_key_press            (GtkWidget        *widget,
282                                               GdkEventKey      *event);
283 static gint   gtk_entry_key_release          (GtkWidget        *widget,
284                                               GdkEventKey      *event);
285 static gint   gtk_entry_focus_in             (GtkWidget        *widget,
286                                               GdkEventFocus    *event);
287 static gint   gtk_entry_focus_out            (GtkWidget        *widget,
288                                               GdkEventFocus    *event);
289 static void   gtk_entry_grab_focus           (GtkWidget        *widget);
290 static void   gtk_entry_style_set            (GtkWidget        *widget,
291                                               GtkStyle         *previous_style);
292 static gboolean gtk_entry_query_tooltip      (GtkWidget        *widget,
293                                               gint              x,
294                                               gint              y,
295                                               gboolean          keyboard_tip,
296                                               GtkTooltip       *tooltip);
297 static void   gtk_entry_direction_changed    (GtkWidget        *widget,
298                                               GtkTextDirection  previous_dir);
299 static void   gtk_entry_state_changed        (GtkWidget        *widget,
300                                               GtkStateType      previous_state);
301 static void   gtk_entry_screen_changed       (GtkWidget        *widget,
302                                               GdkScreen        *old_screen);
303
304 static gboolean gtk_entry_drag_drop          (GtkWidget        *widget,
305                                               GdkDragContext   *context,
306                                               gint              x,
307                                               gint              y,
308                                               guint             time);
309 static gboolean gtk_entry_drag_motion        (GtkWidget        *widget,
310                                               GdkDragContext   *context,
311                                               gint              x,
312                                               gint              y,
313                                               guint             time);
314 static void     gtk_entry_drag_leave         (GtkWidget        *widget,
315                                               GdkDragContext   *context,
316                                               guint             time);
317 static void     gtk_entry_drag_data_received (GtkWidget        *widget,
318                                               GdkDragContext   *context,
319                                               gint              x,
320                                               gint              y,
321                                               GtkSelectionData *selection_data,
322                                               guint             info,
323                                               guint             time);
324 static void     gtk_entry_drag_data_get      (GtkWidget        *widget,
325                                               GdkDragContext   *context,
326                                               GtkSelectionData *selection_data,
327                                               guint             info,
328                                               guint             time);
329 static void     gtk_entry_drag_data_delete   (GtkWidget        *widget,
330                                               GdkDragContext   *context);
331 static void     gtk_entry_drag_begin         (GtkWidget        *widget,
332                                               GdkDragContext   *context);
333 static void     gtk_entry_drag_end           (GtkWidget        *widget,
334                                               GdkDragContext   *context);
335
336
337 /* GtkEditable method implementations
338  */
339 static void     gtk_entry_insert_text          (GtkEditable *editable,
340                                                 const gchar *new_text,
341                                                 gint         new_text_length,
342                                                 gint        *position);
343 static void     gtk_entry_delete_text          (GtkEditable *editable,
344                                                 gint         start_pos,
345                                                 gint         end_pos);
346 static gchar *  gtk_entry_get_chars            (GtkEditable *editable,
347                                                 gint         start_pos,
348                                                 gint         end_pos);
349 static void     gtk_entry_real_set_position    (GtkEditable *editable,
350                                                 gint         position);
351 static gint     gtk_entry_get_position         (GtkEditable *editable);
352 static void     gtk_entry_set_selection_bounds (GtkEditable *editable,
353                                                 gint         start,
354                                                 gint         end);
355 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
356                                                 gint        *start,
357                                                 gint        *end);
358
359 /* GtkCellEditable method implementations
360  */
361 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
362                                      GdkEvent        *event);
363
364 /* Default signal handlers
365  */
366 static void gtk_entry_real_insert_text   (GtkEditable     *editable,
367                                           const gchar     *new_text,
368                                           gint             new_text_length,
369                                           gint            *position);
370 static void gtk_entry_real_delete_text   (GtkEditable     *editable,
371                                           gint             start_pos,
372                                           gint             end_pos);
373 static void gtk_entry_move_cursor        (GtkEntry        *entry,
374                                           GtkMovementStep  step,
375                                           gint             count,
376                                           gboolean         extend_selection);
377 static void gtk_entry_insert_at_cursor   (GtkEntry        *entry,
378                                           const gchar     *str);
379 static void gtk_entry_delete_from_cursor (GtkEntry        *entry,
380                                           GtkDeleteType    type,
381                                           gint             count);
382 static void gtk_entry_backspace          (GtkEntry        *entry);
383 static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
384 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
385 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
386 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
387 static void gtk_entry_select_all         (GtkEntry        *entry);
388 static void gtk_entry_real_activate      (GtkEntry        *entry);
389 static gboolean gtk_entry_popup_menu     (GtkWidget       *widget);
390
391 static void keymap_direction_changed     (GdkKeymap       *keymap,
392                                           GtkEntry        *entry);
393 static void keymap_state_changed         (GdkKeymap       *keymap,
394                                           GtkEntry        *entry);
395 static void remove_capslock_feedback     (GtkEntry        *entry);
396
397 /* IM Context Callbacks
398  */
399 static void     gtk_entry_commit_cb               (GtkIMContext *context,
400                                                    const gchar  *str,
401                                                    GtkEntry     *entry);
402 static void     gtk_entry_preedit_changed_cb      (GtkIMContext *context,
403                                                    GtkEntry     *entry);
404 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
405                                                    GtkEntry     *entry);
406 static gboolean gtk_entry_delete_surrounding_cb   (GtkIMContext *context,
407                                                    gint          offset,
408                                                    gint          n_chars,
409                                                    GtkEntry     *entry);
410
411 /* Internal routines
412  */
413 static void         gtk_entry_enter_text               (GtkEntry       *entry,
414                                                         const gchar    *str);
415 static void         gtk_entry_set_positions            (GtkEntry       *entry,
416                                                         gint            current_pos,
417                                                         gint            selection_bound);
418 static void         gtk_entry_draw_text                (GtkEntry       *entry,
419                                                         cairo_t        *cr);
420 static void         gtk_entry_draw_cursor              (GtkEntry       *entry,
421                                                         cairo_t        *cr,
422                                                         CursorType      type);
423 static PangoLayout *gtk_entry_ensure_layout            (GtkEntry       *entry,
424                                                         gboolean        include_preedit);
425 static void         gtk_entry_reset_layout             (GtkEntry       *entry);
426 static void         gtk_entry_queue_draw               (GtkEntry       *entry);
427 static void         gtk_entry_recompute                (GtkEntry       *entry);
428 static gint         gtk_entry_find_position            (GtkEntry       *entry,
429                                                         gint            x);
430 static void         gtk_entry_get_cursor_locations     (GtkEntry       *entry,
431                                                         CursorType      type,
432                                                         gint           *strong_x,
433                                                         gint           *weak_x);
434 static void         gtk_entry_adjust_scroll            (GtkEntry       *entry);
435 static gint         gtk_entry_move_visually            (GtkEntry       *editable,
436                                                         gint            start,
437                                                         gint            count);
438 static gint         gtk_entry_move_logically           (GtkEntry       *entry,
439                                                         gint            start,
440                                                         gint            count);
441 static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
442                                                         gint            start,
443                                                         gboolean        allow_whitespace);
444 static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
445                                                         gint            start,
446                                                         gboolean        allow_whitespace);
447 static void         gtk_entry_delete_whitespace        (GtkEntry       *entry);
448 static void         gtk_entry_select_word              (GtkEntry       *entry);
449 static void         gtk_entry_select_line              (GtkEntry       *entry);
450 static void         gtk_entry_paste                    (GtkEntry       *entry,
451                                                         GdkAtom         selection);
452 static void         gtk_entry_update_primary_selection (GtkEntry       *entry);
453 static void         gtk_entry_do_popup                 (GtkEntry       *entry,
454                                                         GdkEventButton *event);
455 static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
456                                                         gboolean        group_cycling);
457 static void         gtk_entry_check_cursor_blink       (GtkEntry       *entry);
458 static void         gtk_entry_pend_cursor_blink        (GtkEntry       *entry);
459 static void         gtk_entry_reset_blink_time         (GtkEntry       *entry);
460 static void         gtk_entry_get_text_area_size       (GtkEntry       *entry,
461                                                         gint           *x,
462                                                         gint           *y,
463                                                         gint           *width,
464                                                         gint           *height);
465 static void         get_text_area_size                 (GtkEntry       *entry,
466                                                         gint           *x,
467                                                         gint           *y,
468                                                         gint           *width,
469                                                         gint           *height);
470 static void         get_frame_size                     (GtkEntry       *entry,
471                                                         gint           *x,
472                                                         gint           *y,
473                                                         gint           *width,
474                                                         gint           *height);
475 static void         gtk_entry_move_adjustments         (GtkEntry             *entry);
476 static void         gtk_entry_ensure_pixbuf            (GtkEntry             *entry,
477                                                         GtkEntryIconPosition  icon_pos);
478
479
480 /* Completion */
481 static gint         gtk_entry_completion_timeout       (gpointer            data);
482 static gboolean     gtk_entry_completion_key_press     (GtkWidget          *widget,
483                                                         GdkEventKey        *event,
484                                                         gpointer            user_data);
485 static void         gtk_entry_completion_changed       (GtkWidget          *entry,
486                                                         gpointer            user_data);
487 static gboolean     check_completion_callback          (GtkEntryCompletion *completion);
488 static void         clear_completion_callback          (GtkEntry           *entry,
489                                                         GParamSpec         *pspec);
490 static gboolean     accept_completion_callback         (GtkEntry           *entry);
491 static void         completion_insert_text_callback    (GtkEntry           *entry,
492                                                         const gchar        *text,
493                                                         gint                length,
494                                                         gint                position,
495                                                         GtkEntryCompletion *completion);
496 static void         completion_changed                 (GtkEntryCompletion *completion,
497                                                         GParamSpec         *pspec,
498                                                         gpointer            data);
499 static void         disconnect_completion_signals      (GtkEntry           *entry,
500                                                         GtkEntryCompletion *completion);
501 static void         connect_completion_signals         (GtkEntry           *entry,
502                                                         GtkEntryCompletion *completion);
503
504 static void         begin_change                       (GtkEntry *entry);
505 static void         end_change                         (GtkEntry *entry);
506 static void         emit_changed                       (GtkEntry *entry);
507
508
509 static void         buffer_inserted_text               (GtkEntryBuffer *buffer, 
510                                                         guint           position,
511                                                         const gchar    *chars,
512                                                         guint           n_chars,
513                                                         GtkEntry       *entry);
514 static void         buffer_deleted_text                (GtkEntryBuffer *buffer, 
515                                                         guint           position,
516                                                         guint           n_chars,
517                                                         GtkEntry       *entry);
518 static void         buffer_notify_text                 (GtkEntryBuffer *buffer, 
519                                                         GParamSpec     *spec,
520                                                         GtkEntry       *entry);
521 static void         buffer_notify_length               (GtkEntryBuffer *buffer, 
522                                                         GParamSpec     *spec,
523                                                         GtkEntry       *entry);
524 static void         buffer_notify_max_length           (GtkEntryBuffer *buffer, 
525                                                         GParamSpec     *spec,
526                                                         GtkEntry       *entry);
527 static void         buffer_connect_signals             (GtkEntry       *entry);
528 static void         buffer_disconnect_signals          (GtkEntry       *entry);
529 static GtkEntryBuffer *get_buffer                      (GtkEntry       *entry);
530
531
532 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
533                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
534                                                 gtk_entry_editable_init)
535                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
536                                                 gtk_entry_cell_editable_init))
537
538 static void
539 add_move_binding (GtkBindingSet  *binding_set,
540                   guint           keyval,
541                   guint           modmask,
542                   GtkMovementStep step,
543                   gint            count)
544 {
545   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
546   
547   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
548                                 "move-cursor", 3,
549                                 G_TYPE_ENUM, step,
550                                 G_TYPE_INT, count,
551                                 G_TYPE_BOOLEAN, FALSE);
552
553   /* Selection-extending version */
554   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
555                                 "move-cursor", 3,
556                                 G_TYPE_ENUM, step,
557                                 G_TYPE_INT, count,
558                                 G_TYPE_BOOLEAN, TRUE);
559 }
560
561 static void
562 gtk_entry_class_init (GtkEntryClass *class)
563 {
564   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
565   GtkWidgetClass *widget_class;
566   GtkBindingSet *binding_set;
567
568   widget_class = (GtkWidgetClass*) class;
569
570   gobject_class->dispose = gtk_entry_dispose;
571   gobject_class->finalize = gtk_entry_finalize;
572   gobject_class->set_property = gtk_entry_set_property;
573   gobject_class->get_property = gtk_entry_get_property;
574
575   widget_class->destroy = gtk_entry_destroy;
576   widget_class->map = gtk_entry_map;
577   widget_class->unmap = gtk_entry_unmap;
578   widget_class->realize = gtk_entry_realize;
579   widget_class->unrealize = gtk_entry_unrealize;
580   widget_class->size_request = gtk_entry_size_request;
581   widget_class->size_allocate = gtk_entry_size_allocate;
582   widget_class->draw = gtk_entry_draw;
583   widget_class->enter_notify_event = gtk_entry_enter_notify;
584   widget_class->leave_notify_event = gtk_entry_leave_notify;
585   widget_class->button_press_event = gtk_entry_button_press;
586   widget_class->button_release_event = gtk_entry_button_release;
587   widget_class->motion_notify_event = gtk_entry_motion_notify;
588   widget_class->key_press_event = gtk_entry_key_press;
589   widget_class->key_release_event = gtk_entry_key_release;
590   widget_class->focus_in_event = gtk_entry_focus_in;
591   widget_class->focus_out_event = gtk_entry_focus_out;
592   widget_class->grab_focus = gtk_entry_grab_focus;
593   widget_class->style_set = gtk_entry_style_set;
594   widget_class->query_tooltip = gtk_entry_query_tooltip;
595   widget_class->drag_begin = gtk_entry_drag_begin;
596   widget_class->drag_end = gtk_entry_drag_end;
597   widget_class->direction_changed = gtk_entry_direction_changed;
598   widget_class->state_changed = gtk_entry_state_changed;
599   widget_class->screen_changed = gtk_entry_screen_changed;
600   widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
601
602   widget_class->drag_drop = gtk_entry_drag_drop;
603   widget_class->drag_motion = gtk_entry_drag_motion;
604   widget_class->drag_leave = gtk_entry_drag_leave;
605   widget_class->drag_data_received = gtk_entry_drag_data_received;
606   widget_class->drag_data_get = gtk_entry_drag_data_get;
607   widget_class->drag_data_delete = gtk_entry_drag_data_delete;
608
609   widget_class->popup_menu = gtk_entry_popup_menu;
610
611   class->move_cursor = gtk_entry_move_cursor;
612   class->insert_at_cursor = gtk_entry_insert_at_cursor;
613   class->delete_from_cursor = gtk_entry_delete_from_cursor;
614   class->backspace = gtk_entry_backspace;
615   class->cut_clipboard = gtk_entry_cut_clipboard;
616   class->copy_clipboard = gtk_entry_copy_clipboard;
617   class->paste_clipboard = gtk_entry_paste_clipboard;
618   class->toggle_overwrite = gtk_entry_toggle_overwrite;
619   class->activate = gtk_entry_real_activate;
620   class->get_text_area_size = gtk_entry_get_text_area_size;
621   
622   quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
623   quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
624   quark_cursor_hadjustment = g_quark_from_static_string ("gtk-hadjustment");
625   quark_capslock_feedback = g_quark_from_static_string ("gtk-entry-capslock-feedback");
626
627   g_object_class_override_property (gobject_class,
628                                     PROP_EDITING_CANCELED,
629                                     "editing-canceled");
630
631   g_object_class_install_property (gobject_class,
632                                    PROP_BUFFER,
633                                    g_param_spec_object ("buffer",
634                                                         P_("Text Buffer"),
635                                                         P_("Text buffer object which actually stores entry text"),
636                                                         GTK_TYPE_ENTRY_BUFFER,
637                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
638
639   g_object_class_install_property (gobject_class,
640                                    PROP_CURSOR_POSITION,
641                                    g_param_spec_int ("cursor-position",
642                                                      P_("Cursor Position"),
643                                                      P_("The current position of the insertion cursor in chars"),
644                                                      0,
645                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
646                                                      0,
647                                                      GTK_PARAM_READABLE));
648   
649   g_object_class_install_property (gobject_class,
650                                    PROP_SELECTION_BOUND,
651                                    g_param_spec_int ("selection-bound",
652                                                      P_("Selection Bound"),
653                                                      P_("The position of the opposite end of the selection from the cursor in chars"),
654                                                      0,
655                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
656                                                      0,
657                                                      GTK_PARAM_READABLE));
658   
659   g_object_class_install_property (gobject_class,
660                                    PROP_EDITABLE,
661                                    g_param_spec_boolean ("editable",
662                                                          P_("Editable"),
663                                                          P_("Whether the entry contents can be edited"),
664                                                          TRUE,
665                                                          GTK_PARAM_READWRITE));
666   
667   g_object_class_install_property (gobject_class,
668                                    PROP_MAX_LENGTH,
669                                    g_param_spec_int ("max-length",
670                                                      P_("Maximum length"),
671                                                      P_("Maximum number of characters for this entry. Zero if no maximum"),
672                                                      0,
673                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
674                                                      0,
675                                                      GTK_PARAM_READWRITE));
676   g_object_class_install_property (gobject_class,
677                                    PROP_VISIBILITY,
678                                    g_param_spec_boolean ("visibility",
679                                                          P_("Visibility"),
680                                                          P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
681                                                          TRUE,
682                                                          GTK_PARAM_READWRITE));
683
684   g_object_class_install_property (gobject_class,
685                                    PROP_HAS_FRAME,
686                                    g_param_spec_boolean ("has-frame",
687                                                          P_("Has Frame"),
688                                                          P_("FALSE removes outside bevel from entry"),
689                                                          TRUE,
690                                                          GTK_PARAM_READWRITE));
691
692   g_object_class_install_property (gobject_class,
693                                    PROP_INNER_BORDER,
694                                    g_param_spec_boxed ("inner-border",
695                                                        P_("Inner Border"),
696                                                        P_("Border between text and frame. Overrides the inner-border style property"),
697                                                        GTK_TYPE_BORDER,
698                                                        GTK_PARAM_READWRITE));
699
700   g_object_class_install_property (gobject_class,
701                                    PROP_INVISIBLE_CHAR,
702                                    g_param_spec_unichar ("invisible-char",
703                                                          P_("Invisible character"),
704                                                          P_("The character to use when masking entry contents (in \"password mode\")"),
705                                                          '*',
706                                                          GTK_PARAM_READWRITE));
707
708   g_object_class_install_property (gobject_class,
709                                    PROP_ACTIVATES_DEFAULT,
710                                    g_param_spec_boolean ("activates-default",
711                                                          P_("Activates default"),
712                                                          P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
713                                                          FALSE,
714                                                          GTK_PARAM_READWRITE));
715   g_object_class_install_property (gobject_class,
716                                    PROP_WIDTH_CHARS,
717                                    g_param_spec_int ("width-chars",
718                                                      P_("Width in chars"),
719                                                      P_("Number of characters to leave space for in the entry"),
720                                                      -1,
721                                                      G_MAXINT,
722                                                      -1,
723                                                      GTK_PARAM_READWRITE));
724
725   g_object_class_install_property (gobject_class,
726                                    PROP_SCROLL_OFFSET,
727                                    g_param_spec_int ("scroll-offset",
728                                                      P_("Scroll offset"),
729                                                      P_("Number of pixels of the entry scrolled off the screen to the left"),
730                                                      0,
731                                                      G_MAXINT,
732                                                      0,
733                                                      GTK_PARAM_READABLE));
734
735   g_object_class_install_property (gobject_class,
736                                    PROP_TEXT,
737                                    g_param_spec_string ("text",
738                                                         P_("Text"),
739                                                         P_("The contents of the entry"),
740                                                         "",
741                                                         GTK_PARAM_READWRITE));
742
743   /**
744    * GtkEntry:xalign:
745    *
746    * The horizontal alignment, from 0 (left) to 1 (right). 
747    * Reversed for RTL layouts.
748    * 
749    * Since: 2.4
750    */
751   g_object_class_install_property (gobject_class,
752                                    PROP_XALIGN,
753                                    g_param_spec_float ("xalign",
754                                                        P_("X align"),
755                                                        P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
756                                                        0.0,
757                                                        1.0,
758                                                        0.0,
759                                                        GTK_PARAM_READWRITE));
760
761   /**
762    * GtkEntry:truncate-multiline:
763    *
764    * When %TRUE, pasted multi-line text is truncated to the first line.
765    *
766    * Since: 2.10
767    */
768   g_object_class_install_property (gobject_class,
769                                    PROP_TRUNCATE_MULTILINE,
770                                    g_param_spec_boolean ("truncate-multiline",
771                                                          P_("Truncate multiline"),
772                                                          P_("Whether to truncate multiline pastes to one line."),
773                                                          FALSE,
774                                                          GTK_PARAM_READWRITE));
775
776   /**
777    * GtkEntry:shadow-type:
778    *
779    * Which kind of shadow to draw around the entry when 
780    * #GtkEntry:has-frame is set to %TRUE.
781    *
782    * Since: 2.12
783    */
784   g_object_class_install_property (gobject_class,
785                                    PROP_SHADOW_TYPE,
786                                    g_param_spec_enum ("shadow-type",
787                                                       P_("Shadow type"),
788                                                       P_("Which kind of shadow to draw around the entry when has-frame is set"),
789                                                       GTK_TYPE_SHADOW_TYPE,
790                                                       GTK_SHADOW_IN,
791                                                       GTK_PARAM_READWRITE));
792
793   /**
794    * GtkEntry:overwrite-mode:
795    *
796    * If text is overwritten when typing in the #GtkEntry.
797    *
798    * Since: 2.14
799    */
800   g_object_class_install_property (gobject_class,
801                                    PROP_OVERWRITE_MODE,
802                                    g_param_spec_boolean ("overwrite-mode",
803                                                          P_("Overwrite mode"),
804                                                          P_("Whether new text overwrites existing text"),
805                                                          FALSE,
806                                                          GTK_PARAM_READWRITE));
807
808   /**
809    * GtkEntry:text-length:
810    *
811    * The length of the text in the #GtkEntry.
812    *
813    * Since: 2.14
814    */
815   g_object_class_install_property (gobject_class,
816                                    PROP_TEXT_LENGTH,
817                                    g_param_spec_uint ("text-length",
818                                                       P_("Text length"),
819                                                       P_("Length of the text currently in the entry"),
820                                                       0, 
821                                                       G_MAXUINT16,
822                                                       0,
823                                                       GTK_PARAM_READABLE));
824   /**
825    * GtkEntry:invisible-char-set:
826    *
827    * Whether the invisible char has been set for the #GtkEntry.
828    *
829    * Since: 2.16
830    */
831   g_object_class_install_property (gobject_class,
832                                    PROP_INVISIBLE_CHAR_SET,
833                                    g_param_spec_boolean ("invisible-char-set",
834                                                          P_("Invisible character set"),
835                                                          P_("Whether the invisible character has been set"),
836                                                          FALSE,
837                                                          GTK_PARAM_READWRITE));
838
839   /**
840    * GtkEntry:caps-lock-warning
841    *
842    * Whether password entries will show a warning when Caps Lock is on.
843    *
844    * Note that the warning is shown using a secondary icon, and thus
845    * does not work if you are using the secondary icon position for some 
846    * other purpose.
847    *
848    * Since: 2.16
849    */
850   g_object_class_install_property (gobject_class,
851                                    PROP_CAPS_LOCK_WARNING,
852                                    g_param_spec_boolean ("caps-lock-warning",
853                                                          P_("Caps Lock warning"),
854                                                          P_("Whether password entries will show a warning when Caps Lock is on"),
855                                                          TRUE,
856                                                          GTK_PARAM_READWRITE));
857
858   /**
859    * GtkEntry:progress-fraction:
860    *
861    * The current fraction of the task that's been completed.
862    *
863    * Since: 2.16
864    */
865   g_object_class_install_property (gobject_class,
866                                    PROP_PROGRESS_FRACTION,
867                                    g_param_spec_double ("progress-fraction",
868                                                         P_("Progress Fraction"),
869                                                         P_("The current fraction of the task that's been completed"),
870                                                         0.0,
871                                                         1.0,
872                                                         0.0,
873                                                         GTK_PARAM_READWRITE));
874
875   /**
876    * GtkEntry:progress-pulse-step:
877    *
878    * The fraction of total entry width to move the progress
879    * bouncing block for each call to gtk_entry_progress_pulse().
880    *
881    * Since: 2.16
882    */
883   g_object_class_install_property (gobject_class,
884                                    PROP_PROGRESS_PULSE_STEP,
885                                    g_param_spec_double ("progress-pulse-step",
886                                                         P_("Progress Pulse Step"),
887                                                         P_("The fraction of total entry width to move the progress bouncing block for each call to gtk_entry_progress_pulse()"),
888                                                         0.0,
889                                                         1.0,
890                                                         0.1,
891                                                         GTK_PARAM_READWRITE));
892
893   /**
894    * GtkEntry:primary-icon-pixbuf:
895    *
896    * A pixbuf to use as the primary icon for the entry.
897    *
898    * Since: 2.16
899    */
900   g_object_class_install_property (gobject_class,
901                                    PROP_PIXBUF_PRIMARY,
902                                    g_param_spec_object ("primary-icon-pixbuf",
903                                                         P_("Primary pixbuf"),
904                                                         P_("Primary pixbuf for the entry"),
905                                                         GDK_TYPE_PIXBUF,
906                                                         GTK_PARAM_READWRITE));
907   
908   /**
909    * GtkEntry:secondary-icon-pixbuf:
910    *
911    * An pixbuf to use as the secondary icon for the entry.
912    *
913    * Since: 2.16
914    */
915   g_object_class_install_property (gobject_class,
916                                    PROP_PIXBUF_SECONDARY,
917                                    g_param_spec_object ("secondary-icon-pixbuf",
918                                                         P_("Secondary pixbuf"),
919                                                         P_("Secondary pixbuf for the entry"),
920                                                         GDK_TYPE_PIXBUF,
921                                                         GTK_PARAM_READWRITE));
922
923   /**
924    * GtkEntry:primary-icon-stock:
925    *
926    * The stock id to use for the primary icon for the entry.
927    *
928    * Since: 2.16
929    */
930   g_object_class_install_property (gobject_class,
931                                    PROP_STOCK_PRIMARY,
932                                    g_param_spec_string ("primary-icon-stock",
933                                                         P_("Primary stock ID"),
934                                                         P_("Stock ID for primary icon"),
935                                                         NULL,
936                                                         GTK_PARAM_READWRITE));
937
938   /**
939    * GtkEntry:secondary-icon-stock:
940    *
941    * The stock id to use for the secondary icon for the entry.
942    *
943    * Since: 2.16
944    */
945   g_object_class_install_property (gobject_class,
946                                    PROP_STOCK_SECONDARY,
947                                    g_param_spec_string ("secondary-icon-stock",
948                                                         P_("Secondary stock ID"),
949                                                         P_("Stock ID for secondary icon"),
950                                                         NULL,
951                                                         GTK_PARAM_READWRITE));
952   
953   /**
954    * GtkEntry:primary-icon-name:
955    *
956    * The icon name to use for the primary icon for the entry.
957    *
958    * Since: 2.16
959    */
960   g_object_class_install_property (gobject_class,
961                                    PROP_ICON_NAME_PRIMARY,
962                                    g_param_spec_string ("primary-icon-name",
963                                                         P_("Primary icon name"),
964                                                         P_("Icon name for primary icon"),
965                                                         NULL,
966                                                         GTK_PARAM_READWRITE));
967   
968   /**
969    * GtkEntry:secondary-icon-name:
970    *
971    * The icon name to use for the secondary icon for the entry.
972    *
973    * Since: 2.16
974    */
975   g_object_class_install_property (gobject_class,
976                                    PROP_ICON_NAME_SECONDARY,
977                                    g_param_spec_string ("secondary-icon-name",
978                                                         P_("Secondary icon name"),
979                                                         P_("Icon name for secondary icon"),
980                                                         NULL,
981                                                         GTK_PARAM_READWRITE));
982   
983   /**
984    * GtkEntry:primary-icon-gicon:
985    *
986    * The #GIcon to use for the primary icon for the entry.
987    *
988    * Since: 2.16
989    */
990   g_object_class_install_property (gobject_class,
991                                    PROP_GICON_PRIMARY,
992                                    g_param_spec_object ("primary-icon-gicon",
993                                                         P_("Primary GIcon"),
994                                                         P_("GIcon for primary icon"),
995                                                         G_TYPE_ICON,
996                                                         GTK_PARAM_READWRITE));
997   
998   /**
999    * GtkEntry:secondary-icon-gicon:
1000    *
1001    * The #GIcon to use for the secondary icon for the entry.
1002    *
1003    * Since: 2.16
1004    */
1005   g_object_class_install_property (gobject_class,
1006                                    PROP_GICON_SECONDARY,
1007                                    g_param_spec_object ("secondary-icon-gicon",
1008                                                         P_("Secondary GIcon"),
1009                                                         P_("GIcon for secondary icon"),
1010                                                         G_TYPE_ICON,
1011                                                         GTK_PARAM_READWRITE));
1012   
1013   /**
1014    * GtkEntry:primary-icon-storage-type:
1015    *
1016    * The representation which is used for the primary icon of the entry.
1017    *
1018    * Since: 2.16
1019    */
1020   g_object_class_install_property (gobject_class,
1021                                    PROP_STORAGE_TYPE_PRIMARY,
1022                                    g_param_spec_enum ("primary-icon-storage-type",
1023                                                       P_("Primary storage type"),
1024                                                       P_("The representation being used for primary icon"),
1025                                                       GTK_TYPE_IMAGE_TYPE,
1026                                                       GTK_IMAGE_EMPTY,
1027                                                       GTK_PARAM_READABLE));
1028   
1029   /**
1030    * GtkEntry:secondary-icon-storage-type:
1031    *
1032    * The representation which is used for the secondary icon of the entry.
1033    *
1034    * Since: 2.16
1035    */
1036   g_object_class_install_property (gobject_class,
1037                                    PROP_STORAGE_TYPE_SECONDARY,
1038                                    g_param_spec_enum ("secondary-icon-storage-type",
1039                                                       P_("Secondary storage type"),
1040                                                       P_("The representation being used for secondary icon"),
1041                                                       GTK_TYPE_IMAGE_TYPE,
1042                                                       GTK_IMAGE_EMPTY,
1043                                                       GTK_PARAM_READABLE));
1044   
1045   /**
1046    * GtkEntry:primary-icon-activatable:
1047    *
1048    * Whether the primary icon is activatable.
1049    *
1050    * GTK+ emits the #GtkEntry::icon-press and #GtkEntry::icon-release 
1051    * signals only on sensitive, activatable icons. 
1052    *
1053    * Sensitive, but non-activatable icons can be used for purely 
1054    * informational purposes.
1055    *
1056    * Since: 2.16
1057    */
1058   g_object_class_install_property (gobject_class,
1059                                    PROP_ACTIVATABLE_PRIMARY,
1060                                    g_param_spec_boolean ("primary-icon-activatable",
1061                                                          P_("Primary icon activatable"),
1062                                                          P_("Whether the primary icon is activatable"),
1063                                                          FALSE,
1064                                                          GTK_PARAM_READWRITE));
1065   
1066   /**
1067    * GtkEntry:secondary-icon-activatable:
1068    *
1069    * Whether the secondary icon is activatable.
1070    *
1071    * GTK+ emits the #GtkEntry::icon-press and #GtkEntry::icon-release 
1072    * signals only on sensitive, activatable icons.
1073    *
1074    * Sensitive, but non-activatable icons can be used for purely 
1075    * informational purposes.
1076    *
1077    * Since: 2.16
1078    */
1079   g_object_class_install_property (gobject_class,
1080                                    PROP_ACTIVATABLE_SECONDARY,
1081                                    g_param_spec_boolean ("secondary-icon-activatable",
1082                                                          P_("Secondary icon activatable"),
1083                                                          P_("Whether the secondary icon is activatable"),
1084                                                          FALSE,
1085                                                          GTK_PARAM_READWRITE));
1086   
1087   
1088   /**
1089    * GtkEntry:primary-icon-sensitive:
1090    *
1091    * Whether the primary icon is sensitive.
1092    *
1093    * An insensitive icon appears grayed out. GTK+ does not emit the 
1094    * #GtkEntry::icon-press and #GtkEntry::icon-release signals and 
1095    * does not allow DND from insensitive icons.
1096    *
1097    * An icon should be set insensitive if the action that would trigger
1098    * when clicked is currently not available.
1099    * 
1100    * Since: 2.16
1101    */
1102   g_object_class_install_property (gobject_class,
1103                                    PROP_SENSITIVE_PRIMARY,
1104                                    g_param_spec_boolean ("primary-icon-sensitive",
1105                                                          P_("Primary icon sensitive"),
1106                                                          P_("Whether the primary icon is sensitive"),
1107                                                          TRUE,
1108                                                          GTK_PARAM_READWRITE));
1109   
1110   /**
1111    * GtkEntry:secondary-icon-sensitive:
1112    *
1113    * Whether the secondary icon is sensitive.
1114    *
1115    * An insensitive icon appears grayed out. GTK+ does not emit the 
1116    * #GtkEntry::icon-press and #GtkEntry::icon-release signals and 
1117    * does not allow DND from insensitive icons.
1118    *
1119    * An icon should be set insensitive if the action that would trigger
1120    * when clicked is currently not available.
1121    *
1122    * Since: 2.16
1123    */
1124   g_object_class_install_property (gobject_class,
1125                                    PROP_SENSITIVE_SECONDARY,
1126                                    g_param_spec_boolean ("secondary-icon-sensitive",
1127                                                          P_("Secondary icon sensitive"),
1128                                                          P_("Whether the secondary icon is sensitive"),
1129                                                          TRUE,
1130                                                          GTK_PARAM_READWRITE));
1131   
1132   /**
1133    * GtkEntry:primary-icon-tooltip-text:
1134    * 
1135    * The contents of the tooltip on the primary icon.
1136    *
1137    * Also see gtk_entry_set_icon_tooltip_text().
1138    *
1139    * Since: 2.16
1140    */
1141   g_object_class_install_property (gobject_class,
1142                                    PROP_TOOLTIP_TEXT_PRIMARY,
1143                                    g_param_spec_string ("primary-icon-tooltip-text",
1144                                                         P_("Primary icon tooltip text"),
1145                                                         P_("The contents of the tooltip on the primary icon"),                              
1146                                                         NULL,
1147                                                         GTK_PARAM_READWRITE));
1148   
1149   /**
1150    * GtkEntry:secondary-icon-tooltip-text:
1151    * 
1152    * The contents of the tooltip on the secondary icon.
1153    *
1154    * Also see gtk_entry_set_icon_tooltip_text().
1155    *
1156    * Since: 2.16
1157    */
1158   g_object_class_install_property (gobject_class,
1159                                    PROP_TOOLTIP_TEXT_SECONDARY,
1160                                    g_param_spec_string ("secondary-icon-tooltip-text",
1161                                                         P_("Secondary icon tooltip text"),
1162                                                         P_("The contents of the tooltip on the secondary icon"),                              
1163                                                         NULL,
1164                                                         GTK_PARAM_READWRITE));
1165
1166   /**
1167    * GtkEntry:primary-icon-tooltip-markup:
1168    * 
1169    * The contents of the tooltip on the primary icon, which is marked up
1170    * with the <link linkend="PangoMarkupFormat">Pango text markup 
1171    * language</link>.
1172    *
1173    * Also see gtk_entry_set_icon_tooltip_markup().
1174    *
1175    * Since: 2.16
1176    */
1177   g_object_class_install_property (gobject_class,
1178                                    PROP_TOOLTIP_MARKUP_PRIMARY,
1179                                    g_param_spec_string ("primary-icon-tooltip-markup",
1180                                                         P_("Primary icon tooltip markup"),
1181                                                         P_("The contents of the tooltip on the primary icon"),                              
1182                                                         NULL,
1183                                                         GTK_PARAM_READWRITE));
1184
1185   /**
1186    * GtkEntry:secondary-icon-tooltip-markup:
1187    * 
1188    * The contents of the tooltip on the secondary icon, which is marked up
1189    * with the <link linkend="PangoMarkupFormat">Pango text markup 
1190    * language</link>.
1191    *
1192    * Also see gtk_entry_set_icon_tooltip_markup().
1193    *
1194    * Since: 2.16
1195    */
1196   g_object_class_install_property (gobject_class,
1197                                    PROP_TOOLTIP_MARKUP_SECONDARY,
1198                                    g_param_spec_string ("secondary-icon-tooltip-markup",
1199                                                         P_("Secondary icon tooltip markup"),
1200                                                         P_("The contents of the tooltip on the secondary icon"),                              
1201                                                         NULL,
1202                                                         GTK_PARAM_READWRITE));
1203
1204   /**
1205    * GtkEntry:im-module:
1206    *
1207    * Which IM (input method) module should be used for this entry. 
1208    * See #GtkIMContext.
1209    * 
1210    * Setting this to a non-%NULL value overrides the
1211    * system-wide IM module setting. See the GtkSettings 
1212    * #GtkSettings:gtk-im-module property.
1213    *
1214    * Since: 2.16
1215    */  
1216   g_object_class_install_property (gobject_class,
1217                                    PROP_IM_MODULE,
1218                                    g_param_spec_string ("im-module",
1219                                                         P_("IM module"),
1220                                                         P_("Which IM module should be used"),
1221                                                         NULL,
1222                                                         GTK_PARAM_READWRITE));
1223
1224   /**
1225    * GtkEntry:icon-prelight:
1226    *
1227    * The prelight style property determines whether activatable
1228    * icons prelight on mouseover.
1229    *
1230    * Since: 2.16
1231    */
1232   gtk_widget_class_install_style_property (widget_class,
1233                                            g_param_spec_boolean ("icon-prelight",
1234                                                                  P_("Icon Prelight"),
1235                                                                  P_("Whether activatable icons should prelight when hovered"),
1236                                                                  TRUE,
1237                                                                  GTK_PARAM_READABLE));
1238
1239   /**
1240    * GtkEntry:progress-border:
1241    *
1242    * The border around the progress bar in the entry.
1243    *
1244    * Since: 2.16
1245    */
1246   gtk_widget_class_install_style_property (widget_class,
1247                                            g_param_spec_boxed ("progress-border",
1248                                                                P_("Progress Border"),
1249                                                                P_("Border around the progress bar"),
1250                                                                GTK_TYPE_BORDER,
1251                                                                GTK_PARAM_READABLE));
1252   
1253   /**
1254    * GtkEntry:invisible-char:
1255    *
1256    * The invisible character is used when masking entry contents (in
1257    * \"password mode\")"). When it is not explicitly set with the
1258    * #GtkEntry::invisible-char property, GTK+ determines the character
1259    * to use from a list of possible candidates, depending on availability
1260    * in the current font.
1261    *
1262    * This style property allows the theme to prepend a character
1263    * to the list of candidates.
1264    *
1265    * Since: 2.18
1266    */
1267   gtk_widget_class_install_style_property (widget_class,
1268                                            g_param_spec_unichar ("invisible-char",
1269                                                                  P_("Invisible character"),
1270                                                                  P_("The character to use when masking entry contents (in \"password mode\")"),
1271                                                                  0,
1272                                                                  GTK_PARAM_READABLE));
1273   
1274   /**
1275    * GtkEntry::populate-popup:
1276    * @entry: The entry on which the signal is emitted
1277    * @menu: the menu that is being populated
1278    *
1279    * The ::populate-popup signal gets emitted before showing the 
1280    * context menu of the entry. 
1281    *
1282    * If you need to add items to the context menu, connect
1283    * to this signal and append your menuitems to the @menu.
1284    */
1285   signals[POPULATE_POPUP] =
1286     g_signal_new (I_("populate-popup"),
1287                   G_OBJECT_CLASS_TYPE (gobject_class),
1288                   G_SIGNAL_RUN_LAST,
1289                   G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
1290                   NULL, NULL,
1291                   _gtk_marshal_VOID__OBJECT,
1292                   G_TYPE_NONE, 1,
1293                   GTK_TYPE_MENU);
1294   
1295  /* Action signals */
1296   
1297   /**
1298    * GtkEntry::activate:
1299    * @entry: The entry on which the signal is emitted
1300    *
1301    * A  <link linkend="keybinding-signals">keybinding signal</link>
1302    * which gets emitted when the user activates the entry.
1303    * 
1304    * Applications should not connect to it, but may emit it with
1305    * g_signal_emit_by_name() if they need to control activation 
1306    * programmatically.
1307    *
1308    * The default bindings for this signal are all forms of the Enter key.
1309    */
1310   signals[ACTIVATE] =
1311     g_signal_new (I_("activate"),
1312                   G_OBJECT_CLASS_TYPE (gobject_class),
1313                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1314                   G_STRUCT_OFFSET (GtkEntryClass, activate),
1315                   NULL, NULL,
1316                   _gtk_marshal_VOID__VOID,
1317                   G_TYPE_NONE, 0);
1318   widget_class->activate_signal = signals[ACTIVATE];
1319
1320   /**
1321    * GtkEntry::move-cursor:
1322    * @entry: the object which received the signal
1323    * @step: the granularity of the move, as a #GtkMovementStep
1324    * @count: the number of @step units to move
1325    * @extend_selection: %TRUE if the move should extend the selection
1326    *
1327    * The ::move-cursor signal is a
1328    * <link linkend="keybinding-signals">keybinding signal</link>
1329    * which gets emitted when the user initiates a cursor movement.
1330    * If the cursor is not visible in @entry, this signal causes
1331    * the viewport to be moved instead.
1332    *
1333    * Applications should not connect to it, but may emit it with
1334    * g_signal_emit_by_name() if they need to control the cursor
1335    * programmatically.
1336    *
1337    * The default bindings for this signal come in two variants,
1338    * the variant with the Shift modifier extends the selection,
1339    * the variant without the Shift modifer does not.
1340    * There are too many key combinations to list them all here.
1341    * <itemizedlist>
1342    * <listitem>Arrow keys move by individual characters/lines</listitem>
1343    * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
1344    * <listitem>Home/End keys move to the ends of the buffer</listitem>
1345    * </itemizedlist>
1346    */
1347   signals[MOVE_CURSOR] = 
1348     g_signal_new (I_("move-cursor"),
1349                   G_OBJECT_CLASS_TYPE (gobject_class),
1350                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1351                   G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
1352                   NULL, NULL,
1353                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
1354                   G_TYPE_NONE, 3,
1355                   GTK_TYPE_MOVEMENT_STEP,
1356                   G_TYPE_INT,
1357                   G_TYPE_BOOLEAN);
1358
1359   /**
1360    * GtkEntry::insert-at-cursor:
1361    * @entry: the object which received the signal
1362    * @string: the string to insert
1363    *
1364    * The ::insert-at-cursor signal is a
1365    * <link linkend="keybinding-signals">keybinding signal</link>
1366    * which gets emitted when the user initiates the insertion of a
1367    * fixed string at the cursor.
1368    *
1369    * This signal has no default bindings.
1370    */
1371   signals[INSERT_AT_CURSOR] = 
1372     g_signal_new (I_("insert-at-cursor"),
1373                   G_OBJECT_CLASS_TYPE (gobject_class),
1374                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1375                   G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
1376                   NULL, NULL,
1377                   _gtk_marshal_VOID__STRING,
1378                   G_TYPE_NONE, 1,
1379                   G_TYPE_STRING);
1380
1381   /**
1382    * GtkEntry::delete-from-cursor:
1383    * @entry: the object which received the signal
1384    * @type: the granularity of the deletion, as a #GtkDeleteType
1385    * @count: the number of @type units to delete
1386    *
1387    * The ::delete-from-cursor signal is a
1388    * <link linkend="keybinding-signals">keybinding signal</link>
1389    * which gets emitted when the user initiates a text deletion.
1390    *
1391    * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
1392    * if there is one, otherwise it deletes the requested number
1393    * of characters.
1394    *
1395    * The default bindings for this signal are
1396    * Delete for deleting a character and Ctrl-Delete for
1397    * deleting a word.
1398    */
1399   signals[DELETE_FROM_CURSOR] = 
1400     g_signal_new (I_("delete-from-cursor"),
1401                   G_OBJECT_CLASS_TYPE (gobject_class),
1402                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1403                   G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
1404                   NULL, NULL,
1405                   _gtk_marshal_VOID__ENUM_INT,
1406                   G_TYPE_NONE, 2,
1407                   GTK_TYPE_DELETE_TYPE,
1408                   G_TYPE_INT);
1409
1410   /**
1411    * GtkEntry::backspace:
1412    * @entry: the object which received the signal
1413    *
1414    * The ::backspace signal is a
1415    * <link linkend="keybinding-signals">keybinding signal</link>
1416    * which gets emitted when the user asks for it.
1417    *
1418    * The default bindings for this signal are
1419    * Backspace and Shift-Backspace.
1420    */
1421   signals[BACKSPACE] =
1422     g_signal_new (I_("backspace"),
1423                   G_OBJECT_CLASS_TYPE (gobject_class),
1424                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1425                   G_STRUCT_OFFSET (GtkEntryClass, backspace),
1426                   NULL, NULL,
1427                   _gtk_marshal_VOID__VOID,
1428                   G_TYPE_NONE, 0);
1429
1430   /**
1431    * GtkEntry::cut-clipboard:
1432    * @entry: the object which received the signal
1433    *
1434    * The ::cut-clipboard signal is a
1435    * <link linkend="keybinding-signals">keybinding signal</link>
1436    * which gets emitted to cut the selection to the clipboard.
1437    *
1438    * The default bindings for this signal are
1439    * Ctrl-x and Shift-Delete.
1440    */
1441   signals[CUT_CLIPBOARD] =
1442     g_signal_new (I_("cut-clipboard"),
1443                   G_OBJECT_CLASS_TYPE (gobject_class),
1444                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1445                   G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
1446                   NULL, NULL,
1447                   _gtk_marshal_VOID__VOID,
1448                   G_TYPE_NONE, 0);
1449
1450   /**
1451    * GtkEntry::copy-clipboard:
1452    * @entry: the object which received the signal
1453    *
1454    * The ::copy-clipboard signal is a
1455    * <link linkend="keybinding-signals">keybinding signal</link>
1456    * which gets emitted to copy the selection to the clipboard.
1457    *
1458    * The default bindings for this signal are
1459    * Ctrl-c and Ctrl-Insert.
1460    */
1461   signals[COPY_CLIPBOARD] =
1462     g_signal_new (I_("copy-clipboard"),
1463                   G_OBJECT_CLASS_TYPE (gobject_class),
1464                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1465                   G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
1466                   NULL, NULL,
1467                   _gtk_marshal_VOID__VOID,
1468                   G_TYPE_NONE, 0);
1469
1470   /**
1471    * GtkEntry::paste-clipboard:
1472    * @entry: the object which received the signal
1473    *
1474    * The ::paste-clipboard signal is a
1475    * <link linkend="keybinding-signals">keybinding signal</link>
1476    * which gets emitted to paste the contents of the clipboard
1477    * into the text view.
1478    *
1479    * The default bindings for this signal are
1480    * Ctrl-v and Shift-Insert.
1481    */
1482   signals[PASTE_CLIPBOARD] =
1483     g_signal_new (I_("paste-clipboard"),
1484                   G_OBJECT_CLASS_TYPE (gobject_class),
1485                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1486                   G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
1487                   NULL, NULL,
1488                   _gtk_marshal_VOID__VOID,
1489                   G_TYPE_NONE, 0);
1490
1491   /**
1492    * GtkEntry::toggle-overwrite:
1493    * @entry: the object which received the signal
1494    *
1495    * The ::toggle-overwrite signal is a
1496    * <link linkend="keybinding-signals">keybinding signal</link>
1497    * which gets emitted to toggle the overwrite mode of the entry.
1498    *
1499    * The default bindings for this signal is Insert.
1500    */
1501   signals[TOGGLE_OVERWRITE] =
1502     g_signal_new (I_("toggle-overwrite"),
1503                   G_OBJECT_CLASS_TYPE (gobject_class),
1504                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1505                   G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
1506                   NULL, NULL,
1507                   _gtk_marshal_VOID__VOID,
1508                   G_TYPE_NONE, 0);
1509
1510   /**
1511    * GtkEntry::icon-press:
1512    * @entry: The entry on which the signal is emitted
1513    * @icon_pos: The position of the clicked icon
1514    * @event: the button press event
1515    *
1516    * The ::icon-press signal is emitted when an activatable icon
1517    * is clicked.
1518    *
1519    * Since: 2.16
1520    */
1521   signals[ICON_PRESS] =
1522     g_signal_new (I_("icon-press"),
1523                   G_TYPE_FROM_CLASS (gobject_class),
1524                   G_SIGNAL_RUN_LAST,
1525                   0,
1526                   NULL, NULL,
1527                   _gtk_marshal_VOID__ENUM_BOXED,
1528                   G_TYPE_NONE, 2,
1529                   GTK_TYPE_ENTRY_ICON_POSITION,
1530                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1531   
1532   /**
1533    * GtkEntry::icon-release:
1534    * @entry: The entry on which the signal is emitted
1535    * @icon_pos: The position of the clicked icon
1536    * @event: the button release event
1537    *
1538    * The ::icon-release signal is emitted on the button release from a
1539    * mouse click over an activatable icon.
1540    *
1541    * Since: 2.16
1542    */
1543   signals[ICON_RELEASE] =
1544     g_signal_new (I_("icon-release"),
1545                   G_TYPE_FROM_CLASS (gobject_class),
1546                   G_SIGNAL_RUN_LAST,
1547                   0,
1548                   NULL, NULL,
1549                   _gtk_marshal_VOID__ENUM_BOXED,
1550                   G_TYPE_NONE, 2,
1551                   GTK_TYPE_ENTRY_ICON_POSITION,
1552                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1553
1554   /**
1555    * GtkEntry::preedit-changed:
1556    * @entry: the object which received the signal
1557    * @preedit: the current preedit string
1558    *
1559    * If an input method is used, the typed text will not immediately
1560    * be committed to the buffer. So if you are interested in the text,
1561    * connect to this signal.
1562    *
1563    * Since: 2.20
1564    */
1565   signals[PREEDIT_CHANGED] =
1566     g_signal_new_class_handler (I_("preedit-changed"),
1567                                 G_OBJECT_CLASS_TYPE (gobject_class),
1568                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1569                                 NULL,
1570                                 NULL, NULL,
1571                                 _gtk_marshal_VOID__STRING,
1572                                 G_TYPE_NONE, 1,
1573                                 G_TYPE_STRING);
1574
1575
1576   /*
1577    * Key bindings
1578    */
1579
1580   binding_set = gtk_binding_set_by_class (class);
1581
1582   /* Moving the insertion point */
1583   add_move_binding (binding_set, GDK_KEY_Right, 0,
1584                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1585   
1586   add_move_binding (binding_set, GDK_KEY_Left, 0,
1587                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1588
1589   add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
1590                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1591   
1592   add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
1593                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1594   
1595   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1596                     GTK_MOVEMENT_WORDS, 1);
1597
1598   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1599                     GTK_MOVEMENT_WORDS, -1);
1600
1601   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1602                     GTK_MOVEMENT_WORDS, 1);
1603
1604   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1605                     GTK_MOVEMENT_WORDS, -1);
1606   
1607   add_move_binding (binding_set, GDK_KEY_Home, 0,
1608                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1609
1610   add_move_binding (binding_set, GDK_KEY_End, 0,
1611                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1612
1613   add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1614                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1615
1616   add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1617                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1618   
1619   add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1620                     GTK_MOVEMENT_BUFFER_ENDS, -1);
1621
1622   add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1623                     GTK_MOVEMENT_BUFFER_ENDS, 1);
1624
1625   add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1626                     GTK_MOVEMENT_BUFFER_ENDS, -1);
1627
1628   add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1629                     GTK_MOVEMENT_BUFFER_ENDS, 1);
1630
1631   /* Select all
1632    */
1633   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1634                                 "move-cursor", 3,
1635                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1636                                 G_TYPE_INT, -1,
1637                                 G_TYPE_BOOLEAN, FALSE);
1638   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1639                                 "move-cursor", 3,
1640                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1641                                 G_TYPE_INT, 1,
1642                                 G_TYPE_BOOLEAN, TRUE);  
1643
1644   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1645                                 "move-cursor", 3,
1646                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1647                                 G_TYPE_INT, -1,
1648                                 G_TYPE_BOOLEAN, FALSE);
1649   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1650                                 "move-cursor", 3,
1651                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1652                                 G_TYPE_INT, 1,
1653                                 G_TYPE_BOOLEAN, TRUE);  
1654   /* Unselect all 
1655    */
1656   gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1657                                 "move-cursor", 3,
1658                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1659                                 G_TYPE_INT, 0,
1660                                 G_TYPE_BOOLEAN, FALSE);
1661   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1662                                 "move-cursor", 3,
1663                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1664                                 G_TYPE_INT, 0,
1665                                 G_TYPE_BOOLEAN, FALSE);
1666
1667   /* Activate
1668    */
1669   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1670                                 "activate", 0);
1671   gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1672                                 "activate", 0);
1673   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1674                                 "activate", 0);
1675   
1676   /* Deleting text */
1677   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, 0,
1678                                 "delete-from-cursor", 2,
1679                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
1680                                 G_TYPE_INT, 1);
1681
1682   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, 0,
1683                                 "delete-from-cursor", 2,
1684                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
1685                                 G_TYPE_INT, 1);
1686   
1687   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1688                                 "backspace", 0);
1689
1690   /* Make this do the same as Backspace, to help with mis-typing */
1691   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK,
1692                                 "backspace", 0);
1693
1694   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_CONTROL_MASK,
1695                                 "delete-from-cursor", 2,
1696                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1697                                 G_TYPE_INT, 1);
1698
1699   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_CONTROL_MASK,
1700                                 "delete-from-cursor", 2,
1701                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1702                                 G_TYPE_INT, 1);
1703   
1704   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_CONTROL_MASK,
1705                                 "delete-from-cursor", 2,
1706                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1707                                 G_TYPE_INT, -1);
1708
1709   /* Cut/copy/paste */
1710
1711   gtk_binding_entry_add_signal (binding_set, GDK_KEY_x, GDK_CONTROL_MASK,
1712                                 "cut-clipboard", 0);
1713   gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1714                                 "copy-clipboard", 0);
1715   gtk_binding_entry_add_signal (binding_set, GDK_KEY_v, GDK_CONTROL_MASK,
1716                                 "paste-clipboard", 0);
1717
1718   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK,
1719                                 "cut-clipboard", 0);
1720   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_CONTROL_MASK,
1721                                 "copy-clipboard", 0);
1722   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
1723                                 "paste-clipboard", 0);
1724
1725   /* Overwrite */
1726   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, 0,
1727                                 "toggle-overwrite", 0);
1728   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
1729                                 "toggle-overwrite", 0);
1730
1731   /**
1732    * GtkEntry:inner-border:
1733    *
1734    * Sets the text area's border between the text and the frame.
1735    *
1736    * Since: 2.10
1737    */
1738   gtk_widget_class_install_style_property (widget_class,
1739                                            g_param_spec_boxed ("inner-border",
1740                                                                P_("Inner Border"),
1741                                                                P_("Border between text and frame."),
1742                                                                GTK_TYPE_BORDER,
1743                                                                GTK_PARAM_READABLE)); 
1744
1745   g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
1746 }
1747
1748 static void
1749 gtk_entry_editable_init (GtkEditableInterface *iface)
1750 {
1751   iface->do_insert_text = gtk_entry_insert_text;
1752   iface->do_delete_text = gtk_entry_delete_text;
1753   iface->insert_text = gtk_entry_real_insert_text;
1754   iface->delete_text = gtk_entry_real_delete_text;
1755   iface->get_chars = gtk_entry_get_chars;
1756   iface->set_selection_bounds = gtk_entry_set_selection_bounds;
1757   iface->get_selection_bounds = gtk_entry_get_selection_bounds;
1758   iface->set_position = gtk_entry_real_set_position;
1759   iface->get_position = gtk_entry_get_position;
1760 }
1761
1762 static void
1763 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
1764 {
1765   iface->start_editing = gtk_entry_start_editing;
1766 }
1767
1768 static void
1769 gtk_entry_set_property (GObject         *object,
1770                         guint            prop_id,
1771                         const GValue    *value,
1772                         GParamSpec      *pspec)
1773 {
1774   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1775   GtkEntry *entry = GTK_ENTRY (object);
1776   GtkWidget *widget;
1777
1778   switch (prop_id)
1779     {
1780     case PROP_BUFFER:
1781       gtk_entry_set_buffer (entry, g_value_get_object (value));
1782       break;
1783
1784     case PROP_EDITABLE:
1785       {
1786         gboolean new_value = g_value_get_boolean (value);
1787
1788         if (new_value != entry->editable)
1789           {
1790             widget = GTK_WIDGET (entry);
1791             if (!new_value)
1792               {
1793                 _gtk_entry_reset_im_context (entry);
1794                 if (gtk_widget_has_focus (widget))
1795                   gtk_im_context_focus_out (entry->im_context);
1796
1797                 entry->preedit_length = 0;
1798                 entry->preedit_cursor = 0;
1799               }
1800
1801             entry->editable = new_value;
1802
1803             if (new_value && gtk_widget_has_focus (widget))
1804               gtk_im_context_focus_in (entry->im_context);
1805             
1806             gtk_entry_queue_draw (entry);
1807           }
1808       }
1809       break;
1810
1811     case PROP_MAX_LENGTH:
1812       gtk_entry_set_max_length (entry, g_value_get_int (value));
1813       break;
1814       
1815     case PROP_VISIBILITY:
1816       gtk_entry_set_visibility (entry, g_value_get_boolean (value));
1817       break;
1818
1819     case PROP_HAS_FRAME:
1820       gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
1821       break;
1822
1823     case PROP_INNER_BORDER:
1824       gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
1825       break;
1826
1827     case PROP_INVISIBLE_CHAR:
1828       gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
1829       break;
1830
1831     case PROP_ACTIVATES_DEFAULT:
1832       gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1833       break;
1834
1835     case PROP_WIDTH_CHARS:
1836       gtk_entry_set_width_chars (entry, g_value_get_int (value));
1837       break;
1838
1839     case PROP_TEXT:
1840       gtk_entry_set_text (entry, g_value_get_string (value));
1841       break;
1842
1843     case PROP_XALIGN:
1844       gtk_entry_set_alignment (entry, g_value_get_float (value));
1845       break;
1846
1847     case PROP_TRUNCATE_MULTILINE:
1848       entry->truncate_multiline = g_value_get_boolean (value);
1849       break;
1850
1851     case PROP_SHADOW_TYPE:
1852       priv->shadow_type = g_value_get_enum (value);
1853       break;
1854
1855     case PROP_OVERWRITE_MODE:
1856       gtk_entry_set_overwrite_mode (entry, g_value_get_boolean (value));
1857       break;
1858
1859     case PROP_INVISIBLE_CHAR_SET:
1860       if (g_value_get_boolean (value))
1861         priv->invisible_char_set = TRUE;
1862       else
1863         gtk_entry_unset_invisible_char (entry);
1864       break;
1865
1866     case PROP_CAPS_LOCK_WARNING:
1867       priv->caps_lock_warning = g_value_get_boolean (value);
1868       break;
1869
1870     case PROP_PROGRESS_FRACTION:
1871       gtk_entry_set_progress_fraction (entry, g_value_get_double (value));
1872       break;
1873
1874     case PROP_PROGRESS_PULSE_STEP:
1875       gtk_entry_set_progress_pulse_step (entry, g_value_get_double (value));
1876       break;
1877
1878     case PROP_PIXBUF_PRIMARY:
1879       gtk_entry_set_icon_from_pixbuf (entry,
1880                                       GTK_ENTRY_ICON_PRIMARY,
1881                                       g_value_get_object (value));
1882       break;
1883
1884     case PROP_PIXBUF_SECONDARY:
1885       gtk_entry_set_icon_from_pixbuf (entry,
1886                                       GTK_ENTRY_ICON_SECONDARY,
1887                                       g_value_get_object (value));
1888       break;
1889
1890     case PROP_STOCK_PRIMARY:
1891       gtk_entry_set_icon_from_stock (entry,
1892                                      GTK_ENTRY_ICON_PRIMARY,
1893                                      g_value_get_string (value));
1894       break;
1895
1896     case PROP_STOCK_SECONDARY:
1897       gtk_entry_set_icon_from_stock (entry,
1898                                      GTK_ENTRY_ICON_SECONDARY,
1899                                      g_value_get_string (value));
1900       break;
1901
1902     case PROP_ICON_NAME_PRIMARY:
1903       gtk_entry_set_icon_from_icon_name (entry,
1904                                          GTK_ENTRY_ICON_PRIMARY,
1905                                          g_value_get_string (value));
1906       break;
1907
1908     case PROP_ICON_NAME_SECONDARY:
1909       gtk_entry_set_icon_from_icon_name (entry,
1910                                          GTK_ENTRY_ICON_SECONDARY,
1911                                          g_value_get_string (value));
1912       break;
1913
1914     case PROP_GICON_PRIMARY:
1915       gtk_entry_set_icon_from_gicon (entry,
1916                                      GTK_ENTRY_ICON_PRIMARY,
1917                                      g_value_get_object (value));
1918       break;
1919
1920     case PROP_GICON_SECONDARY:
1921       gtk_entry_set_icon_from_gicon (entry,
1922                                      GTK_ENTRY_ICON_SECONDARY,
1923                                      g_value_get_object (value));
1924       break;
1925
1926     case PROP_ACTIVATABLE_PRIMARY:
1927       gtk_entry_set_icon_activatable (entry,
1928                                       GTK_ENTRY_ICON_PRIMARY,
1929                                       g_value_get_boolean (value));
1930       break;
1931
1932     case PROP_ACTIVATABLE_SECONDARY:
1933       gtk_entry_set_icon_activatable (entry,
1934                                       GTK_ENTRY_ICON_SECONDARY,
1935                                       g_value_get_boolean (value));
1936       break;
1937
1938     case PROP_SENSITIVE_PRIMARY:
1939       gtk_entry_set_icon_sensitive (entry,
1940                                     GTK_ENTRY_ICON_PRIMARY,
1941                                     g_value_get_boolean (value));
1942       break;
1943
1944     case PROP_SENSITIVE_SECONDARY:
1945       gtk_entry_set_icon_sensitive (entry,
1946                                     GTK_ENTRY_ICON_SECONDARY,
1947                                     g_value_get_boolean (value));
1948       break;
1949
1950     case PROP_TOOLTIP_TEXT_PRIMARY:
1951       gtk_entry_set_icon_tooltip_text (entry,
1952                                        GTK_ENTRY_ICON_PRIMARY,
1953                                        g_value_get_string (value));
1954       break;
1955
1956     case PROP_TOOLTIP_TEXT_SECONDARY:
1957       gtk_entry_set_icon_tooltip_text (entry,
1958                                        GTK_ENTRY_ICON_SECONDARY,
1959                                        g_value_get_string (value));
1960       break;
1961
1962     case PROP_TOOLTIP_MARKUP_PRIMARY:
1963       gtk_entry_set_icon_tooltip_markup (entry,
1964                                          GTK_ENTRY_ICON_PRIMARY,
1965                                          g_value_get_string (value));
1966       break;
1967
1968     case PROP_TOOLTIP_MARKUP_SECONDARY:
1969       gtk_entry_set_icon_tooltip_markup (entry,
1970                                          GTK_ENTRY_ICON_SECONDARY,
1971                                          g_value_get_string (value));
1972       break;
1973
1974     case PROP_IM_MODULE:
1975       g_free (priv->im_module);
1976       priv->im_module = g_value_dup_string (value);
1977       if (GTK_IS_IM_MULTICONTEXT (entry->im_context))
1978         gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (entry->im_context), priv->im_module);
1979       break;
1980
1981     case PROP_EDITING_CANCELED:
1982       entry->editing_canceled = g_value_get_boolean (value);
1983       break;
1984
1985     case PROP_SCROLL_OFFSET:
1986     case PROP_CURSOR_POSITION:
1987     default:
1988       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1989       break;
1990     }
1991 }
1992
1993 static void
1994 gtk_entry_get_property (GObject         *object,
1995                         guint            prop_id,
1996                         GValue          *value,
1997                         GParamSpec      *pspec)
1998 {
1999   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
2000   GtkEntry *entry = GTK_ENTRY (object);
2001
2002   switch (prop_id)
2003     {
2004     case PROP_BUFFER:
2005       g_value_set_object (value, gtk_entry_get_buffer (entry));
2006       break;
2007
2008     case PROP_CURSOR_POSITION:
2009       g_value_set_int (value, entry->current_pos);
2010       break;
2011
2012     case PROP_SELECTION_BOUND:
2013       g_value_set_int (value, entry->selection_bound);
2014       break;
2015
2016     case PROP_EDITABLE:
2017       g_value_set_boolean (value, entry->editable);
2018       break;
2019
2020     case PROP_MAX_LENGTH:
2021       g_value_set_int (value, gtk_entry_buffer_get_max_length (get_buffer (entry)));
2022       break;
2023
2024     case PROP_VISIBILITY:
2025       g_value_set_boolean (value, entry->visible);
2026       break;
2027
2028     case PROP_HAS_FRAME:
2029       g_value_set_boolean (value, entry->has_frame);
2030       break;
2031
2032     case PROP_INNER_BORDER:
2033       g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
2034       break;
2035
2036     case PROP_INVISIBLE_CHAR:
2037       g_value_set_uint (value, entry->invisible_char);
2038       break;
2039
2040     case PROP_ACTIVATES_DEFAULT:
2041       g_value_set_boolean (value, entry->activates_default);
2042       break;
2043
2044     case PROP_WIDTH_CHARS:
2045       g_value_set_int (value, entry->width_chars);
2046       break;
2047
2048     case PROP_SCROLL_OFFSET:
2049       g_value_set_int (value, entry->scroll_offset);
2050       break;
2051
2052     case PROP_TEXT:
2053       g_value_set_string (value, gtk_entry_get_text (entry));
2054       break;
2055
2056     case PROP_XALIGN:
2057       g_value_set_float (value, gtk_entry_get_alignment (entry));
2058       break;
2059
2060     case PROP_TRUNCATE_MULTILINE:
2061       g_value_set_boolean (value, entry->truncate_multiline);
2062       break;
2063
2064     case PROP_SHADOW_TYPE:
2065       g_value_set_enum (value, priv->shadow_type);
2066       break;
2067
2068     case PROP_OVERWRITE_MODE:
2069       g_value_set_boolean (value, entry->overwrite_mode);
2070       break;
2071
2072     case PROP_TEXT_LENGTH:
2073       g_value_set_uint (value, gtk_entry_buffer_get_length (get_buffer (entry)));
2074       break;
2075
2076     case PROP_INVISIBLE_CHAR_SET:
2077       g_value_set_boolean (value, priv->invisible_char_set);
2078       break;
2079
2080     case PROP_IM_MODULE:
2081       g_value_set_string (value, priv->im_module);
2082       break;
2083
2084     case PROP_CAPS_LOCK_WARNING:
2085       g_value_set_boolean (value, priv->caps_lock_warning);
2086       break;
2087
2088     case PROP_PROGRESS_FRACTION:
2089       g_value_set_double (value, priv->progress_fraction);
2090       break;
2091
2092     case PROP_PROGRESS_PULSE_STEP:
2093       g_value_set_double (value, priv->progress_pulse_fraction);
2094       break;
2095
2096     case PROP_PIXBUF_PRIMARY:
2097       g_value_set_object (value,
2098                           gtk_entry_get_icon_pixbuf (entry,
2099                                                      GTK_ENTRY_ICON_PRIMARY));
2100       break;
2101
2102     case PROP_PIXBUF_SECONDARY:
2103       g_value_set_object (value,
2104                           gtk_entry_get_icon_pixbuf (entry,
2105                                                      GTK_ENTRY_ICON_SECONDARY));
2106       break;
2107
2108     case PROP_STOCK_PRIMARY:
2109       g_value_set_string (value,
2110                           gtk_entry_get_icon_stock (entry,
2111                                                     GTK_ENTRY_ICON_PRIMARY));
2112       break;
2113
2114     case PROP_STOCK_SECONDARY:
2115       g_value_set_string (value,
2116                           gtk_entry_get_icon_stock (entry,
2117                                                     GTK_ENTRY_ICON_SECONDARY));
2118       break;
2119
2120     case PROP_ICON_NAME_PRIMARY:
2121       g_value_set_string (value,
2122                           gtk_entry_get_icon_name (entry,
2123                                                    GTK_ENTRY_ICON_PRIMARY));
2124       break;
2125
2126     case PROP_ICON_NAME_SECONDARY:
2127       g_value_set_string (value,
2128                           gtk_entry_get_icon_name (entry,
2129                                                    GTK_ENTRY_ICON_SECONDARY));
2130       break;
2131
2132     case PROP_GICON_PRIMARY:
2133       g_value_set_object (value,
2134                           gtk_entry_get_icon_gicon (entry,
2135                                                     GTK_ENTRY_ICON_PRIMARY));
2136       break;
2137
2138     case PROP_GICON_SECONDARY:
2139       g_value_set_object (value,
2140                           gtk_entry_get_icon_gicon (entry,
2141                                                     GTK_ENTRY_ICON_SECONDARY));
2142       break;
2143
2144     case PROP_STORAGE_TYPE_PRIMARY:
2145       g_value_set_enum (value,
2146                         gtk_entry_get_icon_storage_type (entry, 
2147                                                          GTK_ENTRY_ICON_PRIMARY));
2148       break;
2149
2150     case PROP_STORAGE_TYPE_SECONDARY:
2151       g_value_set_enum (value,
2152                         gtk_entry_get_icon_storage_type (entry, 
2153                                                          GTK_ENTRY_ICON_SECONDARY));
2154       break;
2155
2156     case PROP_ACTIVATABLE_PRIMARY:
2157       g_value_set_boolean (value,
2158                            gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_PRIMARY));
2159       break;
2160
2161     case PROP_ACTIVATABLE_SECONDARY:
2162       g_value_set_boolean (value,
2163                            gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY));
2164       break;
2165
2166     case PROP_SENSITIVE_PRIMARY:
2167       g_value_set_boolean (value,
2168                            gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_PRIMARY));
2169       break;
2170
2171     case PROP_SENSITIVE_SECONDARY:
2172       g_value_set_boolean (value,
2173                            gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_SECONDARY));
2174       break;
2175
2176     case PROP_TOOLTIP_TEXT_PRIMARY:
2177       g_value_take_string (value,
2178                            gtk_entry_get_icon_tooltip_text (entry, GTK_ENTRY_ICON_PRIMARY));
2179       break;
2180
2181     case PROP_TOOLTIP_TEXT_SECONDARY:
2182       g_value_take_string (value,
2183                            gtk_entry_get_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY));
2184       break;
2185
2186     case PROP_TOOLTIP_MARKUP_PRIMARY:
2187       g_value_take_string (value,
2188                            gtk_entry_get_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY));
2189       break;
2190
2191     case PROP_TOOLTIP_MARKUP_SECONDARY:
2192       g_value_take_string (value,
2193                            gtk_entry_get_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY));
2194       break;
2195
2196     case PROP_EDITING_CANCELED:
2197       g_value_set_boolean (value,
2198                            entry->editing_canceled);
2199       break;
2200
2201     default:
2202       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2203       break;
2204     }
2205 }
2206
2207 static gunichar
2208 find_invisible_char (GtkWidget *widget)
2209 {
2210   PangoLayout *layout;
2211   PangoAttrList *attr_list;
2212   gint i;
2213   gunichar invisible_chars [] = {
2214     0,
2215     0x25cf, /* BLACK CIRCLE */
2216     0x2022, /* BULLET */
2217     0x2731, /* HEAVY ASTERISK */
2218     0x273a  /* SIXTEEN POINTED ASTERISK */
2219   };
2220
2221   if (gtk_widget_get_style (widget))
2222     gtk_widget_style_get (widget,
2223                           "invisible-char", &invisible_chars[0],
2224                           NULL);
2225
2226   layout = gtk_widget_create_pango_layout (widget, NULL);
2227
2228   attr_list = pango_attr_list_new ();
2229   pango_attr_list_insert (attr_list, pango_attr_fallback_new (FALSE));
2230
2231   pango_layout_set_attributes (layout, attr_list);
2232   pango_attr_list_unref (attr_list);
2233
2234   for (i = (invisible_chars[0] != 0 ? 0 : 1); i < G_N_ELEMENTS (invisible_chars); i++)
2235     {
2236       gchar text[7] = { 0, };
2237       gint len, count;
2238
2239       len = g_unichar_to_utf8 (invisible_chars[i], text);
2240       pango_layout_set_text (layout, text, len);
2241
2242       count = pango_layout_get_unknown_glyphs_count (layout);
2243
2244       if (count == 0)
2245         {
2246           g_object_unref (layout);
2247           return invisible_chars[i];
2248         }
2249     }
2250
2251   g_object_unref (layout);
2252
2253   return '*';
2254 }
2255
2256 static void
2257 gtk_entry_init (GtkEntry *entry)
2258 {
2259   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2260
2261   gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
2262   gtk_widget_set_has_window (GTK_WIDGET (entry), FALSE);
2263
2264   entry->editable = TRUE;
2265   entry->visible = TRUE;
2266   entry->invisible_char = find_invisible_char (GTK_WIDGET (entry));
2267   entry->dnd_position = -1;
2268   entry->width_chars = -1;
2269   entry->is_cell_renderer = FALSE;
2270   entry->editing_canceled = FALSE;
2271   entry->has_frame = TRUE;
2272   entry->truncate_multiline = FALSE;
2273   priv->shadow_type = GTK_SHADOW_IN;
2274   priv->xalign = 0.0;
2275   priv->caps_lock_warning = TRUE;
2276   priv->caps_lock_warning_shown = FALSE;
2277   priv->progress_fraction = 0.0;
2278   priv->progress_pulse_fraction = 0.1;
2279
2280   gtk_drag_dest_set (GTK_WIDGET (entry),
2281                      GTK_DEST_DEFAULT_HIGHLIGHT,
2282                      NULL, 0,
2283                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
2284   gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
2285
2286   /* This object is completely private. No external entity can gain a reference
2287    * to it; so we create it here and destroy it in finalize().
2288    */
2289   entry->im_context = gtk_im_multicontext_new ();
2290   
2291   g_signal_connect (entry->im_context, "commit",
2292                     G_CALLBACK (gtk_entry_commit_cb), entry);
2293   g_signal_connect (entry->im_context, "preedit-changed",
2294                     G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
2295   g_signal_connect (entry->im_context, "retrieve-surrounding",
2296                     G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
2297   g_signal_connect (entry->im_context, "delete-surrounding",
2298                     G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
2299
2300 }
2301
2302 static gint
2303 get_icon_width (GtkEntry             *entry,
2304                 GtkEntryIconPosition  icon_pos)
2305 {
2306   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2307   EntryIconInfo *icon_info = priv->icons[icon_pos];
2308   GdkScreen *screen;
2309   GtkSettings *settings;
2310   gint menu_icon_width;
2311
2312   if (!icon_info || icon_info->pixbuf == NULL)
2313     return 0;
2314
2315   screen = gtk_widget_get_screen (GTK_WIDGET (entry));
2316   settings = gtk_settings_get_for_screen (screen);
2317
2318   gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
2319                                      &menu_icon_width, NULL);
2320
2321   return MAX (gdk_pixbuf_get_width (icon_info->pixbuf), menu_icon_width);
2322 }
2323
2324 static void
2325 get_icon_allocations (GtkEntry      *entry,
2326                       GtkAllocation *primary,
2327                       GtkAllocation *secondary)
2328
2329 {
2330   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2331   gint x, y, width, height;
2332
2333   get_text_area_size (entry, &x, &y, &width, &height);
2334
2335   if (gtk_widget_has_focus (GTK_WIDGET (entry)) && !priv->interior_focus)
2336     y += priv->focus_width;
2337
2338   primary->y = y;
2339   primary->height = height;
2340   primary->width = get_icon_width (entry, GTK_ENTRY_ICON_PRIMARY);
2341   if (primary->width > 0)
2342     primary->width += 2 * priv->icon_margin;
2343
2344   secondary->y = y;
2345   secondary->height = height;
2346   secondary->width = get_icon_width (entry, GTK_ENTRY_ICON_SECONDARY);
2347   if (secondary->width > 0)
2348     secondary->width += 2 * priv->icon_margin;
2349
2350   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
2351     {
2352       primary->x = x + width - primary->width;
2353       secondary->x = x;
2354     }
2355   else
2356     {
2357       primary->x = x;
2358       secondary->x = x + width - secondary->width;
2359     }
2360 }
2361
2362
2363 static void
2364 begin_change (GtkEntry *entry)
2365 {
2366   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2367
2368   priv->change_count++;
2369 }
2370
2371 static void
2372 end_change (GtkEntry *entry)
2373 {
2374   GtkEditable *editable = GTK_EDITABLE (entry);
2375   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2376  
2377   g_return_if_fail (priv->change_count > 0);
2378
2379   priv->change_count--;
2380
2381   if (priv->change_count == 0)
2382     {
2383        if (priv->real_changed) 
2384          {
2385            g_signal_emit_by_name (editable, "changed");
2386            priv->real_changed = FALSE;
2387          }
2388     } 
2389 }
2390
2391 static void
2392 emit_changed (GtkEntry *entry)
2393 {
2394   GtkEditable *editable = GTK_EDITABLE (entry);
2395   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2396
2397   if (priv->change_count == 0)
2398     g_signal_emit_by_name (editable, "changed");
2399   else 
2400     priv->real_changed = TRUE;
2401 }
2402
2403 static void
2404 gtk_entry_destroy (GtkWidget *widget)
2405 {
2406   GtkEntry *entry = GTK_ENTRY (widget);
2407
2408   entry->current_pos = entry->selection_bound = 0;
2409   _gtk_entry_reset_im_context (entry);
2410   gtk_entry_reset_layout (entry);
2411
2412   if (entry->blink_timeout)
2413     {
2414       g_source_remove (entry->blink_timeout);
2415       entry->blink_timeout = 0;
2416     }
2417
2418   if (entry->recompute_idle)
2419     {
2420       g_source_remove (entry->recompute_idle);
2421       entry->recompute_idle = 0;
2422     }
2423
2424   GTK_WIDGET_CLASS (gtk_entry_parent_class)->destroy (widget);
2425 }
2426
2427 static void
2428 gtk_entry_dispose (GObject *object)
2429 {
2430   GtkEntry *entry = GTK_ENTRY (object);
2431   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2432
2433   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2434   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2435   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2436   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2437
2438   if (priv->buffer)
2439     {
2440       buffer_disconnect_signals (entry);
2441       g_object_unref (priv->buffer);
2442       priv->buffer = NULL;
2443     }
2444
2445   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2446 }
2447
2448 static void
2449 gtk_entry_finalize (GObject *object)
2450 {
2451   GtkEntry *entry = GTK_ENTRY (object);
2452   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2453   EntryIconInfo *icon_info = NULL;
2454   gint i;
2455
2456   for (i = 0; i < MAX_ICONS; i++)
2457     {
2458       if ((icon_info = priv->icons[i]) != NULL)
2459         {
2460           if (icon_info->target_list != NULL)
2461             {
2462               gtk_target_list_unref (icon_info->target_list);
2463               icon_info->target_list = NULL;
2464             }
2465
2466           g_slice_free (EntryIconInfo, icon_info);
2467           priv->icons[i] = NULL;
2468         }
2469     }
2470
2471   gtk_entry_set_completion (entry, NULL);
2472
2473   if (entry->cached_layout)
2474     g_object_unref (entry->cached_layout);
2475
2476   g_object_unref (entry->im_context);
2477
2478   if (entry->blink_timeout)
2479     g_source_remove (entry->blink_timeout);
2480
2481   if (entry->recompute_idle)
2482     g_source_remove (entry->recompute_idle);
2483
2484   g_free (priv->im_module);
2485
2486   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2487 }
2488
2489 static DisplayMode
2490 gtk_entry_get_display_mode (GtkEntry *entry)
2491 {
2492   GtkEntryPrivate *priv;
2493   if (entry->visible)
2494     return DISPLAY_NORMAL;
2495   priv = GTK_ENTRY_GET_PRIVATE (entry);
2496   if (entry->invisible_char == 0 && priv->invisible_char_set)
2497     return DISPLAY_BLANK;
2498   return DISPLAY_INVISIBLE;
2499 }
2500
2501 static gchar*
2502 gtk_entry_get_display_text (GtkEntry *entry,
2503                             gint      start_pos,
2504                             gint      end_pos)
2505 {
2506   GtkEntryPasswordHint *password_hint;
2507   GtkEntryPrivate *priv;
2508   gunichar invisible_char;
2509   const gchar *start;
2510   const gchar *end;
2511   const gchar *text;
2512   gchar char_str[7];
2513   gint char_len;
2514   GString *str;
2515   guint length;
2516   gint i;
2517
2518   priv = GTK_ENTRY_GET_PRIVATE (entry);
2519   text = gtk_entry_buffer_get_text (get_buffer (entry));
2520   length = gtk_entry_buffer_get_length (get_buffer (entry));
2521
2522   if (end_pos < 0)
2523     end_pos = length;
2524   if (start_pos > length)
2525     start_pos = length;
2526
2527   if (end_pos <= start_pos)
2528       return g_strdup ("");
2529   else if (entry->visible)
2530     {
2531       start = g_utf8_offset_to_pointer (text, start_pos);
2532       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2533       return g_strndup (start, end - start);
2534     }
2535   else
2536     {
2537       str = g_string_sized_new (length * 2);
2538
2539       /* Figure out what our invisible char is and encode it */
2540       if (!entry->invisible_char)
2541           invisible_char = priv->invisible_char_set ? ' ' : '*';
2542       else
2543           invisible_char = entry->invisible_char;
2544       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2545
2546       /*
2547        * Add hidden characters for each character in the text
2548        * buffer. If there is a password hint, then keep that
2549        * character visible.
2550        */
2551
2552       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2553       for (i = start_pos; i < end_pos; ++i)
2554         {
2555           if (password_hint && i == password_hint->position)
2556             {
2557               start = g_utf8_offset_to_pointer (text, i);
2558               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2559             }
2560           else
2561             {
2562               g_string_append_len (str, char_str, char_len);
2563             }
2564         }
2565
2566       return g_string_free (str, FALSE);
2567     }
2568
2569 }
2570
2571 static void
2572 update_cursors (GtkWidget *widget)
2573 {
2574   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2575   EntryIconInfo *icon_info = NULL;
2576   GdkDisplay *display;
2577   GdkCursor *cursor;
2578   gint i;
2579
2580   for (i = 0; i < MAX_ICONS; i++)
2581     {
2582       if ((icon_info = priv->icons[i]) != NULL)
2583         {
2584           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2585             gdk_window_show_unraised (icon_info->window);
2586
2587           /* The icon windows are not children of the visible entry window,
2588            * thus we can't just inherit the xterm cursor. Slight complication 
2589            * here is that for the entry, insensitive => arrow cursor, but for 
2590            * an icon in a sensitive entry, insensitive => xterm cursor.
2591            */
2592           if (gtk_widget_is_sensitive (widget) &&
2593               (icon_info->insensitive || 
2594                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2595             {
2596               display = gtk_widget_get_display (widget);
2597               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2598               gdk_window_set_cursor (icon_info->window, cursor);
2599               gdk_cursor_unref (cursor);
2600             }
2601           else
2602             {
2603               gdk_window_set_cursor (icon_info->window, NULL);
2604             }
2605         }
2606     }
2607 }
2608
2609 static void
2610 realize_icon_info (GtkWidget            *widget, 
2611                    GtkEntryIconPosition  icon_pos)
2612 {
2613   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2614   EntryIconInfo *icon_info = priv->icons[icon_pos];
2615   GdkWindowAttr attributes;
2616   gint attributes_mask;
2617
2618   g_return_if_fail (icon_info != NULL);
2619
2620   attributes.x = 0;
2621   attributes.y = 0;
2622   attributes.width = 1;
2623   attributes.height = 1;
2624   attributes.window_type = GDK_WINDOW_CHILD;
2625   attributes.wclass = GDK_INPUT_ONLY;
2626   attributes.event_mask = gtk_widget_get_events (widget);
2627   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2628                                 GDK_BUTTON_RELEASE_MASK |
2629                                 GDK_BUTTON1_MOTION_MASK |
2630                                 GDK_BUTTON3_MOTION_MASK |
2631                                 GDK_POINTER_MOTION_HINT_MASK |
2632                                 GDK_POINTER_MOTION_MASK |
2633                                 GDK_ENTER_NOTIFY_MASK |
2634                             GDK_LEAVE_NOTIFY_MASK);
2635   attributes_mask = GDK_WA_X | GDK_WA_Y;
2636
2637   icon_info->window = gdk_window_new (gtk_widget_get_window (widget),
2638                                       &attributes,
2639                                       attributes_mask);
2640   gdk_window_set_user_data (icon_info->window, widget);
2641
2642   gtk_widget_queue_resize (widget);
2643 }
2644
2645 static EntryIconInfo*
2646 construct_icon_info (GtkWidget            *widget, 
2647                      GtkEntryIconPosition  icon_pos)
2648 {
2649   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2650   EntryIconInfo *icon_info;
2651
2652   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2653
2654   icon_info = g_slice_new0 (EntryIconInfo);
2655   priv->icons[icon_pos] = icon_info;
2656
2657   if (gtk_widget_get_realized (widget))
2658     realize_icon_info (widget, icon_pos);
2659
2660   return icon_info;
2661 }
2662
2663 static void
2664 gtk_entry_map (GtkWidget *widget)
2665 {
2666   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2667   EntryIconInfo *icon_info = NULL;
2668   gint i;
2669
2670   GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2671
2672   for (i = 0; i < MAX_ICONS; i++)
2673     {
2674       if ((icon_info = priv->icons[i]) != NULL)
2675         {
2676           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2677             gdk_window_show (icon_info->window);
2678         }
2679     }
2680
2681   update_cursors (widget);
2682 }
2683
2684 static void
2685 gtk_entry_unmap (GtkWidget *widget)
2686 {
2687   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2688   EntryIconInfo *icon_info = NULL;
2689   gint i;
2690
2691   for (i = 0; i < MAX_ICONS; i++)
2692     {
2693       if ((icon_info = priv->icons[i]) != NULL)
2694         {
2695           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2696             gdk_window_hide (icon_info->window);
2697         }
2698     }
2699
2700   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2701 }
2702
2703 static void
2704 gtk_entry_realize (GtkWidget *widget)
2705 {
2706   GtkEntry *entry;
2707   GtkEntryPrivate *priv;
2708   EntryIconInfo *icon_info;
2709   GdkWindow *window;
2710   GdkWindowAttr attributes;
2711   gint attributes_mask;
2712   gint frame_x, frame_y;
2713   int i;
2714
2715   gtk_widget_set_realized (widget, TRUE);
2716   window = gtk_widget_get_parent_window (widget);
2717   gtk_widget_set_window (widget, window);
2718   g_object_ref (window);
2719
2720   entry = GTK_ENTRY (widget);
2721   priv = GTK_ENTRY_GET_PRIVATE (entry);
2722
2723   attributes.window_type = GDK_WINDOW_CHILD;
2724   attributes.wclass = GDK_INPUT_ONLY;
2725   attributes.event_mask = gtk_widget_get_events (widget);
2726   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2727                             GDK_BUTTON_RELEASE_MASK |
2728                             GDK_BUTTON1_MOTION_MASK |
2729                             GDK_BUTTON3_MOTION_MASK |
2730                             GDK_POINTER_MOTION_HINT_MASK |
2731                             GDK_POINTER_MOTION_MASK |
2732                             GDK_ENTER_NOTIFY_MASK |
2733                             GDK_LEAVE_NOTIFY_MASK);
2734   attributes_mask = GDK_WA_X | GDK_WA_Y;
2735
2736   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2737
2738   get_frame_size (entry, &frame_x, &frame_y, NULL, NULL);
2739   attributes.x += frame_x;
2740   attributes.y += frame_y;
2741
2742   if (gtk_widget_is_sensitive (widget))
2743     {
2744       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2745       attributes_mask |= GDK_WA_CURSOR;
2746     }
2747
2748   entry->text_area = gdk_window_new (gtk_widget_get_window (widget),
2749                                      &attributes,
2750                                      attributes_mask);
2751
2752   gdk_window_set_user_data (entry->text_area, entry);
2753
2754   if (attributes_mask & GDK_WA_CURSOR)
2755     gdk_cursor_unref (attributes.cursor);
2756
2757   gtk_widget_style_attach (widget);
2758
2759   gdk_window_show (entry->text_area);
2760
2761   gtk_im_context_set_client_window (entry->im_context, entry->text_area);
2762
2763   gtk_entry_adjust_scroll (entry);
2764   gtk_entry_update_primary_selection (entry);
2765
2766
2767   /* If the icon positions are already setup, create their windows.
2768    * Otherwise if they don't exist yet, then construct_icon_info()
2769    * will create the windows once the widget is already realized.
2770    */
2771   for (i = 0; i < MAX_ICONS; i++)
2772     {
2773       if ((icon_info = priv->icons[i]) != NULL)
2774         {
2775           if (icon_info->window == NULL)
2776             realize_icon_info (widget, i);
2777         }
2778     }
2779 }
2780
2781 static void
2782 gtk_entry_unrealize (GtkWidget *widget)
2783 {
2784   GtkEntry *entry = GTK_ENTRY (widget);
2785   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2786   GtkClipboard *clipboard;
2787   EntryIconInfo *icon_info;
2788   gint i;
2789
2790   gtk_entry_reset_layout (entry);
2791   
2792   gtk_im_context_set_client_window (entry->im_context, NULL);
2793
2794   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2795   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2796     gtk_clipboard_clear (clipboard);
2797   
2798   if (entry->text_area)
2799     {
2800       gdk_window_set_user_data (entry->text_area, NULL);
2801       gdk_window_destroy (entry->text_area);
2802       entry->text_area = NULL;
2803     }
2804
2805   if (entry->popup_menu)
2806     {
2807       gtk_widget_destroy (entry->popup_menu);
2808       entry->popup_menu = NULL;
2809     }
2810
2811   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
2812
2813   for (i = 0; i < MAX_ICONS; i++)
2814     {
2815       if ((icon_info = priv->icons[i]) != NULL)
2816         {
2817           if (icon_info->window != NULL)
2818             {
2819               gdk_window_destroy (icon_info->window);
2820               icon_info->window = NULL;
2821             }
2822         }
2823     }
2824 }
2825
2826 void
2827 _gtk_entry_get_borders (GtkEntry *entry,
2828                         gint     *xborder,
2829                         gint     *yborder)
2830 {
2831   GtkWidget *widget = GTK_WIDGET (entry);
2832   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2833   GtkStyle *style;
2834
2835   if (entry->has_frame)
2836     {
2837       style = gtk_widget_get_style (widget);
2838
2839       *xborder = style->xthickness;
2840       *yborder = style->ythickness;
2841     }
2842   else
2843     {
2844       *xborder = 0;
2845       *yborder = 0;
2846     }
2847
2848   if (!priv->interior_focus)
2849     {
2850       *xborder += priv->focus_width;
2851       *yborder += priv->focus_width;
2852     }
2853 }
2854
2855 static void
2856 gtk_entry_size_request (GtkWidget      *widget,
2857                         GtkRequisition *requisition)
2858 {
2859   GtkEntry *entry = GTK_ENTRY (widget);
2860   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2861   PangoFontMetrics *metrics;
2862   gint xborder, yborder;
2863   GtkBorder inner_border;
2864   PangoContext *context;
2865   int icon_widths = 0;
2866   int icon_width, i;
2867   
2868   gtk_widget_ensure_style (widget);
2869   context = gtk_widget_get_pango_context (widget);
2870   metrics = pango_context_get_metrics (context,
2871                                        gtk_widget_get_style (widget)->font_desc,
2872                                        pango_context_get_language (context));
2873
2874   entry->ascent = pango_font_metrics_get_ascent (metrics);
2875   entry->descent = pango_font_metrics_get_descent (metrics);
2876   
2877   _gtk_entry_get_borders (entry, &xborder, &yborder);
2878   _gtk_entry_effective_inner_border (entry, &inner_border);
2879
2880   if (entry->width_chars < 0)
2881     requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
2882   else
2883     {
2884       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
2885       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2886       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
2887       
2888       requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
2889     }
2890     
2891   requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
2892
2893   for (i = 0; i < MAX_ICONS; i++)
2894     {
2895       icon_width = get_icon_width (entry, i);
2896       if (icon_width > 0)
2897         icon_widths += icon_width + 2 * priv->icon_margin;
2898     }
2899
2900   if (icon_widths > requisition->width)
2901     requisition->width += icon_widths;
2902
2903   pango_font_metrics_unref (metrics);
2904 }
2905
2906 static void
2907 place_windows (GtkEntry *entry)
2908 {
2909   GtkWidget *widget = GTK_WIDGET (entry);
2910   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2911   gint x, y, width, height;
2912   gint frame_x, frame_y;
2913   GtkAllocation primary;
2914   GtkAllocation secondary;
2915   EntryIconInfo *icon_info = NULL;
2916
2917   get_frame_size (entry, &frame_x, &frame_y, NULL, NULL);
2918   get_text_area_size (entry, &x, &y, &width, &height);
2919   get_icon_allocations (entry, &primary, &secondary);
2920
2921   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
2922     y += priv->focus_width;
2923
2924   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
2925     x += secondary.width;
2926   else
2927     x += primary.width;
2928   width -= primary.width + secondary.width;
2929
2930   x += frame_x;
2931   y += frame_y;
2932   primary.x += frame_x;
2933   primary.y += frame_y;
2934   secondary.x += frame_x;
2935   secondary.y += frame_y;
2936
2937   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
2938     gdk_window_move_resize (icon_info->window,
2939                             primary.x, primary.y,
2940                             primary.width, primary.height);
2941
2942   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
2943     gdk_window_move_resize (icon_info->window,
2944                             secondary.x, secondary.y,
2945                             secondary.width, secondary.height);
2946
2947   gdk_window_move_resize (entry->text_area, x, y, width, height);
2948 }
2949
2950 static void
2951 gtk_entry_get_text_area_size (GtkEntry *entry,
2952                               gint     *x,
2953                               gint     *y,
2954                               gint     *width,
2955                               gint     *height)
2956 {
2957   GtkWidget *widget = GTK_WIDGET (entry);
2958   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2959   GtkAllocation allocation;
2960   GtkRequisition requisition;
2961   gint frame_height;
2962   gint xborder, yborder;
2963
2964   gtk_widget_get_preferred_size (widget, &requisition, NULL);
2965   gtk_widget_get_allocation (widget, &allocation);
2966   _gtk_entry_get_borders (entry, &xborder, &yborder);
2967
2968   if (gtk_widget_get_realized (widget))
2969     get_frame_size (entry, NULL, NULL, NULL, &frame_height);
2970   else
2971     frame_height = requisition.height;
2972
2973   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
2974     frame_height -= 2 * priv->focus_width;
2975
2976   if (x)
2977     *x = xborder;
2978
2979   if (y)
2980     *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
2981
2982   if (width)
2983     *width = allocation.width - xborder * 2;
2984
2985   if (height)
2986     *height = requisition.height - yborder * 2;
2987 }
2988
2989 static void
2990 get_text_area_size (GtkEntry *entry,
2991                     gint     *x,
2992                     gint     *y,
2993                     gint     *width,
2994                     gint     *height)
2995 {
2996   GtkEntryClass *class;
2997
2998   g_return_if_fail (GTK_IS_ENTRY (entry));
2999
3000   class = GTK_ENTRY_GET_CLASS (entry);
3001
3002   if (class->get_text_area_size)
3003     class->get_text_area_size (entry, x, y, width, height);
3004 }
3005
3006
3007 static void
3008 get_frame_size (GtkEntry *entry,
3009                 gint     *x,
3010                 gint     *y,
3011                 gint     *width,
3012                 gint     *height)
3013 {
3014   GtkAllocation allocation;
3015   GtkRequisition requisition;
3016   GtkWidget *widget = GTK_WIDGET (entry);
3017
3018   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3019   gtk_widget_get_allocation (widget, &allocation);
3020
3021   if (x)
3022     *x = allocation.x;
3023
3024   if (y)
3025     {
3026       if (entry->is_cell_renderer)
3027         *y = allocation.y;
3028       else
3029         *y = allocation.y + (allocation.height - requisition.height) / 2;
3030     }
3031
3032   if (width)
3033     *width = allocation.width;
3034
3035   if (height)
3036     {
3037       if (entry->is_cell_renderer)
3038         *height = allocation.height;
3039       else
3040         *height = requisition.height;
3041     }
3042 }
3043
3044 void
3045 _gtk_entry_effective_inner_border (GtkEntry  *entry,
3046                                    GtkBorder *border)
3047 {
3048   GtkBorder *tmp_border;
3049
3050   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
3051
3052   if (tmp_border)
3053     {
3054       *border = *tmp_border;
3055       return;
3056     }
3057
3058   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
3059
3060   if (tmp_border)
3061     {
3062       *border = *tmp_border;
3063       gtk_border_free (tmp_border);
3064       return;
3065     }
3066
3067   *border = default_inner_border;
3068 }
3069
3070 static void
3071 gtk_entry_size_allocate (GtkWidget     *widget,
3072                          GtkAllocation *allocation)
3073 {
3074   GtkEntry *entry = GTK_ENTRY (widget);
3075
3076   gtk_widget_set_allocation (widget, allocation);
3077
3078   if (gtk_widget_get_realized (widget))
3079     {
3080       GtkEntryCompletion* completion;
3081
3082       place_windows (entry);
3083       gtk_entry_recompute (entry);
3084
3085       completion = gtk_entry_get_completion (entry);
3086       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3087         _gtk_entry_completion_resize_popup (completion);
3088     }
3089 }
3090
3091 /* Kudos to the gnome-panel guys. */
3092 static void
3093 colorshift_pixbuf (GdkPixbuf *dest,
3094                    GdkPixbuf *src,
3095                    gint       shift)
3096 {
3097   gint i, j;
3098   gint width, height, has_alpha, src_rowstride, dest_rowstride;
3099   guchar *target_pixels;
3100   guchar *original_pixels;
3101   guchar *pix_src;
3102   guchar *pix_dest;
3103   int val;
3104   guchar r, g, b;
3105
3106   has_alpha       = gdk_pixbuf_get_has_alpha (src);
3107   width           = gdk_pixbuf_get_width (src);
3108   height          = gdk_pixbuf_get_height (src);
3109   src_rowstride   = gdk_pixbuf_get_rowstride (src);
3110   dest_rowstride  = gdk_pixbuf_get_rowstride (dest);
3111   original_pixels = gdk_pixbuf_get_pixels (src);
3112   target_pixels   = gdk_pixbuf_get_pixels (dest);
3113
3114   for (i = 0; i < height; i++)
3115     {
3116       pix_dest = target_pixels   + i * dest_rowstride;
3117       pix_src  = original_pixels + i * src_rowstride;
3118
3119       for (j = 0; j < width; j++)
3120         {
3121           r = *(pix_src++);
3122           g = *(pix_src++);
3123           b = *(pix_src++);
3124
3125           val = r + shift;
3126           *(pix_dest++) = CLAMP (val, 0, 255);
3127
3128           val = g + shift;
3129           *(pix_dest++) = CLAMP (val, 0, 255);
3130
3131           val = b + shift;
3132           *(pix_dest++) = CLAMP (val, 0, 255);
3133
3134           if (has_alpha)
3135             *(pix_dest++) = *(pix_src++);
3136         }
3137     }
3138 }
3139
3140 static gboolean
3141 should_prelight (GtkEntry             *entry,
3142                  GtkEntryIconPosition  icon_pos)
3143 {
3144   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3145   EntryIconInfo *icon_info = priv->icons[icon_pos];
3146   gboolean prelight;
3147
3148   if (!icon_info) 
3149     return FALSE;
3150
3151   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3152     return FALSE;
3153
3154   if (icon_info->pressed)
3155     return FALSE;
3156
3157   gtk_widget_style_get (GTK_WIDGET (entry),
3158                         "icon-prelight", &prelight,
3159                         NULL);
3160
3161   return prelight;
3162 }
3163
3164 static void
3165 draw_icon (GtkWidget            *widget,
3166            cairo_t              *cr,
3167            GtkEntryIconPosition  icon_pos)
3168 {
3169   GtkEntry *entry = GTK_ENTRY (widget);
3170   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3171   EntryIconInfo *icon_info = priv->icons[icon_pos];
3172   GdkPixbuf *pixbuf;
3173   gint x, y, width, height;
3174
3175   if (!icon_info)
3176     return;
3177
3178   gtk_entry_ensure_pixbuf (entry, icon_pos);
3179
3180   if (icon_info->pixbuf == NULL)
3181     return;
3182
3183   width = gdk_window_get_width (icon_info->window);
3184   height = gdk_window_get_height (icon_info->window);
3185
3186   /* size_allocate hasn't been called yet. These are the default values.
3187    */
3188   if (width == 1 || height == 1)
3189     return;
3190
3191   pixbuf = icon_info->pixbuf;
3192   g_object_ref (pixbuf);
3193
3194   if (gdk_pixbuf_get_height (pixbuf) > height)
3195     {
3196       GdkPixbuf *temp_pixbuf;
3197       gint scale;
3198
3199       scale = height - 2 * priv->icon_margin;
3200       temp_pixbuf = gdk_pixbuf_scale_simple (pixbuf, scale, scale,
3201                                              GDK_INTERP_BILINEAR);
3202       g_object_unref (pixbuf);
3203       pixbuf = temp_pixbuf;
3204     }
3205
3206   x = (width  - gdk_pixbuf_get_width (pixbuf)) / 2;
3207   y = (height - gdk_pixbuf_get_height (pixbuf)) / 2;
3208
3209   if (!gtk_widget_is_sensitive (widget) ||
3210       icon_info->insensitive)
3211     {
3212       GdkPixbuf *temp_pixbuf;
3213
3214       temp_pixbuf = gdk_pixbuf_copy (pixbuf);
3215       gdk_pixbuf_saturate_and_pixelate (pixbuf,
3216                                         temp_pixbuf,
3217                                         0.8f,
3218                                         TRUE);
3219       g_object_unref (pixbuf);
3220       pixbuf = temp_pixbuf;
3221     }
3222   else if (icon_info->prelight)
3223     {
3224       GdkPixbuf *temp_pixbuf;
3225
3226       temp_pixbuf = gdk_pixbuf_copy (pixbuf);
3227       colorshift_pixbuf (temp_pixbuf, pixbuf, 30);
3228       g_object_unref (pixbuf);
3229       pixbuf = temp_pixbuf;
3230     }
3231
3232   gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
3233   cairo_paint (cr);
3234
3235   g_object_unref (pixbuf);
3236 }
3237
3238
3239 static void
3240 gtk_entry_draw_frame (GtkWidget      *widget,
3241                       cairo_t        *cr)
3242 {
3243   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
3244   GtkStyle *style;
3245   GdkWindow *window;
3246   gint x = 0, y = 0, width, height;
3247   GtkStateType state;
3248   GtkAllocation allocation;
3249   gint frame_x, frame_y;
3250
3251   cairo_save (cr);
3252
3253   window = gtk_widget_get_window (widget);
3254
3255   get_frame_size (GTK_ENTRY (widget), &frame_x, &frame_y, &width, &height);
3256   gtk_widget_get_allocation (widget, &allocation);
3257
3258   cairo_translate (cr, frame_x - allocation.x, frame_y - allocation.y);
3259
3260   /* Fix a problem with some themes which assume that entry->text_area's
3261    * width equals widget->window's width
3262    * http://bugzilla.gnome.org/show_bug.cgi?id=466000 */
3263   if (GTK_IS_SPIN_BUTTON (widget))
3264     {
3265       gint xborder, yborder;
3266
3267       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3268       _gtk_entry_get_borders (GTK_ENTRY (widget), &xborder, &yborder);
3269
3270       x -= xborder;
3271       width += xborder * 2;
3272     }
3273
3274   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3275     {
3276       x += priv->focus_width;
3277       y += priv->focus_width;
3278       width -= 2 * priv->focus_width;
3279       height -= 2 * priv->focus_width;
3280     }
3281
3282   style = gtk_widget_get_style (widget);
3283   state = gtk_widget_has_focus (widget) ?
3284     GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
3285
3286   gtk_paint_flat_box (style, cr,
3287                       state, GTK_SHADOW_NONE,
3288                       widget, "entry_bg",
3289                       x, y, width, height);
3290
3291   gtk_paint_shadow (style, cr,
3292                     state, priv->shadow_type,
3293                     widget, "entry", x, y, width, height);
3294
3295
3296   gtk_entry_draw_progress (widget, cr);
3297
3298   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3299     {
3300       x -= priv->focus_width;
3301       y -= priv->focus_width;
3302       width += 2 * priv->focus_width;
3303       height += 2 * priv->focus_width;
3304
3305       gtk_paint_focus (style, cr,
3306                        gtk_widget_get_state (widget),
3307                        widget, "entry",
3308                        0, 0, width, height);
3309     }
3310
3311   cairo_restore (cr);
3312 }
3313
3314 static void
3315 gtk_entry_get_progress_border (GtkWidget *widget,
3316                                GtkBorder *progress_border)
3317 {
3318   GtkBorder *tmp_border;
3319   GtkStyle *style;
3320
3321   gtk_widget_style_get (widget, "progress-border", &tmp_border, NULL);
3322   if (tmp_border)
3323     {
3324       *progress_border = *tmp_border;
3325       gtk_border_free (tmp_border);
3326     }
3327   else
3328     {
3329       style = gtk_widget_get_style (widget);
3330
3331       progress_border->left = style->xthickness;
3332       progress_border->right = style->xthickness;
3333       progress_border->top = style->ythickness;
3334       progress_border->bottom = style->ythickness;
3335     }
3336 }
3337
3338 static void
3339 get_progress_area (GtkWidget *widget,
3340                   gint       *x,
3341                   gint       *y,
3342                   gint       *width,
3343                   gint       *height)
3344 {
3345   GtkEntryPrivate *private = GTK_ENTRY_GET_PRIVATE (widget);
3346   GtkEntry *entry = GTK_ENTRY (widget);
3347   GtkBorder progress_border;
3348   gint frame_width, frame_height;
3349
3350   get_frame_size (entry, NULL, NULL, &frame_width, &frame_height);
3351   gtk_entry_get_progress_border (widget, &progress_border);
3352
3353   *x = progress_border.left;
3354   *y = progress_border.top;
3355
3356   *width = frame_width
3357            - progress_border.left - progress_border.right;
3358   *height = frame_height
3359             - progress_border.top - progress_border.bottom;
3360
3361   if (gtk_widget_has_focus (widget) && !private->interior_focus)
3362     {
3363       *x += private->focus_width;
3364       *y += private->focus_width;
3365       *width -= 2 * private->focus_width;
3366       *height -= 2 * private->focus_width;
3367     }
3368
3369   if (private->progress_pulse_mode)
3370     {
3371       gdouble value = private->progress_pulse_current;
3372
3373       *x += (gint) floor(value * (*width));
3374       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3375     }
3376   else if (private->progress_fraction > 0)
3377     {
3378       gdouble value = private->progress_fraction;
3379
3380       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3381         {
3382           gint bar_width;
3383
3384           bar_width = floor(value * (*width) + 0.5);
3385           *x += *width - bar_width;
3386           *width = bar_width;
3387         }
3388       else
3389         {
3390           *width = (gint) floor(value * (*width) + 0.5);
3391         }
3392     }
3393   else
3394     {
3395       *width = 0;
3396       *height = 0;
3397     }
3398 }
3399
3400 static void
3401 gtk_entry_draw_progress (GtkWidget      *widget,
3402                          cairo_t        *cr)
3403 {
3404   gint x, y, width, height;
3405   GtkStateType state;
3406
3407   get_progress_area (widget, &x, &y, &width, &height);
3408
3409   if ((width <= 0) || (height <= 0))
3410     return;
3411
3412   state = GTK_STATE_SELECTED;
3413   if (!gtk_widget_get_sensitive (widget))
3414     state = GTK_STATE_INSENSITIVE;
3415
3416   gtk_paint_box (gtk_widget_get_style (widget), cr,
3417                  state, GTK_SHADOW_OUT,
3418                  widget, "entry-progress",
3419                  x, y,
3420                  width, height);
3421 }
3422
3423 static gint
3424 gtk_entry_draw (GtkWidget *widget,
3425                 cairo_t   *cr)
3426 {
3427   GtkEntry *entry = GTK_ENTRY (widget);
3428   GtkStyle *style;
3429   GtkStateType state;
3430   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3431   int i;
3432
3433   style = gtk_widget_get_style (widget);
3434
3435   state = gtk_widget_has_focus (widget) ?
3436     GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
3437
3438   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3439     {
3440       /* Draw entry_bg, shadow, progress and focus */
3441       gtk_entry_draw_frame (widget, cr);
3442
3443       /* Draw text and cursor */
3444       cairo_save (cr);
3445
3446       gtk_cairo_transform_to_window (cr, widget, entry->text_area);
3447
3448       if (entry->dnd_position != -1)
3449         gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3450       
3451       gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3452
3453       /* When no text is being displayed at all, don't show the cursor */
3454       if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3455           gtk_widget_has_focus (widget) &&
3456           entry->selection_bound == entry->current_pos && entry->cursor_visible)
3457         gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3458
3459       cairo_restore (cr);
3460
3461       /* Draw icons */
3462       for (i = 0; i < MAX_ICONS; i++)
3463         {
3464           EntryIconInfo *icon_info = priv->icons[i];
3465
3466           if (icon_info != NULL)
3467             {
3468               cairo_save (cr);
3469
3470               gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3471
3472               draw_icon (widget, cr, i);
3473
3474               cairo_restore (cr);
3475             }
3476         }
3477     }
3478
3479   return FALSE;
3480 }
3481
3482 static gint
3483 gtk_entry_enter_notify (GtkWidget *widget,
3484                         GdkEventCrossing *event)
3485 {
3486   GtkEntry *entry = GTK_ENTRY (widget);
3487   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3488   gint i;
3489
3490   for (i = 0; i < MAX_ICONS; i++)
3491     {
3492       EntryIconInfo *icon_info = priv->icons[i];
3493
3494       if (icon_info != NULL && event->window == icon_info->window)
3495         {
3496           if (should_prelight (entry, i))
3497             {
3498               icon_info->prelight = TRUE;
3499               gtk_widget_queue_draw (widget);
3500             }
3501
3502           break;
3503         }
3504     }
3505
3506     return FALSE;
3507 }
3508
3509 static gint
3510 gtk_entry_leave_notify (GtkWidget        *widget,
3511                         GdkEventCrossing *event)
3512 {
3513   GtkEntry *entry = GTK_ENTRY (widget);
3514   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3515   gint i;
3516
3517   for (i = 0; i < MAX_ICONS; i++)
3518     {
3519       EntryIconInfo *icon_info = priv->icons[i];
3520
3521       if (icon_info != NULL && event->window == icon_info->window)
3522         {
3523           /* a grab means that we may never see the button release */
3524           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3525             icon_info->pressed = FALSE;
3526
3527           if (should_prelight (entry, i))
3528             {
3529               icon_info->prelight = FALSE;
3530               gtk_widget_queue_draw (widget);
3531             }
3532
3533           break;
3534         }
3535     }
3536
3537   return FALSE;
3538 }
3539
3540 static void
3541 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3542                             gint     **ranges,
3543                             gint      *n_ranges)
3544 {
3545   gint start_char, end_char;
3546
3547   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3548     {
3549       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3550       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3551       const char *text = pango_layout_get_text (layout);
3552       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3553       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3554       gint real_n_ranges, i;
3555
3556       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3557
3558       if (ranges)
3559         {
3560           gint *r = *ranges;
3561           
3562           for (i = 0; i < real_n_ranges; ++i)
3563             {
3564               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3565               r[2 * i] = r[2 * i] / PANGO_SCALE;
3566             }
3567         }
3568       
3569       if (n_ranges)
3570         *n_ranges = real_n_ranges;
3571     }
3572   else
3573     {
3574       if (n_ranges)
3575         *n_ranges = 0;
3576       if (ranges)
3577         *ranges = NULL;
3578     }
3579 }
3580
3581 static gboolean
3582 in_selection (GtkEntry *entry,
3583               gint      x)
3584 {
3585   gint *ranges;
3586   gint n_ranges, i;
3587   gint retval = FALSE;
3588
3589   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3590
3591   for (i = 0; i < n_ranges; ++i)
3592     {
3593       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3594         {
3595           retval = TRUE;
3596           break;
3597         }
3598     }
3599
3600   g_free (ranges);
3601   return retval;
3602 }
3603               
3604 static gint
3605 gtk_entry_button_press (GtkWidget      *widget,
3606                         GdkEventButton *event)
3607 {
3608   GtkEntry *entry = GTK_ENTRY (widget);
3609   GtkEditable *editable = GTK_EDITABLE (widget);
3610   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3611   EntryIconInfo *icon_info = NULL;
3612   gint tmp_pos;
3613   gint sel_start, sel_end;
3614   gint i;
3615
3616   for (i = 0; i < MAX_ICONS; i++)
3617     {
3618       icon_info = priv->icons[i];
3619
3620       if (!icon_info || icon_info->insensitive)
3621         continue;
3622
3623       if (event->window == icon_info->window)
3624         {
3625           if (should_prelight (entry, i))
3626             {
3627               icon_info->prelight = FALSE;
3628               gtk_widget_queue_draw (widget);
3629             }
3630
3631           priv->start_x = event->x;
3632           priv->start_y = event->y;
3633           icon_info->pressed = TRUE;
3634
3635           if (!icon_info->nonactivatable)
3636             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3637
3638           return TRUE;
3639         }
3640     }
3641
3642   if (event->window != entry->text_area ||
3643       (entry->button && event->button != entry->button))
3644     return FALSE;
3645
3646   gtk_entry_reset_blink_time (entry);
3647
3648   entry->button = event->button;
3649   
3650   if (!gtk_widget_has_focus (widget))
3651     {
3652       entry->in_click = TRUE;
3653       gtk_widget_grab_focus (widget);
3654       entry->in_click = FALSE;
3655     }
3656   
3657   tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
3658     
3659   if (event->button == 1)
3660     {
3661       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3662       
3663       entry->select_words = FALSE;
3664       entry->select_lines = FALSE;
3665
3666       if (event->state & GDK_SHIFT_MASK)
3667         {
3668           _gtk_entry_reset_im_context (entry);
3669           
3670           if (!have_selection) /* select from the current position to the clicked position */
3671             sel_start = sel_end = entry->current_pos;
3672           
3673           if (tmp_pos > sel_start && tmp_pos < sel_end)
3674             {
3675               /* Truncate current selection, but keep it as big as possible */
3676               if (tmp_pos - sel_start > sel_end - tmp_pos)
3677                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3678               else
3679                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3680             }
3681           else
3682             {
3683               gboolean extend_to_left;
3684               gint start, end;
3685
3686               /* Figure out what click selects and extend current selection */
3687               switch (event->type)
3688                 {
3689                 case GDK_BUTTON_PRESS:
3690                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3691                   break;
3692                   
3693                 case GDK_2BUTTON_PRESS:
3694                   entry->select_words = TRUE;
3695                   gtk_entry_select_word (entry);
3696                   break;
3697                   
3698                 case GDK_3BUTTON_PRESS:
3699                   entry->select_lines = TRUE;
3700                   gtk_entry_select_line (entry);
3701                   break;
3702
3703                 default:
3704                   break;
3705                 }
3706
3707               start = MIN (entry->current_pos, entry->selection_bound);
3708               start = MIN (sel_start, start);
3709               
3710               end = MAX (entry->current_pos, entry->selection_bound);
3711               end = MAX (sel_end, end);
3712
3713               if (tmp_pos == sel_start || tmp_pos == sel_end)
3714                 extend_to_left = (tmp_pos == start);
3715               else
3716                 extend_to_left = (end == sel_end);
3717               
3718               if (extend_to_left)
3719                 gtk_entry_set_positions (entry, start, end);
3720               else
3721                 gtk_entry_set_positions (entry, end, start);
3722             }
3723         }
3724       else /* no shift key */
3725         switch (event->type)
3726         {
3727         case GDK_BUTTON_PRESS:
3728           if (in_selection (entry, event->x + entry->scroll_offset))
3729             {
3730               /* Click inside the selection - we'll either start a drag, or
3731                * clear the selection
3732                */
3733               entry->in_drag = TRUE;
3734               entry->drag_start_x = event->x + entry->scroll_offset;
3735               entry->drag_start_y = event->y;
3736             }
3737           else
3738             gtk_editable_set_position (editable, tmp_pos);
3739           break;
3740  
3741         case GDK_2BUTTON_PRESS:
3742           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3743            * receiving a GDK_2BUTTON_PRESS so we need to reset
3744            * entry->in_drag which may have been set above
3745            */
3746           entry->in_drag = FALSE;
3747           entry->select_words = TRUE;
3748           gtk_entry_select_word (entry);
3749           break;
3750         
3751         case GDK_3BUTTON_PRESS:
3752           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3753            * receiving a GDK_3BUTTON_PRESS so we need to reset
3754            * entry->in_drag which may have been set above
3755            */
3756           entry->in_drag = FALSE;
3757           entry->select_lines = TRUE;
3758           gtk_entry_select_line (entry);
3759           break;
3760
3761         default:
3762           break;
3763         }
3764
3765       return TRUE;
3766     }
3767   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3768     {
3769       if (entry->editable)
3770         {
3771           priv->insert_pos = tmp_pos;
3772           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3773           return TRUE;
3774         }
3775       else
3776         {
3777           gtk_widget_error_bell (widget);
3778         }
3779     }
3780   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3781     {
3782       gtk_entry_do_popup (entry, event);
3783       entry->button = 0;        /* Don't wait for release, since the menu will gtk_grab_add */
3784
3785       return TRUE;
3786     }
3787
3788   return FALSE;
3789 }
3790
3791 static gint
3792 gtk_entry_button_release (GtkWidget      *widget,
3793                           GdkEventButton *event)
3794 {
3795   GtkEntry *entry = GTK_ENTRY (widget);
3796   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
3797   EntryIconInfo *icon_info = NULL;
3798   gint i;
3799
3800   for (i = 0; i < MAX_ICONS; i++)
3801     {
3802       icon_info = priv->icons[i];
3803
3804       if (!icon_info || icon_info->insensitive)
3805         continue;
3806
3807       if (event->window == icon_info->window)
3808         {
3809           icon_info->pressed = FALSE;
3810
3811           if (should_prelight (entry, i) &&
3812               event->x >= 0 && event->y >= 0 &&
3813               event->x < gdk_window_get_width (icon_info->window) &&
3814               event->y < gdk_window_get_height (icon_info->window))
3815             {
3816               icon_info->prelight = TRUE;
3817               gtk_widget_queue_draw (widget);
3818             }
3819
3820           if (!icon_info->nonactivatable)
3821             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3822
3823           return TRUE;
3824         }
3825     }
3826
3827   if (event->window != entry->text_area || entry->button != event->button)
3828     return FALSE;
3829
3830   if (entry->in_drag)
3831     {
3832       gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
3833
3834       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3835
3836       entry->in_drag = 0;
3837     }
3838   
3839   entry->button = 0;
3840   
3841   gtk_entry_update_primary_selection (entry);
3842               
3843   return TRUE;
3844 }
3845
3846 static gchar *
3847 _gtk_entry_get_selected_text (GtkEntry *entry)
3848 {
3849   GtkEditable *editable = GTK_EDITABLE (entry);
3850   gint         start_text, end_text;
3851   gchar       *text = NULL;
3852
3853   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3854     text = gtk_editable_get_chars (editable, start_text, end_text);
3855
3856   return text;
3857 }
3858
3859 static gint
3860 gtk_entry_motion_notify (GtkWidget      *widget,
3861                          GdkEventMotion *event)
3862 {
3863   GtkEntry *entry = GTK_ENTRY (widget);
3864   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3865   EntryIconInfo *icon_info = NULL;
3866   GdkDragContext *context;
3867   gint tmp_pos;
3868   gint i;
3869
3870   for (i = 0; i < MAX_ICONS; i++)
3871     {
3872       icon_info = priv->icons[i];
3873
3874       if (!icon_info || icon_info->insensitive)
3875         continue;
3876
3877       if (event->window == icon_info->window)
3878         {
3879           if (icon_info->pressed &&
3880               icon_info->target_list != NULL &&
3881               gtk_drag_check_threshold (widget,
3882                                         priv->start_x,
3883                                         priv->start_y,
3884                                         event->x,
3885                                         event->y))
3886             {
3887               icon_info->in_drag = TRUE;
3888               icon_info->pressed = FALSE;
3889               context = gtk_drag_begin (widget,
3890                                         icon_info->target_list,
3891                                         icon_info->actions,
3892                                         1,
3893                                         (GdkEvent*)event);
3894             }
3895
3896           return TRUE;
3897         }
3898     }
3899
3900   if (entry->mouse_cursor_obscured)
3901     {
3902       GdkCursor *cursor;
3903       
3904       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3905       gdk_window_set_cursor (entry->text_area, cursor);
3906       gdk_cursor_unref (cursor);
3907       entry->mouse_cursor_obscured = FALSE;
3908     }
3909
3910   if (event->window != entry->text_area || entry->button != 1)
3911     return FALSE;
3912
3913   if (entry->select_lines)
3914     return TRUE;
3915
3916   gdk_event_request_motions (event);
3917
3918   if (entry->in_drag)
3919     {
3920       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
3921           gtk_drag_check_threshold (widget,
3922                                     entry->drag_start_x, entry->drag_start_y,
3923                                     event->x + entry->scroll_offset, event->y))
3924         {
3925           GdkDragContext *context;
3926           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
3927           guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
3928           gchar *text = NULL;
3929           cairo_surface_t *surface;
3930
3931           gtk_target_list_add_text_targets (target_list, 0);
3932
3933           text = _gtk_entry_get_selected_text (entry);
3934           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
3935
3936           context = gtk_drag_begin (widget, target_list, actions,
3937                                     entry->button, (GdkEvent *)event);
3938           
3939           if (surface)
3940             gtk_drag_set_icon_surface (context, surface);
3941           else
3942             gtk_drag_set_icon_default (context);
3943           
3944           if (surface)
3945             cairo_surface_destroy (surface);
3946           g_free (text);
3947
3948           entry->in_drag = FALSE;
3949           entry->button = 0;
3950           
3951           gtk_target_list_unref (target_list);
3952         }
3953     }
3954   else
3955     {
3956       if (event->y < 0)
3957         tmp_pos = 0;
3958       else if (event->y >= gdk_window_get_height (entry->text_area))
3959         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
3960       else
3961         tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
3962       
3963       if (entry->select_words) 
3964         {
3965           gint min, max;
3966           gint old_min, old_max;
3967           gint pos, bound;
3968           
3969           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
3970           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
3971           
3972           pos = entry->current_pos;
3973           bound = entry->selection_bound;
3974
3975           old_min = MIN(entry->current_pos, entry->selection_bound);
3976           old_max = MAX(entry->current_pos, entry->selection_bound);
3977           
3978           if (min < old_min)
3979             {
3980               pos = min;
3981               bound = old_max;
3982             }
3983           else if (old_max < max) 
3984             {
3985               pos = max;
3986               bound = old_min;
3987             }
3988           else if (pos == old_min) 
3989             {
3990               if (entry->current_pos != min)
3991                 pos = max;
3992             }
3993           else 
3994             {
3995               if (entry->current_pos != max)
3996                 pos = min;
3997             }
3998         
3999           gtk_entry_set_positions (entry, pos, bound);
4000         }
4001       else
4002         gtk_entry_set_positions (entry, tmp_pos, -1);
4003     }
4004       
4005   return TRUE;
4006 }
4007
4008 static void
4009 set_invisible_cursor (GdkWindow *window)
4010 {
4011   GdkDisplay *display;
4012   GdkCursor *cursor;
4013
4014   display = gdk_window_get_display (window);
4015   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4016
4017   gdk_window_set_cursor (window, cursor);
4018
4019   gdk_cursor_unref (cursor);
4020 }
4021
4022 static void
4023 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4024 {
4025   if (entry->mouse_cursor_obscured)
4026     return;
4027
4028   set_invisible_cursor (entry->text_area);
4029   
4030   entry->mouse_cursor_obscured = TRUE;  
4031 }
4032
4033 static gint
4034 gtk_entry_key_press (GtkWidget   *widget,
4035                      GdkEventKey *event)
4036 {
4037   GtkEntry *entry = GTK_ENTRY (widget);
4038
4039   gtk_entry_reset_blink_time (entry);
4040   gtk_entry_pend_cursor_blink (entry);
4041
4042   if (entry->editable)
4043     {
4044       if (gtk_im_context_filter_keypress (entry->im_context, event))
4045         {
4046           gtk_entry_obscure_mouse_cursor (entry);
4047           entry->need_im_reset = TRUE;
4048           return TRUE;
4049         }
4050     }
4051
4052   if (event->keyval == GDK_KEY_Return || 
4053       event->keyval == GDK_KEY_KP_Enter || 
4054       event->keyval == GDK_KEY_ISO_Enter || 
4055       event->keyval == GDK_KEY_Escape)
4056     {
4057       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4058       
4059       if (completion && completion->priv->completion_timeout)
4060         {
4061           g_source_remove (completion->priv->completion_timeout);
4062           completion->priv->completion_timeout = 0;
4063         }
4064
4065       _gtk_entry_reset_im_context (entry);
4066     }
4067
4068   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4069     /* Activate key bindings
4070      */
4071     return TRUE;
4072
4073   if (!entry->editable && event->length)
4074     gtk_widget_error_bell (widget);
4075
4076   return FALSE;
4077 }
4078
4079 static gint
4080 gtk_entry_key_release (GtkWidget   *widget,
4081                        GdkEventKey *event)
4082 {
4083   GtkEntry *entry = GTK_ENTRY (widget);
4084
4085   if (entry->editable)
4086     {
4087       if (gtk_im_context_filter_keypress (entry->im_context, event))
4088         {
4089           entry->need_im_reset = TRUE;
4090           return TRUE;
4091         }
4092     }
4093
4094   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4095 }
4096
4097 static gint
4098 gtk_entry_focus_in (GtkWidget     *widget,
4099                     GdkEventFocus *event)
4100 {
4101   GtkEntry *entry = GTK_ENTRY (widget);
4102   GdkKeymap *keymap;
4103
4104   gtk_widget_queue_draw (widget);
4105
4106   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4107
4108   if (entry->editable)
4109     {
4110       entry->need_im_reset = TRUE;
4111       gtk_im_context_focus_in (entry->im_context);
4112       keymap_state_changed (keymap, entry);
4113       g_signal_connect (keymap, "state-changed", 
4114                         G_CALLBACK (keymap_state_changed), entry);
4115     }
4116
4117   g_signal_connect (keymap, "direction-changed",
4118                     G_CALLBACK (keymap_direction_changed), entry);
4119
4120   gtk_entry_reset_blink_time (entry);
4121   gtk_entry_check_cursor_blink (entry);
4122
4123   return FALSE;
4124 }
4125
4126 static gint
4127 gtk_entry_focus_out (GtkWidget     *widget,
4128                      GdkEventFocus *event)
4129 {
4130   GtkEntry *entry = GTK_ENTRY (widget);
4131   GtkEntryCompletion *completion;
4132   GdkKeymap *keymap;
4133
4134   gtk_widget_queue_draw (widget);
4135
4136   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4137
4138   if (entry->editable)
4139     {
4140       entry->need_im_reset = TRUE;
4141       gtk_im_context_focus_out (entry->im_context);
4142       remove_capslock_feedback (entry);
4143     }
4144
4145   gtk_entry_check_cursor_blink (entry);
4146   
4147   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4148   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4149
4150   completion = gtk_entry_get_completion (entry);
4151   if (completion)
4152     _gtk_entry_completion_popdown (completion);
4153   
4154   return FALSE;
4155 }
4156
4157 static void
4158 gtk_entry_grab_focus (GtkWidget        *widget)
4159 {
4160   GtkEntry *entry = GTK_ENTRY (widget);
4161   gboolean select_on_focus;
4162   
4163   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4164
4165   if (entry->editable && !entry->in_click)
4166     {
4167       g_object_get (gtk_widget_get_settings (widget),
4168                     "gtk-entry-select-on-focus",
4169                     &select_on_focus,
4170                     NULL);
4171   
4172       if (select_on_focus)
4173         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4174     }
4175 }
4176
4177 static void 
4178 gtk_entry_direction_changed (GtkWidget        *widget,
4179                              GtkTextDirection  previous_dir)
4180 {
4181   GtkEntry *entry = GTK_ENTRY (widget);
4182
4183   gtk_entry_recompute (entry);
4184       
4185   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4186 }
4187
4188 static void
4189 gtk_entry_state_changed (GtkWidget      *widget,
4190                          GtkStateType    previous_state)
4191 {
4192   GtkEntry *entry = GTK_ENTRY (widget);
4193   GdkCursor *cursor;
4194   
4195   if (gtk_widget_get_realized (widget))
4196     {
4197       if (gtk_widget_is_sensitive (widget))
4198         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4199       else 
4200         cursor = NULL;
4201       
4202       gdk_window_set_cursor (entry->text_area, cursor);
4203
4204       if (cursor)
4205         gdk_cursor_unref (cursor);
4206
4207       entry->mouse_cursor_obscured = FALSE;
4208
4209       update_cursors (widget);
4210     }
4211
4212   if (!gtk_widget_is_sensitive (widget))
4213     {
4214       /* Clear any selection */
4215       gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);      
4216     }
4217   
4218   gtk_widget_queue_draw (widget);
4219 }
4220
4221 static void
4222 gtk_entry_screen_changed (GtkWidget *widget,
4223                           GdkScreen *old_screen)
4224 {
4225   gtk_entry_recompute (GTK_ENTRY (widget));
4226 }
4227
4228 /* GtkEditable method implementations
4229  */
4230 static void
4231 gtk_entry_insert_text (GtkEditable *editable,
4232                        const gchar *new_text,
4233                        gint         new_text_length,
4234                        gint        *position)
4235 {
4236   g_object_ref (editable);
4237
4238   /*
4239    * The incoming text may a password or other secret. We make sure
4240    * not to copy it into temporary buffers.
4241    */
4242
4243   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4244
4245   g_object_unref (editable);
4246 }
4247
4248 static void
4249 gtk_entry_delete_text (GtkEditable *editable,
4250                        gint         start_pos,
4251                        gint         end_pos)
4252 {
4253   g_object_ref (editable);
4254
4255   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4256
4257   g_object_unref (editable);
4258 }
4259
4260 static gchar *    
4261 gtk_entry_get_chars      (GtkEditable   *editable,
4262                           gint           start_pos,
4263                           gint           end_pos)
4264 {
4265   GtkEntry *entry = GTK_ENTRY (editable);
4266   const gchar *text;
4267   gint text_length;
4268   gint start_index, end_index;
4269
4270   text = gtk_entry_buffer_get_text (get_buffer (entry));
4271   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4272
4273   if (end_pos < 0)
4274     end_pos = text_length;
4275
4276   start_pos = MIN (text_length, start_pos);
4277   end_pos = MIN (text_length, end_pos);
4278
4279   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4280   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4281
4282   return g_strndup (text + start_index, end_index - start_index);
4283 }
4284
4285 static void
4286 gtk_entry_real_set_position (GtkEditable *editable,
4287                              gint         position)
4288 {
4289   GtkEntry *entry = GTK_ENTRY (editable);
4290
4291   guint length;
4292
4293   length = gtk_entry_buffer_get_length (get_buffer (entry));
4294   if (position < 0 || position > length)
4295     position = length;
4296
4297   if (position != entry->current_pos ||
4298       position != entry->selection_bound)
4299     {
4300       _gtk_entry_reset_im_context (entry);
4301       gtk_entry_set_positions (entry, position, position);
4302     }
4303 }
4304
4305 static gint
4306 gtk_entry_get_position (GtkEditable *editable)
4307 {
4308   return GTK_ENTRY (editable)->current_pos;
4309 }
4310
4311 static void
4312 gtk_entry_set_selection_bounds (GtkEditable *editable,
4313                                 gint         start,
4314                                 gint         end)
4315 {
4316   GtkEntry *entry = GTK_ENTRY (editable);
4317   guint length;
4318
4319   length = gtk_entry_buffer_get_length (get_buffer (entry));
4320   if (start < 0)
4321     start = length;
4322   if (end < 0)
4323     end = length;
4324   
4325   _gtk_entry_reset_im_context (entry);
4326
4327   gtk_entry_set_positions (entry,
4328                            MIN (end, length),
4329                            MIN (start, length));
4330
4331   gtk_entry_update_primary_selection (entry);
4332 }
4333
4334 static gboolean
4335 gtk_entry_get_selection_bounds (GtkEditable *editable,
4336                                 gint        *start,
4337                                 gint        *end)
4338 {
4339   GtkEntry *entry = GTK_ENTRY (editable);
4340
4341   *start = entry->selection_bound;
4342   *end = entry->current_pos;
4343
4344   return (entry->selection_bound != entry->current_pos);
4345 }
4346
4347 static void
4348 icon_theme_changed (GtkEntry *entry)
4349 {
4350   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4351   gint i;
4352
4353   for (i = 0; i < MAX_ICONS; i++)
4354     {
4355       EntryIconInfo *icon_info = priv->icons[i];
4356       if (icon_info != NULL) 
4357         {
4358           if (icon_info->storage_type == GTK_IMAGE_ICON_NAME)
4359             gtk_entry_set_icon_from_icon_name (entry, i, icon_info->icon_name);
4360           else if (icon_info->storage_type == GTK_IMAGE_STOCK)
4361             gtk_entry_set_icon_from_stock (entry, i, icon_info->stock_id);
4362           else if (icon_info->storage_type == GTK_IMAGE_GICON)
4363             gtk_entry_set_icon_from_gicon (entry, i, icon_info->gicon);
4364         }
4365     }
4366
4367   gtk_widget_queue_draw (GTK_WIDGET (entry));
4368 }
4369
4370 static void
4371 icon_margin_changed (GtkEntry *entry)
4372 {
4373   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4374   GtkBorder border;
4375
4376   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4377
4378   priv->icon_margin = border.left;
4379 }
4380
4381 static void 
4382 gtk_entry_style_set (GtkWidget *widget,
4383                      GtkStyle  *previous_style)
4384 {
4385   GtkEntry *entry = GTK_ENTRY (widget);
4386   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4387   gint focus_width;
4388   gboolean interior_focus;
4389
4390   gtk_widget_style_get (widget,
4391                         "focus-line-width", &focus_width,
4392                         "interior-focus", &interior_focus,
4393                         NULL);
4394
4395   priv->focus_width = focus_width;
4396   priv->interior_focus = interior_focus;
4397
4398   if (!priv->invisible_char_set)
4399     entry->invisible_char = find_invisible_char (GTK_WIDGET (entry));
4400
4401   gtk_entry_recompute (entry);
4402
4403   icon_theme_changed (entry);
4404   icon_margin_changed (entry);
4405 }
4406
4407 /* GtkCellEditable method implementations
4408  */
4409 static void
4410 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4411 {
4412   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4413   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4414 }
4415
4416 static gboolean
4417 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4418                                    GdkEventKey *key_event,
4419                                    gpointer     data)
4420 {
4421   if (key_event->keyval == GDK_KEY_Escape)
4422     {
4423       entry->editing_canceled = TRUE;
4424       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4425       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4426
4427       return TRUE;
4428     }
4429
4430   /* override focus */
4431   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4432     {
4433       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4434       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4435
4436       return TRUE;
4437     }
4438
4439   return FALSE;
4440 }
4441
4442 static void
4443 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4444                          GdkEvent        *event)
4445 {
4446   GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
4447
4448   g_signal_connect (cell_editable, "activate",
4449                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4450   g_signal_connect (cell_editable, "key-press-event",
4451                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4452 }
4453
4454 static void
4455 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4456 {
4457   if (password_hint->source_id)
4458     g_source_remove (password_hint->source_id);
4459
4460   g_slice_free (GtkEntryPasswordHint, password_hint);
4461 }
4462
4463
4464 static gboolean
4465 gtk_entry_remove_password_hint (gpointer data)
4466 {
4467   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4468   password_hint->position = -1;
4469
4470   /* Force the string to be redrawn, but now without a visible character */
4471   gtk_entry_recompute (GTK_ENTRY (data));
4472   return FALSE;
4473 }
4474
4475 /* Default signal handlers
4476  */
4477 static void
4478 gtk_entry_real_insert_text (GtkEditable *editable,
4479                             const gchar *new_text,
4480                             gint         new_text_length,
4481                             gint        *position)
4482 {
4483   guint n_inserted;
4484   gint n_chars;
4485
4486   n_chars = g_utf8_strlen (new_text, new_text_length);
4487
4488   /*
4489    * The actual insertion into the buffer. This will end up firing the
4490    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4491    * buffer_notify_text(), buffer_notify_length()
4492    */
4493   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4494
4495   if (n_inserted != n_chars)
4496       gtk_widget_error_bell (GTK_WIDGET (editable));
4497
4498   *position += n_inserted;
4499 }
4500
4501 static void
4502 gtk_entry_real_delete_text (GtkEditable *editable,
4503                             gint         start_pos,
4504                             gint         end_pos)
4505 {
4506   /*
4507    * The actual deletion from the buffer. This will end up firing the
4508    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4509    * buffer_notify_text(), buffer_notify_length()
4510    */
4511
4512   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4513 }
4514
4515 /* GtkEntryBuffer signal handlers
4516  */
4517 static void
4518 buffer_inserted_text (GtkEntryBuffer *buffer,
4519                       guint           position,
4520                       const gchar    *chars,
4521                       guint           n_chars,
4522                       GtkEntry       *entry)
4523 {
4524   guint password_hint_timeout;
4525
4526   if (entry->current_pos > position)
4527     entry->current_pos += n_chars;
4528
4529   if (entry->selection_bound > position)
4530     entry->selection_bound += n_chars;
4531
4532   /* Calculate the password hint if it needs to be displayed. */
4533   if (n_chars == 1 && !entry->visible)
4534     {
4535       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4536                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4537                     NULL);
4538
4539       if (password_hint_timeout > 0)
4540         {
4541           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4542                                                                     quark_password_hint);
4543           if (!password_hint)
4544             {
4545               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4546               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4547                                        (GDestroyNotify)gtk_entry_password_hint_free);
4548             }
4549
4550           password_hint->position = position;
4551           if (password_hint->source_id)
4552             g_source_remove (password_hint->source_id);
4553           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4554                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4555         }
4556     }
4557 }
4558
4559 static void
4560 buffer_deleted_text (GtkEntryBuffer *buffer,
4561                      guint           position,
4562                      guint           n_chars,
4563                      GtkEntry       *entry)
4564 {
4565   guint end_pos = position + n_chars;
4566   gint selection_bound;
4567   guint current_pos;
4568
4569   current_pos = entry->current_pos;
4570   if (current_pos > position)
4571     current_pos -= MIN (current_pos, end_pos) - position;
4572
4573   selection_bound = entry->selection_bound;
4574   if (selection_bound > position)
4575     selection_bound -= MIN (selection_bound, end_pos) - position;
4576
4577   gtk_entry_set_positions (entry, current_pos, selection_bound);
4578
4579   /* We might have deleted the selection */
4580   gtk_entry_update_primary_selection (entry);
4581
4582   /* Disable the password hint if one exists. */
4583   if (!entry->visible)
4584     {
4585       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4586                                                                 quark_password_hint);
4587       if (password_hint)
4588         {
4589           if (password_hint->source_id)
4590             g_source_remove (password_hint->source_id);
4591           password_hint->source_id = 0;
4592           password_hint->position = -1;
4593         }
4594     }
4595 }
4596
4597 static void
4598 buffer_notify_text (GtkEntryBuffer *buffer,
4599                     GParamSpec     *spec,
4600                     GtkEntry       *entry)
4601 {
4602   gtk_entry_recompute (entry);
4603   emit_changed (entry);
4604   g_object_notify (G_OBJECT (entry), "text");
4605 }
4606
4607 static void
4608 buffer_notify_length (GtkEntryBuffer *buffer,
4609                       GParamSpec     *spec,
4610                       GtkEntry       *entry)
4611 {
4612   g_object_notify (G_OBJECT (entry), "text-length");
4613 }
4614
4615 static void
4616 buffer_notify_max_length (GtkEntryBuffer *buffer,
4617                           GParamSpec     *spec,
4618                           GtkEntry       *entry)
4619 {
4620   g_object_notify (G_OBJECT (entry), "max-length");
4621 }
4622
4623 static void
4624 buffer_connect_signals (GtkEntry *entry)
4625 {
4626   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4627   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4628   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4629   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4630   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4631 }
4632
4633 static void
4634 buffer_disconnect_signals (GtkEntry *entry)
4635 {
4636   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4637   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4638   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4639   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4640   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4641 }
4642
4643 /* Compute the X position for an offset that corresponds to the "more important
4644  * cursor position for that offset. We use this when trying to guess to which
4645  * end of the selection we should go to when the user hits the left or
4646  * right arrow key.
4647  */
4648 static gint
4649 get_better_cursor_x (GtkEntry *entry,
4650                      gint      offset)
4651 {
4652   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4653   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4654   gboolean split_cursor;
4655   
4656   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4657   const gchar *text = pango_layout_get_text (layout);
4658   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4659   
4660   PangoRectangle strong_pos, weak_pos;
4661   
4662   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4663                 "gtk-split-cursor", &split_cursor,
4664                 NULL);
4665
4666   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4667
4668   if (split_cursor)
4669     return strong_pos.x / PANGO_SCALE;
4670   else
4671     return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4672 }
4673
4674 static void
4675 gtk_entry_move_cursor (GtkEntry       *entry,
4676                        GtkMovementStep step,
4677                        gint            count,
4678                        gboolean        extend_selection)
4679 {
4680   gint new_pos = entry->current_pos;
4681   GtkEntryPrivate *priv;
4682
4683   _gtk_entry_reset_im_context (entry);
4684
4685   if (entry->current_pos != entry->selection_bound && !extend_selection)
4686     {
4687       /* If we have a current selection and aren't extending it, move to the
4688        * start/or end of the selection as appropriate
4689        */
4690       switch (step)
4691         {
4692         case GTK_MOVEMENT_VISUAL_POSITIONS:
4693           {
4694             gint current_x = get_better_cursor_x (entry, entry->current_pos);
4695             gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
4696
4697             if (count <= 0)
4698               new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
4699             else 
4700               new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
4701             break;
4702           }
4703         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4704         case GTK_MOVEMENT_WORDS:
4705           if (count < 0)
4706             new_pos = MIN (entry->current_pos, entry->selection_bound);
4707           else
4708             new_pos = MAX (entry->current_pos, entry->selection_bound);
4709           break;
4710         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4711         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4712         case GTK_MOVEMENT_BUFFER_ENDS:
4713           priv = GTK_ENTRY_GET_PRIVATE (entry);
4714           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4715           break;
4716         case GTK_MOVEMENT_DISPLAY_LINES:
4717         case GTK_MOVEMENT_PARAGRAPHS:
4718         case GTK_MOVEMENT_PAGES:
4719         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4720           break;
4721         }
4722     }
4723   else
4724     {
4725       switch (step)
4726         {
4727         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4728           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4729           break;
4730         case GTK_MOVEMENT_VISUAL_POSITIONS:
4731           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4732           if (entry->current_pos == new_pos)
4733             {
4734               if (!extend_selection)
4735                 {
4736                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4737                                                  count > 0 ?
4738                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4739                     {
4740                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4741
4742                       if (toplevel)
4743                         gtk_widget_child_focus (toplevel,
4744                                                 count > 0 ?
4745                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4746                     }
4747                 }
4748               else
4749                 {
4750                   gtk_widget_error_bell (GTK_WIDGET (entry));
4751                 }
4752             }
4753           break;
4754         case GTK_MOVEMENT_WORDS:
4755           while (count > 0)
4756             {
4757               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4758               count--;
4759             }
4760           while (count < 0)
4761             {
4762               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4763               count++;
4764             }
4765           if (entry->current_pos == new_pos)
4766             gtk_widget_error_bell (GTK_WIDGET (entry));
4767           break;
4768         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4769         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4770         case GTK_MOVEMENT_BUFFER_ENDS:
4771           priv = GTK_ENTRY_GET_PRIVATE (entry);
4772           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4773           if (entry->current_pos == new_pos)
4774             gtk_widget_error_bell (GTK_WIDGET (entry));
4775           break;
4776         case GTK_MOVEMENT_DISPLAY_LINES:
4777         case GTK_MOVEMENT_PARAGRAPHS:
4778         case GTK_MOVEMENT_PAGES:
4779         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4780           break;
4781         }
4782     }
4783
4784   if (extend_selection)
4785     gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
4786   else
4787     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4788   
4789   gtk_entry_pend_cursor_blink (entry);
4790 }
4791
4792 static void
4793 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4794                             const gchar *str)
4795 {
4796   GtkEditable *editable = GTK_EDITABLE (entry);
4797   gint pos = entry->current_pos;
4798
4799   if (entry->editable)
4800     {
4801       _gtk_entry_reset_im_context (entry);
4802
4803       gtk_editable_insert_text (editable, str, -1, &pos);
4804       gtk_editable_set_position (editable, pos);
4805     }
4806 }
4807
4808 static void
4809 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4810                               GtkDeleteType   type,
4811                               gint            count)
4812 {
4813   GtkEditable *editable = GTK_EDITABLE (entry);
4814   gint start_pos = entry->current_pos;
4815   gint end_pos = entry->current_pos;
4816   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
4817   
4818   _gtk_entry_reset_im_context (entry);
4819
4820   if (!entry->editable)
4821     {
4822       gtk_widget_error_bell (GTK_WIDGET (entry));
4823       return;
4824     }
4825
4826   if (entry->selection_bound != entry->current_pos)
4827     {
4828       gtk_editable_delete_selection (editable);
4829       return;
4830     }
4831   
4832   switch (type)
4833     {
4834     case GTK_DELETE_CHARS:
4835       end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
4836       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
4837       break;
4838     case GTK_DELETE_WORDS:
4839       if (count < 0)
4840         {
4841           /* Move to end of current word, or if not on a word, end of previous word */
4842           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
4843           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4844         }
4845       else if (count > 0)
4846         {
4847           /* Move to beginning of current word, or if not on a word, begining of next word */
4848           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
4849           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4850         }
4851         
4852       /* Fall through */
4853     case GTK_DELETE_WORD_ENDS:
4854       while (count < 0)
4855         {
4856           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4857           count++;
4858         }
4859       while (count > 0)
4860         {
4861           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4862           count--;
4863         }
4864       gtk_editable_delete_text (editable, start_pos, end_pos);
4865       break;
4866     case GTK_DELETE_DISPLAY_LINE_ENDS:
4867     case GTK_DELETE_PARAGRAPH_ENDS:
4868       if (count < 0)
4869         gtk_editable_delete_text (editable, 0, entry->current_pos);
4870       else
4871         gtk_editable_delete_text (editable, entry->current_pos, -1);
4872       break;
4873     case GTK_DELETE_DISPLAY_LINES:
4874     case GTK_DELETE_PARAGRAPHS:
4875       gtk_editable_delete_text (editable, 0, -1);  
4876       break;
4877     case GTK_DELETE_WHITESPACE:
4878       gtk_entry_delete_whitespace (entry);
4879       break;
4880     }
4881
4882   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
4883     gtk_widget_error_bell (GTK_WIDGET (entry));
4884
4885   gtk_entry_pend_cursor_blink (entry);
4886 }
4887
4888 static void
4889 gtk_entry_backspace (GtkEntry *entry)
4890 {
4891   GtkEditable *editable = GTK_EDITABLE (entry);
4892   gint prev_pos;
4893
4894   _gtk_entry_reset_im_context (entry);
4895
4896   if (!entry->editable)
4897     {
4898       gtk_widget_error_bell (GTK_WIDGET (entry));
4899       return;
4900     }
4901
4902   if (entry->selection_bound != entry->current_pos)
4903     {
4904       gtk_editable_delete_selection (editable);
4905       return;
4906     }
4907
4908   prev_pos = gtk_entry_move_logically (entry, entry->current_pos, -1);
4909
4910   if (prev_pos < entry->current_pos)
4911     {
4912       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4913       PangoLogAttr *log_attrs;
4914       gint n_attrs;
4915
4916       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4917
4918       /* Deleting parts of characters */
4919       if (log_attrs[entry->current_pos].backspace_deletes_character)
4920         {
4921           gchar *cluster_text;
4922           gchar *normalized_text;
4923           glong  len;
4924
4925           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
4926                                                      entry->current_pos);
4927           normalized_text = g_utf8_normalize (cluster_text,
4928                                               strlen (cluster_text),
4929                                               G_NORMALIZE_NFD);
4930           len = g_utf8_strlen (normalized_text, -1);
4931
4932           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
4933           if (len > 1)
4934             {
4935               gint pos = entry->current_pos;
4936
4937               gtk_editable_insert_text (editable, normalized_text,
4938                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
4939                                         &pos);
4940               gtk_editable_set_position (editable, pos);
4941             }
4942
4943           g_free (normalized_text);
4944           g_free (cluster_text);
4945         }
4946       else
4947         {
4948           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
4949         }
4950       
4951       g_free (log_attrs);
4952     }
4953   else
4954     {
4955       gtk_widget_error_bell (GTK_WIDGET (entry));
4956     }
4957
4958   gtk_entry_pend_cursor_blink (entry);
4959 }
4960
4961 static void
4962 gtk_entry_copy_clipboard (GtkEntry *entry)
4963 {
4964   GtkEditable *editable = GTK_EDITABLE (entry);
4965   gint start, end;
4966   gchar *str;
4967
4968   if (gtk_editable_get_selection_bounds (editable, &start, &end))
4969     {
4970       if (!entry->visible)
4971         {
4972           gtk_widget_error_bell (GTK_WIDGET (entry));
4973           return;
4974         }
4975
4976       str = gtk_entry_get_display_text (entry, start, end);
4977       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
4978                                                         GDK_SELECTION_CLIPBOARD),
4979                               str, -1);
4980       g_free (str);
4981     }
4982 }
4983
4984 static void
4985 gtk_entry_cut_clipboard (GtkEntry *entry)
4986 {
4987   GtkEditable *editable = GTK_EDITABLE (entry);
4988   gint start, end;
4989
4990   if (!entry->visible)
4991     {
4992       gtk_widget_error_bell (GTK_WIDGET (entry));
4993       return;
4994     }
4995
4996   gtk_entry_copy_clipboard (entry);
4997
4998   if (entry->editable)
4999     {
5000       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5001         gtk_editable_delete_text (editable, start, end);
5002     }
5003   else
5004     {
5005       gtk_widget_error_bell (GTK_WIDGET (entry));
5006     }
5007 }
5008
5009 static void
5010 gtk_entry_paste_clipboard (GtkEntry *entry)
5011 {
5012   if (entry->editable)
5013     gtk_entry_paste (entry, GDK_NONE);
5014   else
5015     gtk_widget_error_bell (GTK_WIDGET (entry));
5016 }
5017
5018 static void
5019 gtk_entry_delete_cb (GtkEntry *entry)
5020 {
5021   GtkEditable *editable = GTK_EDITABLE (entry);
5022   gint start, end;
5023
5024   if (entry->editable)
5025     {
5026       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5027         gtk_editable_delete_text (editable, start, end);
5028     }
5029 }
5030
5031 static void
5032 gtk_entry_toggle_overwrite (GtkEntry *entry)
5033 {
5034   entry->overwrite_mode = !entry->overwrite_mode;
5035   gtk_entry_pend_cursor_blink (entry);
5036   gtk_widget_queue_draw (GTK_WIDGET (entry));
5037 }
5038
5039 static void
5040 gtk_entry_select_all (GtkEntry *entry)
5041 {
5042   gtk_entry_select_line (entry);
5043 }
5044
5045 static void
5046 gtk_entry_real_activate (GtkEntry *entry)
5047 {
5048   GtkWindow *window;
5049   GtkWidget *default_widget, *focus_widget;
5050   GtkWidget *toplevel;
5051   GtkWidget *widget;
5052
5053   widget = GTK_WIDGET (entry);
5054
5055   if (entry->activates_default)
5056     {
5057       toplevel = gtk_widget_get_toplevel (widget);
5058       if (GTK_IS_WINDOW (toplevel))
5059         {
5060           window = GTK_WINDOW (toplevel);
5061
5062           if (window)
5063             {
5064               default_widget = gtk_window_get_default_widget (window);
5065               focus_widget = gtk_window_get_focus (window);
5066               if (widget != default_widget &&
5067                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5068                 gtk_window_activate_default (window);
5069             }
5070         }
5071     }
5072 }
5073
5074 static void
5075 keymap_direction_changed (GdkKeymap *keymap,
5076                           GtkEntry  *entry)
5077 {
5078   gtk_entry_recompute (entry);
5079 }
5080
5081 /* IM Context Callbacks
5082  */
5083
5084 static void
5085 gtk_entry_commit_cb (GtkIMContext *context,
5086                      const gchar  *str,
5087                      GtkEntry     *entry)
5088 {
5089   if (entry->editable)
5090     gtk_entry_enter_text (entry, str);
5091 }
5092
5093 static void 
5094 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5095                               GtkEntry     *entry)
5096 {
5097   if (entry->editable)
5098     {
5099       gchar *preedit_string;
5100       gint cursor_pos;
5101       
5102       gtk_im_context_get_preedit_string (entry->im_context,
5103                                          &preedit_string, NULL,
5104                                          &cursor_pos);
5105       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5106       entry->preedit_length = strlen (preedit_string);
5107       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5108       entry->preedit_cursor = cursor_pos;
5109       g_free (preedit_string);
5110     
5111       gtk_entry_recompute (entry);
5112     }
5113 }
5114
5115 static gboolean
5116 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5117                                GtkEntry         *entry)
5118 {
5119   gchar *text;
5120
5121   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5122   text = gtk_entry_get_display_text (entry, 0, -1);
5123   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5124                                   g_utf8_offset_to_pointer (text, entry->current_pos) - text);
5125   g_free (text);
5126
5127   return TRUE;
5128 }
5129
5130 static gboolean
5131 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5132                                  gint          offset,
5133                                  gint          n_chars,
5134                                  GtkEntry     *entry)
5135 {
5136   if (entry->editable)
5137     gtk_editable_delete_text (GTK_EDITABLE (entry),
5138                               entry->current_pos + offset,
5139                               entry->current_pos + offset + n_chars);
5140
5141   return TRUE;
5142 }
5143
5144 /* Internal functions
5145  */
5146
5147 /* Used for im_commit_cb and inserting Unicode chars */
5148 static void
5149 gtk_entry_enter_text (GtkEntry       *entry,
5150                       const gchar    *str)
5151 {
5152   GtkEditable *editable = GTK_EDITABLE (entry);
5153   gint tmp_pos;
5154   gboolean old_need_im_reset;
5155
5156   old_need_im_reset = entry->need_im_reset;
5157   entry->need_im_reset = FALSE;
5158
5159   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5160     gtk_editable_delete_selection (editable);
5161   else
5162     {
5163       if (entry->overwrite_mode)
5164         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5165     }
5166
5167   tmp_pos = entry->current_pos;
5168   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5169   gtk_editable_set_position (editable, tmp_pos);
5170
5171   entry->need_im_reset = old_need_im_reset;
5172 }
5173
5174 /* All changes to entry->current_pos and entry->selection_bound
5175  * should go through this function.
5176  */
5177 static void
5178 gtk_entry_set_positions (GtkEntry *entry,
5179                          gint      current_pos,
5180                          gint      selection_bound)
5181 {
5182   gboolean changed = FALSE;
5183
5184   g_object_freeze_notify (G_OBJECT (entry));
5185   
5186   if (current_pos != -1 &&
5187       entry->current_pos != current_pos)
5188     {
5189       entry->current_pos = current_pos;
5190       changed = TRUE;
5191
5192       g_object_notify (G_OBJECT (entry), "cursor-position");
5193     }
5194
5195   if (selection_bound != -1 &&
5196       entry->selection_bound != selection_bound)
5197     {
5198       entry->selection_bound = selection_bound;
5199       changed = TRUE;
5200       
5201       g_object_notify (G_OBJECT (entry), "selection-bound");
5202     }
5203
5204   g_object_thaw_notify (G_OBJECT (entry));
5205
5206   if (changed) 
5207     {
5208       gtk_entry_move_adjustments (entry);
5209       gtk_entry_recompute (entry);
5210     }
5211 }
5212
5213 static void
5214 gtk_entry_reset_layout (GtkEntry *entry)
5215 {
5216   if (entry->cached_layout)
5217     {
5218       g_object_unref (entry->cached_layout);
5219       entry->cached_layout = NULL;
5220     }
5221 }
5222
5223 static void
5224 update_im_cursor_location (GtkEntry *entry)
5225 {
5226   GdkRectangle area;
5227   gint strong_x;
5228   gint strong_xoffset;
5229   gint area_width, area_height;
5230
5231   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5232   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5233
5234   strong_xoffset = strong_x - entry->scroll_offset;
5235   if (strong_xoffset < 0)
5236     {
5237       strong_xoffset = 0;
5238     }
5239   else if (strong_xoffset > area_width)
5240     {
5241       strong_xoffset = area_width;
5242     }
5243   area.x = strong_xoffset;
5244   area.y = 0;
5245   area.width = 0;
5246   area.height = area_height;
5247
5248   gtk_im_context_set_cursor_location (entry->im_context, &area);
5249 }
5250
5251 static gboolean
5252 recompute_idle_func (gpointer data)
5253 {
5254   GtkEntry *entry;
5255
5256   entry = GTK_ENTRY (data);
5257
5258   entry->recompute_idle = 0;
5259   
5260   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5261     {
5262       gtk_entry_adjust_scroll (entry);
5263       gtk_entry_queue_draw (entry);
5264       
5265       update_im_cursor_location (entry);
5266     }
5267
5268   return FALSE;
5269 }
5270
5271 static void
5272 gtk_entry_recompute (GtkEntry *entry)
5273 {
5274   gtk_entry_reset_layout (entry);
5275   gtk_entry_check_cursor_blink (entry);
5276   
5277   if (!entry->recompute_idle)
5278     {
5279       entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5280                                                recompute_idle_func, entry, NULL); 
5281     }
5282 }
5283
5284 static PangoLayout *
5285 gtk_entry_create_layout (GtkEntry *entry,
5286                          gboolean  include_preedit)
5287 {
5288   GtkWidget *widget = GTK_WIDGET (entry);
5289   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5290   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5291
5292   gchar *preedit_string = NULL;
5293   gint preedit_length = 0;
5294   PangoAttrList *preedit_attrs = NULL;
5295
5296   gchar *display;
5297   guint n_bytes;
5298
5299   pango_layout_set_single_paragraph_mode (layout, TRUE);
5300
5301   display = gtk_entry_get_display_text (entry, 0, -1);
5302   n_bytes = strlen (display);
5303
5304   if (include_preedit)
5305     {
5306       gtk_im_context_get_preedit_string (entry->im_context,
5307                                          &preedit_string, &preedit_attrs, NULL);
5308       preedit_length = entry->preedit_length;
5309     }
5310
5311   if (preedit_length)
5312     {
5313       GString *tmp_string = g_string_new (display);
5314       gint cursor_index = g_utf8_offset_to_pointer (display, entry->current_pos) - display;
5315       
5316       g_string_insert (tmp_string, cursor_index, preedit_string);
5317       
5318       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5319       
5320       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5321                               cursor_index, preedit_length);
5322       
5323       g_string_free (tmp_string, TRUE);
5324     }
5325   else
5326     {
5327       PangoDirection pango_dir;
5328       
5329       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5330         pango_dir = pango_find_base_dir (display, n_bytes);
5331       else
5332         pango_dir = PANGO_DIRECTION_NEUTRAL;
5333
5334       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5335         {
5336           if (gtk_widget_has_focus (widget))
5337             {
5338               GdkDisplay *display = gtk_widget_get_display (widget);
5339               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5340               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5341                 pango_dir = PANGO_DIRECTION_RTL;
5342               else
5343                 pango_dir = PANGO_DIRECTION_LTR;
5344             }
5345           else
5346             {
5347               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5348                 pango_dir = PANGO_DIRECTION_RTL;
5349               else
5350                 pango_dir = PANGO_DIRECTION_LTR;
5351             }
5352         }
5353
5354       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5355                                   pango_dir);
5356
5357       entry->resolved_dir = pango_dir;
5358
5359       pango_layout_set_text (layout, display, n_bytes);
5360     }
5361       
5362   pango_layout_set_attributes (layout, tmp_attrs);
5363
5364   g_free (preedit_string);
5365   g_free (display);
5366
5367   if (preedit_attrs)
5368     pango_attr_list_unref (preedit_attrs);
5369       
5370   pango_attr_list_unref (tmp_attrs);
5371
5372   return layout;
5373 }
5374
5375 static PangoLayout *
5376 gtk_entry_ensure_layout (GtkEntry *entry,
5377                          gboolean  include_preedit)
5378 {
5379   if (entry->preedit_length > 0 &&
5380       !include_preedit != !entry->cache_includes_preedit)
5381     gtk_entry_reset_layout (entry);
5382
5383   if (!entry->cached_layout)
5384     {
5385       entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5386       entry->cache_includes_preedit = include_preedit;
5387     }
5388   
5389   return entry->cached_layout;
5390 }
5391
5392 static void
5393 get_layout_position (GtkEntry *entry,
5394                      gint     *x,
5395                      gint     *y)
5396 {
5397   PangoLayout *layout;
5398   PangoRectangle logical_rect;
5399   gint area_width, area_height;
5400   GtkBorder inner_border;
5401   gint y_pos;
5402   PangoLayoutLine *line;
5403   
5404   layout = gtk_entry_ensure_layout (entry, TRUE);
5405
5406   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5407   _gtk_entry_effective_inner_border (entry, &inner_border);
5408
5409   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5410
5411   line = pango_layout_get_lines_readonly (layout)->data;
5412   pango_layout_line_get_extents (line, NULL, &logical_rect);
5413   
5414   /* Align primarily for locale's ascent/descent */
5415   y_pos = ((area_height - entry->ascent - entry->descent) / 2 + 
5416            entry->ascent + logical_rect.y);
5417   
5418   /* Now see if we need to adjust to fit in actual drawn string */
5419   if (logical_rect.height > area_height)
5420     y_pos = (area_height - logical_rect.height) / 2;
5421   else if (y_pos < 0)
5422     y_pos = 0;
5423   else if (y_pos + logical_rect.height > area_height)
5424     y_pos = area_height - logical_rect.height;
5425   
5426   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5427
5428   if (x)
5429     *x = inner_border.left - entry->scroll_offset;
5430
5431   if (y)
5432     *y = y_pos;
5433 }
5434
5435 static void
5436 draw_text_with_color (GtkEntry *entry, cairo_t *cr, GdkColor *default_color)
5437 {
5438   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5439   GtkWidget *widget;
5440   gint x, y;
5441   gint start_pos, end_pos;
5442
5443   widget = GTK_WIDGET (entry);
5444
5445   cairo_save (cr);
5446
5447   get_layout_position (entry, &x, &y);
5448
5449   cairo_move_to (cr, x, y);
5450   gdk_cairo_set_source_color (cr, default_color);
5451   pango_cairo_show_layout (cr, layout);
5452
5453   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5454     {
5455       gint *ranges;
5456       gint n_ranges, i;
5457       PangoRectangle logical_rect;
5458       GdkColor *selection_color, *text_color;
5459       GtkBorder inner_border;
5460       GtkStyle *style;
5461
5462       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5463       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5464
5465       style = gtk_widget_get_style (widget);
5466
5467       if (gtk_widget_has_focus (widget))
5468         {
5469           selection_color = &style->base [GTK_STATE_SELECTED];
5470           text_color = &style->text [GTK_STATE_SELECTED];
5471         }
5472       else
5473         {
5474           selection_color = &style->base [GTK_STATE_ACTIVE];
5475           text_color = &style->text [GTK_STATE_ACTIVE];
5476         }
5477
5478       _gtk_entry_effective_inner_border (entry, &inner_border);
5479
5480       for (i = 0; i < n_ranges; ++i)
5481         cairo_rectangle (cr,
5482                          inner_border.left - entry->scroll_offset + ranges[2 * i],
5483                          y,
5484                          ranges[2 * i + 1],
5485                          logical_rect.height);
5486
5487       cairo_clip (cr);
5488           
5489       gdk_cairo_set_source_color (cr, selection_color);
5490       cairo_paint (cr);
5491
5492       cairo_move_to (cr, x, y);
5493       gdk_cairo_set_source_color (cr, text_color);
5494       pango_cairo_show_layout (cr, layout);
5495   
5496       g_free (ranges);
5497     }
5498   cairo_restore (cr);
5499 }
5500
5501 static void
5502 gtk_entry_draw_text (GtkEntry *entry,
5503                      cairo_t  *cr)
5504 {
5505   GtkWidget *widget = GTK_WIDGET (entry);
5506   GtkStateType state;
5507   GtkStyle *style;
5508   GdkColor text_color, bar_text_color;
5509   gint pos_x, pos_y;
5510   gint width, height;
5511   gint progress_x, progress_y, progress_width, progress_height;
5512   gint clip_width, clip_height;
5513
5514   /* Nothing to display at all */
5515   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5516     return;
5517   
5518   state = GTK_STATE_SELECTED;
5519   if (!gtk_widget_get_sensitive (widget))
5520     state = GTK_STATE_INSENSITIVE;
5521   style = gtk_widget_get_style (widget);
5522   text_color = style->text[gtk_widget_get_state (widget)];
5523   bar_text_color = style->fg[state];
5524
5525   get_progress_area (widget,
5526                      &progress_x, &progress_y,
5527                      &progress_width, &progress_height);
5528
5529   clip_width = gdk_window_get_width (entry->text_area);
5530   clip_height = gdk_window_get_height (entry->text_area);
5531   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5532   cairo_clip (cr);
5533
5534   /* If the color is the same, or the progress area has a zero
5535    * size, then we only need to draw once. */
5536   if ((text_color.pixel == bar_text_color.pixel) ||
5537       ((progress_width == 0) || (progress_height == 0)))
5538     {
5539       draw_text_with_color (entry, cr, &text_color);
5540     }
5541   else
5542     {
5543       width = gdk_window_get_width (entry->text_area);
5544       height = gdk_window_get_height (entry->text_area);
5545
5546       cairo_save (cr);
5547
5548       cairo_rectangle (cr, 0, 0, width, height);
5549       cairo_clip (cr);
5550       cairo_save (cr);
5551
5552       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5553       cairo_rectangle (cr, 0, 0, width, height);
5554
5555       gdk_window_get_position (entry->text_area, &pos_x, &pos_y);
5556       progress_x -= pos_x;
5557       progress_y -= pos_y;
5558
5559       cairo_rectangle (cr, progress_x, progress_y,
5560                        progress_width, progress_height);
5561       cairo_clip (cr);
5562       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5563   
5564       draw_text_with_color (entry, cr, &text_color);
5565       cairo_restore (cr);
5566
5567       cairo_rectangle (cr, progress_x, progress_y,
5568                        progress_width, progress_height);
5569       cairo_clip (cr);
5570
5571       draw_text_with_color (entry, cr, &bar_text_color);
5572
5573       cairo_restore (cr);
5574     }
5575 }
5576
5577 static void
5578 draw_insertion_cursor (GtkEntry      *entry,
5579                        cairo_t       *cr,
5580                        GdkRectangle  *cursor_location,
5581                        gboolean       is_primary,
5582                        PangoDirection direction,
5583                        gboolean       draw_arrow)
5584 {
5585   GtkWidget *widget = GTK_WIDGET (entry);
5586   GtkTextDirection text_dir;
5587
5588   if (direction == PANGO_DIRECTION_LTR)
5589     text_dir = GTK_TEXT_DIR_LTR;
5590   else
5591     text_dir = GTK_TEXT_DIR_RTL;
5592
5593   gtk_draw_insertion_cursor (widget, cr,
5594                              cursor_location,
5595                              is_primary, text_dir, draw_arrow);
5596 }
5597
5598 static void
5599 gtk_entry_draw_cursor (GtkEntry  *entry,
5600                        cairo_t   *cr,
5601                        CursorType type)
5602 {
5603   GtkWidget *widget = GTK_WIDGET (entry);
5604   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5605   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5606   GdkRectangle cursor_location;
5607   gboolean split_cursor;
5608   PangoRectangle cursor_rect;
5609   GtkBorder inner_border;
5610   gint xoffset;
5611   gint text_area_height;
5612   gint cursor_index;
5613   gboolean block;
5614   gboolean block_at_line_end;
5615   PangoLayout *layout;
5616   const char *text;
5617
5618   _gtk_entry_effective_inner_border (entry, &inner_border);
5619
5620   xoffset = inner_border.left - entry->scroll_offset;
5621
5622   text_area_height = gdk_window_get_height (entry->text_area);
5623
5624   layout = gtk_entry_ensure_layout (entry, TRUE);
5625   text = pango_layout_get_text (layout);
5626   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5627   if (!entry->overwrite_mode)
5628     block = FALSE;
5629   else
5630     block = _gtk_text_util_get_block_cursor_location (layout,
5631                                                       cursor_index, &cursor_rect, &block_at_line_end);
5632
5633   if (!block)
5634     {
5635       gint strong_x, weak_x;
5636       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
5637       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
5638       gint x1 = 0;
5639       gint x2 = 0;
5640
5641       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
5642
5643       g_object_get (gtk_widget_get_settings (widget),
5644                     "gtk-split-cursor", &split_cursor,
5645                     NULL);
5646
5647       dir1 = entry->resolved_dir;
5648   
5649       if (split_cursor)
5650         {
5651           x1 = strong_x;
5652
5653           if (weak_x != strong_x)
5654             {
5655               dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
5656               x2 = weak_x;
5657             }
5658         }
5659       else
5660         {
5661           if (keymap_direction == entry->resolved_dir)
5662             x1 = strong_x;
5663           else
5664             x1 = weak_x;
5665         }
5666
5667       cursor_location.x = xoffset + x1;
5668       cursor_location.y = inner_border.top;
5669       cursor_location.width = 0;
5670       cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
5671
5672       draw_insertion_cursor (entry, cr,
5673                              &cursor_location, TRUE, dir1,
5674                              dir2 != PANGO_DIRECTION_NEUTRAL);
5675   
5676       if (dir2 != PANGO_DIRECTION_NEUTRAL)
5677         {
5678           cursor_location.x = xoffset + x2;
5679           draw_insertion_cursor (entry, cr,
5680                                  &cursor_location, FALSE, dir2,
5681                                  TRUE);
5682         }
5683     }
5684   else /* overwrite_mode */
5685     {
5686       GdkColor cursor_color;
5687       GdkRectangle rect;
5688       gint x, y;
5689
5690       cairo_save (cr);
5691
5692       get_layout_position (entry, &x, &y);
5693
5694       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5695       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5696       rect.width = PANGO_PIXELS (cursor_rect.width);
5697       rect.height = PANGO_PIXELS (cursor_rect.height);
5698
5699       _gtk_widget_get_cursor_color (widget, &cursor_color);
5700       gdk_cairo_set_source_color (cr, &cursor_color);
5701       gdk_cairo_rectangle (cr, &rect);
5702       cairo_fill (cr);
5703
5704       if (!block_at_line_end)
5705         {
5706           gdk_cairo_rectangle (cr, &rect);
5707           cairo_clip (cr);
5708           cairo_move_to (cr, x, y);
5709           gdk_cairo_set_source_color (cr, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
5710           pango_cairo_show_layout (cr, layout);
5711         }
5712
5713       cairo_restore (cr);
5714     }
5715 }
5716
5717 static void
5718 gtk_entry_queue_draw (GtkEntry *entry)
5719 {
5720   if (gtk_widget_is_drawable (GTK_WIDGET (entry)))
5721     gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (entry)),
5722                                 NULL, FALSE);
5723 }
5724
5725 void
5726 _gtk_entry_reset_im_context (GtkEntry *entry)
5727 {
5728   if (entry->need_im_reset)
5729     {
5730       entry->need_im_reset = FALSE;
5731       gtk_im_context_reset (entry->im_context);
5732     }
5733 }
5734
5735 /**
5736  * gtk_entry_reset_im_context:
5737  * @entry: a #GtkEntry
5738  *
5739  * Reset the input method context of the entry if needed.
5740  *
5741  * This can be necessary in the case where modifying the buffer
5742  * would confuse on-going input method behavior.
5743  *
5744  * Since: 2.22
5745  */
5746 void
5747 gtk_entry_reset_im_context (GtkEntry *entry)
5748 {
5749   g_return_if_fail (GTK_IS_ENTRY (entry));
5750
5751   _gtk_entry_reset_im_context (entry);
5752 }
5753
5754 /**
5755  * gtk_entry_im_context_filter_keypress:
5756  * @entry: a #GtkEntry
5757  * @event: the key event
5758  *
5759  * Allow the #GtkEntry input method to internally handle key press
5760  * and release events. If this function returns %TRUE, then no further
5761  * processing should be done for this key event. See
5762  * gtk_im_context_filter_keypress().
5763  *
5764  * Note that you are expected to call this function from your handler
5765  * when overriding key event handling. This is needed in the case when
5766  * you need to insert your own key handling between the input method
5767  * and the default key event handling of the #GtkEntry.
5768  * See gtk_text_view_reset_im_context() for an example of use.
5769  *
5770  * Return value: %TRUE if the input method handled the key event.
5771  *
5772  * Since: 2.22
5773  */
5774 gboolean
5775 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
5776                                       GdkEventKey *event)
5777 {
5778   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
5779
5780   return gtk_im_context_filter_keypress (entry->im_context, event);
5781 }
5782
5783
5784 static gint
5785 gtk_entry_find_position (GtkEntry *entry,
5786                          gint      x)
5787 {
5788   PangoLayout *layout;
5789   PangoLayoutLine *line;
5790   gint index;
5791   gint pos;
5792   gint trailing;
5793   const gchar *text;
5794   gint cursor_index;
5795   
5796   layout = gtk_entry_ensure_layout (entry, TRUE);
5797   text = pango_layout_get_text (layout);
5798   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
5799   
5800   line = pango_layout_get_lines_readonly (layout)->data;
5801   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
5802
5803   if (index >= cursor_index && entry->preedit_length)
5804     {
5805       if (index >= cursor_index + entry->preedit_length)
5806         index -= entry->preedit_length;
5807       else
5808         {
5809           index = cursor_index;
5810           trailing = 0;
5811         }
5812     }
5813
5814   pos = g_utf8_pointer_to_offset (text, text + index);
5815   pos += trailing;
5816
5817   return pos;
5818 }
5819
5820 static void
5821 gtk_entry_get_cursor_locations (GtkEntry   *entry,
5822                                 CursorType  type,
5823                                 gint       *strong_x,
5824                                 gint       *weak_x)
5825 {
5826   DisplayMode mode = gtk_entry_get_display_mode (entry);
5827
5828   /* Nothing to display at all, so no cursor is relevant */
5829   if (mode == DISPLAY_BLANK)
5830     {
5831       if (strong_x)
5832         *strong_x = 0;
5833       
5834       if (weak_x)
5835         *weak_x = 0;
5836     }
5837   else
5838     {
5839       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5840       const gchar *text = pango_layout_get_text (layout);
5841       PangoRectangle strong_pos, weak_pos;
5842       gint index;
5843   
5844       if (type == CURSOR_STANDARD)
5845         {
5846           index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5847         }
5848       else /* type == CURSOR_DND */
5849         {
5850           index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
5851
5852           if (entry->dnd_position > entry->current_pos)
5853             {
5854               if (mode == DISPLAY_NORMAL)
5855                 index += entry->preedit_length;
5856               else
5857                 {
5858                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
5859                   index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
5860                 }
5861             }
5862         }
5863       
5864       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
5865       
5866       if (strong_x)
5867         *strong_x = strong_pos.x / PANGO_SCALE;
5868       
5869       if (weak_x)
5870         *weak_x = weak_pos.x / PANGO_SCALE;
5871     }
5872 }
5873
5874 static void
5875 gtk_entry_adjust_scroll (GtkEntry *entry)
5876 {
5877   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5878   gint min_offset, max_offset;
5879   gint text_area_width, text_width;
5880   GtkBorder inner_border;
5881   gint strong_x, weak_x;
5882   gint strong_xoffset, weak_xoffset;
5883   gfloat xalign;
5884   PangoLayout *layout;
5885   PangoLayoutLine *line;
5886   PangoRectangle logical_rect;
5887
5888   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
5889     return;
5890
5891   _gtk_entry_effective_inner_border (entry, &inner_border);
5892
5893   text_area_width = gdk_window_get_width (entry->text_area);
5894   text_area_width -= inner_border.left + inner_border.right;
5895   if (text_area_width < 0)
5896     text_area_width = 0;
5897
5898   layout = gtk_entry_ensure_layout (entry, TRUE);
5899   line = pango_layout_get_lines_readonly (layout)->data;
5900
5901   pango_layout_line_get_extents (line, NULL, &logical_rect);
5902
5903   /* Display as much text as we can */
5904
5905   if (entry->resolved_dir == PANGO_DIRECTION_LTR)
5906       xalign = priv->xalign;
5907   else
5908       xalign = 1.0 - priv->xalign;
5909
5910   text_width = PANGO_PIXELS(logical_rect.width);
5911
5912   if (text_width > text_area_width)
5913     {
5914       min_offset = 0;
5915       max_offset = text_width - text_area_width;
5916     }
5917   else
5918     {
5919       min_offset = (text_width - text_area_width) * xalign;
5920       max_offset = min_offset;
5921     }
5922
5923   entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
5924
5925   /* And make sure cursors are on screen. Note that the cursor is
5926    * actually drawn one pixel into the INNER_BORDER space on
5927    * the right, when the scroll is at the utmost right. This
5928    * looks better to to me than confining the cursor inside the
5929    * border entirely, though it means that the cursor gets one
5930    * pixel closer to the edge of the widget on the right than
5931    * on the left. This might need changing if one changed
5932    * INNER_BORDER from 2 to 1, as one would do on a
5933    * small-screen-real-estate display.
5934    *
5935    * We always make sure that the strong cursor is on screen, and
5936    * put the weak cursor on screen if possible.
5937    */
5938
5939   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
5940   
5941   strong_xoffset = strong_x - entry->scroll_offset;
5942
5943   if (strong_xoffset < 0)
5944     {
5945       entry->scroll_offset += strong_xoffset;
5946       strong_xoffset = 0;
5947     }
5948   else if (strong_xoffset > text_area_width)
5949     {
5950       entry->scroll_offset += strong_xoffset - text_area_width;
5951       strong_xoffset = text_area_width;
5952     }
5953
5954   weak_xoffset = weak_x - entry->scroll_offset;
5955
5956   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
5957     {
5958       entry->scroll_offset += weak_xoffset;
5959     }
5960   else if (weak_xoffset > text_area_width &&
5961            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
5962     {
5963       entry->scroll_offset += weak_xoffset - text_area_width;
5964     }
5965
5966   g_object_notify (G_OBJECT (entry), "scroll-offset");
5967 }
5968
5969 static void
5970 gtk_entry_move_adjustments (GtkEntry *entry)
5971 {
5972   GtkAllocation allocation;
5973   GtkAdjustment *adjustment;
5974   PangoContext *context;
5975   PangoFontMetrics *metrics;
5976   gint x, layout_x, border_x, border_y;
5977   gint char_width;
5978
5979   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
5980   if (!adjustment)
5981     return;
5982
5983   gtk_widget_get_allocation (&(entry->widget), &allocation);
5984
5985   /* Cursor position, layout offset, border width, and widget allocation */
5986   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
5987   get_layout_position (entry, &layout_x, NULL);
5988   _gtk_entry_get_borders (entry, &border_x, &border_y);
5989   x += allocation.x + layout_x + border_x;
5990
5991   /* Approximate width of a char, so user can see what is ahead/behind */
5992   context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
5993   metrics = pango_context_get_metrics (context, 
5994                                        gtk_widget_get_style (&(entry->widget))->font_desc,
5995                                        pango_context_get_language (context));
5996   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
5997
5998   /* Scroll it */
5999   gtk_adjustment_clamp_page (adjustment, 
6000                              x - (char_width + 1),   /* one char + one pixel before */
6001                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6002 }
6003
6004 static gint
6005 gtk_entry_move_visually (GtkEntry *entry,
6006                          gint      start,
6007                          gint      count)
6008 {
6009   gint index;
6010   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6011   const gchar *text;
6012
6013   text = pango_layout_get_text (layout);
6014   
6015   index = g_utf8_offset_to_pointer (text, start) - text;
6016
6017   while (count != 0)
6018     {
6019       int new_index, new_trailing;
6020       gboolean split_cursor;
6021       gboolean strong;
6022
6023       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6024                     "gtk-split-cursor", &split_cursor,
6025                     NULL);
6026
6027       if (split_cursor)
6028         strong = TRUE;
6029       else
6030         {
6031           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6032           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6033
6034           strong = keymap_direction == entry->resolved_dir;
6035         }
6036       
6037       if (count > 0)
6038         {
6039           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6040           count--;
6041         }
6042       else
6043         {
6044           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6045           count++;
6046         }
6047
6048       if (new_index < 0)
6049         index = 0;
6050       else if (new_index != G_MAXINT)
6051         index = new_index;
6052       
6053       while (new_trailing--)
6054         index = g_utf8_next_char (text + index) - text;
6055     }
6056   
6057   return g_utf8_pointer_to_offset (text, text + index);
6058 }
6059
6060 static gint
6061 gtk_entry_move_logically (GtkEntry *entry,
6062                           gint      start,
6063                           gint      count)
6064 {
6065   gint new_pos = start;
6066   guint length;
6067
6068   length = gtk_entry_buffer_get_length (get_buffer (entry));
6069
6070   /* Prevent any leak of information */
6071   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6072     {
6073       new_pos = CLAMP (start + count, 0, length);
6074     }
6075   else
6076     {
6077       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6078       PangoLogAttr *log_attrs;
6079       gint n_attrs;
6080
6081       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6082
6083       while (count > 0 && new_pos < length)
6084         {
6085           do
6086             new_pos++;
6087           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6088           
6089           count--;
6090         }
6091       while (count < 0 && new_pos > 0)
6092         {
6093           do
6094             new_pos--;
6095           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6096           
6097           count++;
6098         }
6099       
6100       g_free (log_attrs);
6101     }
6102
6103   return new_pos;
6104 }
6105
6106 static gint
6107 gtk_entry_move_forward_word (GtkEntry *entry,
6108                              gint      start,
6109                              gboolean  allow_whitespace)
6110 {
6111   gint new_pos = start;
6112   guint length;
6113
6114   length = gtk_entry_buffer_get_length (get_buffer (entry));
6115
6116   /* Prevent any leak of information */
6117   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6118     {
6119       new_pos = length;
6120     }
6121   else if (new_pos < length)
6122     {
6123       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6124       PangoLogAttr *log_attrs;
6125       gint n_attrs;
6126
6127       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6128       
6129       /* Find the next word boundary */
6130       new_pos++;
6131       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6132                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6133         new_pos++;
6134
6135       g_free (log_attrs);
6136     }
6137
6138   return new_pos;
6139 }
6140
6141
6142 static gint
6143 gtk_entry_move_backward_word (GtkEntry *entry,
6144                               gint      start,
6145                               gboolean  allow_whitespace)
6146 {
6147   gint new_pos = start;
6148
6149   /* Prevent any leak of information */
6150   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6151     {
6152       new_pos = 0;
6153     }
6154   else if (start > 0)
6155     {
6156       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6157       PangoLogAttr *log_attrs;
6158       gint n_attrs;
6159
6160       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6161
6162       new_pos = start - 1;
6163
6164       /* Find the previous word boundary */
6165       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6166                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6167         new_pos--;
6168
6169       g_free (log_attrs);
6170     }
6171
6172   return new_pos;
6173 }
6174
6175 static void
6176 gtk_entry_delete_whitespace (GtkEntry *entry)
6177 {
6178   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6179   PangoLogAttr *log_attrs;
6180   gint n_attrs;
6181   gint start, end;
6182
6183   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6184
6185   start = end = entry->current_pos;
6186   
6187   while (start > 0 && log_attrs[start-1].is_white)
6188     start--;
6189
6190   while (end < n_attrs && log_attrs[end].is_white)
6191     end++;
6192
6193   g_free (log_attrs);
6194
6195   if (start != end)
6196     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6197 }
6198
6199
6200 static void
6201 gtk_entry_select_word (GtkEntry *entry)
6202 {
6203   gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
6204   gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
6205
6206   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6207 }
6208
6209 static void
6210 gtk_entry_select_line (GtkEntry *entry)
6211 {
6212   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6213 }
6214
6215 static gint
6216 truncate_multiline (const gchar *text)
6217 {
6218   gint length;
6219
6220   for (length = 0;
6221        text[length] && text[length] != '\n' && text[length] != '\r';
6222        length++);
6223
6224   return length;
6225 }
6226
6227 static void
6228 paste_received (GtkClipboard *clipboard,
6229                 const gchar  *text,
6230                 gpointer      data)
6231 {
6232   GtkEntry *entry = GTK_ENTRY (data);
6233   GtkEditable *editable = GTK_EDITABLE (entry);
6234   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6235       
6236   if (entry->button == 2)
6237     {
6238       gint pos, start, end;
6239       pos = priv->insert_pos;
6240       gtk_editable_get_selection_bounds (editable, &start, &end);
6241       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6242         gtk_editable_select_region (editable, pos, pos);
6243     }
6244       
6245   if (text)
6246     {
6247       gint pos, start, end;
6248       gint length = -1;
6249       gboolean popup_completion;
6250       GtkEntryCompletion *completion;
6251
6252       completion = gtk_entry_get_completion (entry);
6253
6254       if (entry->truncate_multiline)
6255         length = truncate_multiline (text);
6256
6257       /* only complete if the selection is at the end */
6258       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6259                           MAX (entry->current_pos, entry->selection_bound));
6260
6261       if (completion)
6262         {
6263           if (gtk_widget_get_mapped (completion->priv->popup_window))
6264             _gtk_entry_completion_popdown (completion);
6265
6266           if (!popup_completion && completion->priv->changed_id > 0)
6267             g_signal_handler_block (entry, completion->priv->changed_id);
6268         }
6269
6270       begin_change (entry);
6271       g_object_freeze_notify (G_OBJECT (entry));
6272       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6273         gtk_editable_delete_text (editable, start, end);
6274
6275       pos = entry->current_pos;
6276       gtk_editable_insert_text (editable, text, length, &pos);
6277       gtk_editable_set_position (editable, pos);
6278       g_object_thaw_notify (G_OBJECT (entry));
6279       end_change (entry);
6280
6281       if (completion &&
6282           !popup_completion && completion->priv->changed_id > 0)
6283         g_signal_handler_unblock (entry, completion->priv->changed_id);
6284     }
6285
6286   g_object_unref (entry);
6287 }
6288
6289 static void
6290 gtk_entry_paste (GtkEntry *entry,
6291                  GdkAtom   selection)
6292 {
6293   g_object_ref (entry);
6294   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6295                               paste_received, entry);
6296 }
6297
6298 static void
6299 primary_get_cb (GtkClipboard     *clipboard,
6300                 GtkSelectionData *selection_data,
6301                 guint             info,
6302                 gpointer          data)
6303 {
6304   GtkEntry *entry = GTK_ENTRY (data);
6305   gint start, end;
6306   
6307   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6308     {
6309       gchar *str = gtk_entry_get_display_text (entry, start, end);
6310       gtk_selection_data_set_text (selection_data, str, -1);
6311       g_free (str);
6312     }
6313 }
6314
6315 static void
6316 primary_clear_cb (GtkClipboard *clipboard,
6317                   gpointer      data)
6318 {
6319   GtkEntry *entry = GTK_ENTRY (data);
6320
6321   gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
6322 }
6323
6324 static void
6325 gtk_entry_update_primary_selection (GtkEntry *entry)
6326 {
6327   GtkTargetList *list;
6328   GtkTargetEntry *targets;
6329   GtkClipboard *clipboard;
6330   gint start, end;
6331   gint n_targets;
6332
6333   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6334     return;
6335
6336   list = gtk_target_list_new (NULL, 0);
6337   gtk_target_list_add_text_targets (list, 0);
6338
6339   targets = gtk_target_table_new_from_list (list, &n_targets);
6340
6341   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6342   
6343   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6344     {
6345       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6346                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6347         primary_clear_cb (clipboard, entry);
6348     }
6349   else
6350     {
6351       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6352         gtk_clipboard_clear (clipboard);
6353     }
6354
6355   gtk_target_table_free (targets, n_targets);
6356   gtk_target_list_unref (list);
6357 }
6358
6359 static void
6360 gtk_entry_clear (GtkEntry             *entry,
6361                  GtkEntryIconPosition  icon_pos)
6362 {
6363   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6364   EntryIconInfo *icon_info = priv->icons[icon_pos];
6365
6366   if (!icon_info || icon_info->storage_type == GTK_IMAGE_EMPTY)
6367     return;
6368
6369   g_object_freeze_notify (G_OBJECT (entry));
6370
6371   /* Explicitly check, as the pointer may become invalidated
6372    * during destruction.
6373    */
6374   if (GDK_IS_WINDOW (icon_info->window))
6375     gdk_window_hide (icon_info->window);
6376
6377   if (icon_info->pixbuf)
6378     {
6379       g_object_unref (icon_info->pixbuf);
6380       icon_info->pixbuf = NULL;
6381     }
6382
6383   switch (icon_info->storage_type)
6384     {
6385     case GTK_IMAGE_PIXBUF:
6386       g_object_notify (G_OBJECT (entry),
6387                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6388       break;
6389
6390     case GTK_IMAGE_STOCK:
6391       g_free (icon_info->stock_id);
6392       icon_info->stock_id = NULL;
6393       g_object_notify (G_OBJECT (entry),
6394                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6395       break;
6396
6397     case GTK_IMAGE_ICON_NAME:
6398       g_free (icon_info->icon_name);
6399       icon_info->icon_name = NULL;
6400       g_object_notify (G_OBJECT (entry),
6401                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6402       break;
6403
6404     case GTK_IMAGE_GICON:
6405       if (icon_info->gicon)
6406         {
6407           g_object_unref (icon_info->gicon);
6408           icon_info->gicon = NULL;
6409         }
6410       g_object_notify (G_OBJECT (entry),
6411                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6412       break;
6413
6414     default:
6415       g_assert_not_reached ();
6416       break;
6417     }
6418
6419   icon_info->storage_type = GTK_IMAGE_EMPTY;
6420   g_object_notify (G_OBJECT (entry),
6421                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6422
6423   g_object_thaw_notify (G_OBJECT (entry));
6424 }
6425
6426 static void
6427 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6428                          GtkEntryIconPosition  icon_pos)
6429 {
6430   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6431   EntryIconInfo *icon_info = priv->icons[icon_pos];
6432   GtkIconInfo *info;
6433   GtkIconTheme *icon_theme;
6434   GtkSettings *settings;
6435   GtkStateType state;
6436   GtkWidget *widget;
6437   GdkScreen *screen;
6438   gint width, height;
6439
6440   if (!icon_info || icon_info->pixbuf)
6441     return;
6442
6443   widget = GTK_WIDGET (entry);
6444
6445   switch (icon_info->storage_type)
6446     {
6447     case GTK_IMAGE_EMPTY:
6448     case GTK_IMAGE_PIXBUF:
6449       break;
6450     case GTK_IMAGE_STOCK:
6451       state = gtk_widget_get_state (widget);
6452       gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6453       icon_info->pixbuf = gtk_widget_render_icon (widget,
6454                                                   icon_info->stock_id,
6455                                                   GTK_ICON_SIZE_MENU,
6456                                                   NULL);
6457       if (!icon_info->pixbuf)
6458         icon_info->pixbuf = gtk_widget_render_icon (widget,
6459                                                     GTK_STOCK_MISSING_IMAGE,
6460                                                     GTK_ICON_SIZE_MENU,
6461                                                     NULL);
6462       gtk_widget_set_state (widget, state);
6463       break;
6464
6465     case GTK_IMAGE_ICON_NAME:
6466       screen = gtk_widget_get_screen (widget);
6467       if (screen)
6468         {
6469           icon_theme = gtk_icon_theme_get_for_screen (screen);
6470           settings = gtk_settings_get_for_screen (screen);
6471           
6472           gtk_icon_size_lookup_for_settings (settings,
6473                                              GTK_ICON_SIZE_MENU,
6474                                              &width, &height);
6475
6476           icon_info->pixbuf = gtk_icon_theme_load_icon (icon_theme,
6477                                                         icon_info->icon_name,
6478                                                         MIN (width, height),
6479                                                         0, NULL);
6480
6481           if (icon_info->pixbuf == NULL)
6482             {
6483               state = gtk_widget_get_state (widget);
6484               gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6485               icon_info->pixbuf = gtk_widget_render_icon (widget,
6486                                                           GTK_STOCK_MISSING_IMAGE,
6487                                                           GTK_ICON_SIZE_MENU,
6488                                                           NULL);
6489               gtk_widget_set_state (widget, state);
6490             }
6491         }
6492       break;
6493
6494     case GTK_IMAGE_GICON:
6495       screen = gtk_widget_get_screen (widget);
6496       if (screen)
6497         {
6498           icon_theme = gtk_icon_theme_get_for_screen (screen);
6499           settings = gtk_settings_get_for_screen (screen);
6500
6501           gtk_icon_size_lookup_for_settings (settings,
6502                                              GTK_ICON_SIZE_MENU,
6503                                              &width, &height);
6504
6505           info = gtk_icon_theme_lookup_by_gicon (icon_theme,
6506                                                  icon_info->gicon,
6507                                                  MIN (width, height), 
6508                                                  GTK_ICON_LOOKUP_USE_BUILTIN);
6509           if (info)
6510             {
6511               icon_info->pixbuf = gtk_icon_info_load_icon (info, NULL);
6512               gtk_icon_info_free (info);
6513             }
6514
6515           if (icon_info->pixbuf == NULL)
6516             {
6517               state = gtk_widget_get_state (widget);
6518               gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6519               icon_info->pixbuf = gtk_widget_render_icon (widget,
6520                                                           GTK_STOCK_MISSING_IMAGE,
6521                                                           GTK_ICON_SIZE_MENU,
6522                                                           NULL);
6523               gtk_widget_set_state (widget, state);
6524             }
6525         }
6526       break;
6527
6528     default:
6529       g_assert_not_reached ();
6530       break;
6531     }
6532     
6533   if (icon_info->pixbuf != NULL && icon_info->window != NULL)
6534     gdk_window_show_unraised (icon_info->window);
6535 }
6536
6537
6538 /* Public API
6539  */
6540
6541 /**
6542  * gtk_entry_new:
6543  *
6544  * Creates a new entry.
6545  *
6546  * Return value: a new #GtkEntry.
6547  */
6548 GtkWidget*
6549 gtk_entry_new (void)
6550 {
6551   return g_object_new (GTK_TYPE_ENTRY, NULL);
6552 }
6553
6554 /**
6555  * gtk_entry_new_with_buffer:
6556  * @buffer: The buffer to use for the new #GtkEntry.
6557  *
6558  * Creates a new entry with the specified text buffer.
6559  *
6560  * Return value: a new #GtkEntry
6561  *
6562  * Since: 2.18
6563  */
6564 GtkWidget*
6565 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6566 {
6567   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6568   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6569 }
6570
6571 static GtkEntryBuffer*
6572 get_buffer (GtkEntry *entry)
6573 {
6574   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6575
6576   if (priv->buffer == NULL)
6577     {
6578       GtkEntryBuffer *buffer;
6579       buffer = gtk_entry_buffer_new (NULL, 0);
6580       gtk_entry_set_buffer (entry, buffer);
6581       g_object_unref (buffer);
6582     }
6583
6584   return priv->buffer;
6585 }
6586
6587 /**
6588  * gtk_entry_get_buffer:
6589  * @entry: a #GtkEntry
6590  *
6591  * Get the #GtkEntryBuffer object which holds the text for
6592  * this widget.
6593  *
6594  * Since: 2.18
6595  *
6596  * Returns: (transfer none): A #GtkEntryBuffer object.
6597  */
6598 GtkEntryBuffer*
6599 gtk_entry_get_buffer (GtkEntry *entry)
6600 {
6601   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6602
6603   return get_buffer (entry);
6604 }
6605
6606 /**
6607  * gtk_entry_set_buffer:
6608  * @entry: a #GtkEntry
6609  * @buffer: a #GtkEntryBuffer
6610  *
6611  * Set the #GtkEntryBuffer object which holds the text for
6612  * this widget.
6613  *
6614  * Since: 2.18
6615  */
6616 void
6617 gtk_entry_set_buffer (GtkEntry       *entry,
6618                       GtkEntryBuffer *buffer)
6619 {
6620   GtkEntryPrivate *priv;
6621   GObject *obj;
6622
6623   g_return_if_fail (GTK_IS_ENTRY (entry));
6624   priv = GTK_ENTRY_GET_PRIVATE (entry);
6625
6626   if (buffer)
6627     {
6628       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6629       g_object_ref (buffer);
6630     }
6631
6632   if (priv->buffer)
6633     {
6634       buffer_disconnect_signals (entry);
6635       g_object_unref (priv->buffer);
6636     }
6637
6638   priv->buffer = buffer;
6639
6640   if (priv->buffer)
6641      buffer_connect_signals (entry);
6642
6643   obj = G_OBJECT (entry);
6644   g_object_freeze_notify (obj);
6645   g_object_notify (obj, "buffer");
6646   g_object_notify (obj, "text");
6647   g_object_notify (obj, "text-length");
6648   g_object_notify (obj, "max-length");
6649   g_object_notify (obj, "visibility");
6650   g_object_notify (obj, "invisible-char");
6651   g_object_notify (obj, "invisible-char-set");
6652   g_object_thaw_notify (obj);
6653
6654   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6655   gtk_entry_recompute (entry);
6656 }
6657
6658 /**
6659  * gtk_entry_set_text:
6660  * @entry: a #GtkEntry
6661  * @text: the new text
6662  *
6663  * Sets the text in the widget to the given
6664  * value, replacing the current contents.
6665  *
6666  * See gtk_entry_buffer_set_text().
6667  */
6668 void
6669 gtk_entry_set_text (GtkEntry    *entry,
6670                     const gchar *text)
6671 {
6672   gint tmp_pos;
6673   GtkEntryCompletion *completion;
6674   GtkEntryPrivate *priv;
6675
6676   g_return_if_fail (GTK_IS_ENTRY (entry));
6677   g_return_if_fail (text != NULL);
6678   priv = GTK_ENTRY_GET_PRIVATE (entry);
6679
6680   /* Actually setting the text will affect the cursor and selection;
6681    * if the contents don't actually change, this will look odd to the user.
6682    */
6683   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6684     return;
6685
6686   completion = gtk_entry_get_completion (entry);
6687   if (completion && completion->priv->changed_id > 0)
6688     g_signal_handler_block (entry, completion->priv->changed_id);
6689
6690   begin_change (entry);
6691   g_object_freeze_notify (G_OBJECT (entry));
6692   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6693   tmp_pos = 0;
6694   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6695   g_object_thaw_notify (G_OBJECT (entry));
6696   end_change (entry);
6697
6698   if (completion && completion->priv->changed_id > 0)
6699     g_signal_handler_unblock (entry, completion->priv->changed_id);
6700 }
6701
6702 /**
6703  * gtk_entry_set_visibility:
6704  * @entry: a #GtkEntry
6705  * @visible: %TRUE if the contents of the entry are displayed
6706  *           as plaintext
6707  *
6708  * Sets whether the contents of the entry are visible or not. 
6709  * When visibility is set to %FALSE, characters are displayed 
6710  * as the invisible char, and will also appear that way when 
6711  * the text in the entry widget is copied elsewhere.
6712  *
6713  * By default, GTK+ picks the best invisible character available
6714  * in the current font, but it can be changed with
6715  * gtk_entry_set_invisible_char().
6716  */
6717 void
6718 gtk_entry_set_visibility (GtkEntry *entry,
6719                           gboolean visible)
6720 {
6721   g_return_if_fail (GTK_IS_ENTRY (entry));
6722
6723   visible = visible != FALSE;
6724
6725   if (entry->visible != visible)
6726     {
6727       entry->visible = visible;
6728
6729       g_object_notify (G_OBJECT (entry), "visibility");
6730       gtk_entry_recompute (entry);
6731     }
6732 }
6733
6734 /**
6735  * gtk_entry_get_visibility:
6736  * @entry: a #GtkEntry
6737  *
6738  * Retrieves whether the text in @entry is visible. See
6739  * gtk_entry_set_visibility().
6740  *
6741  * Return value: %TRUE if the text is currently visible
6742  **/
6743 gboolean
6744 gtk_entry_get_visibility (GtkEntry *entry)
6745 {
6746   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6747
6748   return entry->visible;
6749 }
6750
6751 /**
6752  * gtk_entry_set_invisible_char:
6753  * @entry: a #GtkEntry
6754  * @ch: a Unicode character
6755  * 
6756  * Sets the character to use in place of the actual text when
6757  * gtk_entry_set_visibility() has been called to set text visibility
6758  * to %FALSE. i.e. this is the character used in "password mode" to
6759  * show the user how many characters have been typed. By default, GTK+
6760  * picks the best invisible char available in the current font. If you
6761  * set the invisible char to 0, then the user will get no feedback
6762  * at all; there will be no text on the screen as they type.
6763  **/
6764 void
6765 gtk_entry_set_invisible_char (GtkEntry *entry,
6766                               gunichar  ch)
6767 {
6768   GtkEntryPrivate *priv;
6769
6770   g_return_if_fail (GTK_IS_ENTRY (entry));
6771
6772   priv = GTK_ENTRY_GET_PRIVATE (entry);
6773
6774   if (!priv->invisible_char_set)
6775     {
6776       priv->invisible_char_set = TRUE;
6777       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6778     }
6779
6780   if (ch == entry->invisible_char)
6781     return;
6782
6783   entry->invisible_char = ch;
6784   g_object_notify (G_OBJECT (entry), "invisible-char");
6785   gtk_entry_recompute (entry);  
6786 }
6787
6788 /**
6789  * gtk_entry_get_invisible_char:
6790  * @entry: a #GtkEntry
6791  *
6792  * Retrieves the character displayed in place of the real characters
6793  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6794  *
6795  * Return value: the current invisible char, or 0, if the entry does not
6796  *               show invisible text at all. 
6797  **/
6798 gunichar
6799 gtk_entry_get_invisible_char (GtkEntry *entry)
6800 {
6801   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6802
6803   return entry->invisible_char;
6804 }
6805
6806 /**
6807  * gtk_entry_unset_invisible_char:
6808  * @entry: a #GtkEntry
6809  *
6810  * Unsets the invisible char previously set with
6811  * gtk_entry_set_invisible_char(). So that the
6812  * default invisible char is used again.
6813  *
6814  * Since: 2.16
6815  **/
6816 void
6817 gtk_entry_unset_invisible_char (GtkEntry *entry)
6818 {
6819   GtkEntryPrivate *priv;
6820   gunichar ch;
6821
6822   g_return_if_fail (GTK_IS_ENTRY (entry));
6823
6824   priv = GTK_ENTRY_GET_PRIVATE (entry);
6825
6826   if (!priv->invisible_char_set)
6827     return;
6828
6829   priv->invisible_char_set = FALSE;
6830   ch = find_invisible_char (GTK_WIDGET (entry));
6831
6832   if (entry->invisible_char != ch)
6833     {
6834       entry->invisible_char = ch;
6835       g_object_notify (G_OBJECT (entry), "invisible-char");
6836     }
6837
6838   g_object_notify (G_OBJECT (entry), "invisible-char-set");
6839   gtk_entry_recompute (entry);
6840 }
6841
6842 /**
6843  * gtk_entry_set_overwrite_mode:
6844  * @entry: a #GtkEntry
6845  * @overwrite: new value
6846  * 
6847  * Sets whether the text is overwritten when typing in the #GtkEntry.
6848  *
6849  * Since: 2.14
6850  **/
6851 void
6852 gtk_entry_set_overwrite_mode (GtkEntry *entry,
6853                               gboolean  overwrite)
6854 {
6855   g_return_if_fail (GTK_IS_ENTRY (entry));
6856   
6857   if (entry->overwrite_mode == overwrite) 
6858     return;
6859   
6860   gtk_entry_toggle_overwrite (entry);
6861
6862   g_object_notify (G_OBJECT (entry), "overwrite-mode");
6863 }
6864
6865 /**
6866  * gtk_entry_get_overwrite_mode:
6867  * @entry: a #GtkEntry
6868  * 
6869  * Gets the value set by gtk_entry_set_overwrite_mode().
6870  * 
6871  * Return value: whether the text is overwritten when typing.
6872  *
6873  * Since: 2.14
6874  **/
6875 gboolean
6876 gtk_entry_get_overwrite_mode (GtkEntry *entry)
6877 {
6878   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6879
6880   return entry->overwrite_mode;
6881 }
6882
6883 /**
6884  * gtk_entry_get_text:
6885  * @entry: a #GtkEntry
6886  *
6887  * Retrieves the contents of the entry widget.
6888  * See also gtk_editable_get_chars().
6889  *
6890  * This is equivalent to:
6891  *
6892  * <informalexample><programlisting>
6893  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
6894  * </programlisting></informalexample>
6895  *
6896  * Return value: a pointer to the contents of the widget as a
6897  *      string. This string points to internally allocated
6898  *      storage in the widget and must not be freed, modified or
6899  *      stored.
6900  **/
6901 G_CONST_RETURN gchar*
6902 gtk_entry_get_text (GtkEntry *entry)
6903 {
6904   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6905   return gtk_entry_buffer_get_text (get_buffer (entry));
6906 }
6907
6908 /**
6909  * gtk_entry_set_max_length:
6910  * @entry: a #GtkEntry
6911  * @max: the maximum length of the entry, or 0 for no maximum.
6912  *   (other than the maximum length of entries.) The value passed in will
6913  *   be clamped to the range 0-65536.
6914  * 
6915  * Sets the maximum allowed length of the contents of the widget. If
6916  * the current contents are longer than the given length, then they
6917  * will be truncated to fit.
6918  *
6919  * This is equivalent to:
6920  *
6921  * <informalexample><programlisting>
6922  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
6923  * </programlisting></informalexample>
6924  **/
6925 void
6926 gtk_entry_set_max_length (GtkEntry     *entry,
6927                           gint          max)
6928 {
6929   g_return_if_fail (GTK_IS_ENTRY (entry));
6930   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
6931 }
6932
6933 /**
6934  * gtk_entry_get_max_length:
6935  * @entry: a #GtkEntry
6936  *
6937  * Retrieves the maximum allowed length of the text in
6938  * @entry. See gtk_entry_set_max_length().
6939  *
6940  * This is equivalent to:
6941  *
6942  * <informalexample><programlisting>
6943  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
6944  * </programlisting></informalexample>
6945  *
6946  * Return value: the maximum allowed number of characters
6947  *               in #GtkEntry, or 0 if there is no maximum.
6948  **/
6949 gint
6950 gtk_entry_get_max_length (GtkEntry *entry)
6951 {
6952   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6953   return gtk_entry_buffer_get_max_length (get_buffer (entry));
6954 }
6955
6956 /**
6957  * gtk_entry_get_text_length:
6958  * @entry: a #GtkEntry
6959  *
6960  * Retrieves the current length of the text in
6961  * @entry. 
6962  *
6963  * This is equivalent to:
6964  *
6965  * <informalexample><programlisting>
6966  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
6967  * </programlisting></informalexample>
6968  *
6969  * Return value: the current number of characters
6970  *               in #GtkEntry, or 0 if there are none.
6971  *
6972  * Since: 2.14
6973  **/
6974 guint16
6975 gtk_entry_get_text_length (GtkEntry *entry)
6976 {
6977   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6978   return gtk_entry_buffer_get_length (get_buffer (entry));
6979 }
6980
6981 /**
6982  * gtk_entry_set_activates_default:
6983  * @entry: a #GtkEntry
6984  * @setting: %TRUE to activate window's default widget on Enter keypress
6985  *
6986  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
6987  * widget for the window containing the entry. This usually means that
6988  * the dialog box containing the entry will be closed, since the default
6989  * widget is usually one of the dialog buttons.
6990  *
6991  * (For experts: if @setting is %TRUE, the entry calls
6992  * gtk_window_activate_default() on the window containing the entry, in
6993  * the default handler for the #GtkWidget::activate signal.)
6994  **/
6995 void
6996 gtk_entry_set_activates_default (GtkEntry *entry,
6997                                  gboolean  setting)
6998 {
6999   g_return_if_fail (GTK_IS_ENTRY (entry));
7000   setting = setting != FALSE;
7001
7002   if (setting != entry->activates_default)
7003     {
7004       entry->activates_default = setting;
7005       g_object_notify (G_OBJECT (entry), "activates-default");
7006     }
7007 }
7008
7009 /**
7010  * gtk_entry_get_activates_default:
7011  * @entry: a #GtkEntry
7012  * 
7013  * Retrieves the value set by gtk_entry_set_activates_default().
7014  * 
7015  * Return value: %TRUE if the entry will activate the default widget
7016  **/
7017 gboolean
7018 gtk_entry_get_activates_default (GtkEntry *entry)
7019 {
7020   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7021
7022   return entry->activates_default;
7023 }
7024
7025 /**
7026  * gtk_entry_set_width_chars:
7027  * @entry: a #GtkEntry
7028  * @n_chars: width in chars
7029  *
7030  * Changes the size request of the entry to be about the right size
7031  * for @n_chars characters. Note that it changes the size
7032  * <emphasis>request</emphasis>, the size can still be affected by
7033  * how you pack the widget into containers. If @n_chars is -1, the
7034  * size reverts to the default entry size.
7035  **/
7036 void
7037 gtk_entry_set_width_chars (GtkEntry *entry,
7038                            gint      n_chars)
7039 {
7040   g_return_if_fail (GTK_IS_ENTRY (entry));
7041
7042   if (entry->width_chars != n_chars)
7043     {
7044       entry->width_chars = n_chars;
7045       g_object_notify (G_OBJECT (entry), "width-chars");
7046       gtk_widget_queue_resize (GTK_WIDGET (entry));
7047     }
7048 }
7049
7050 /**
7051  * gtk_entry_get_width_chars:
7052  * @entry: a #GtkEntry
7053  * 
7054  * Gets the value set by gtk_entry_set_width_chars().
7055  * 
7056  * Return value: number of chars to request space for, or negative if unset
7057  **/
7058 gint
7059 gtk_entry_get_width_chars (GtkEntry *entry)
7060 {
7061   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7062
7063   return entry->width_chars;
7064 }
7065
7066 /**
7067  * gtk_entry_set_has_frame:
7068  * @entry: a #GtkEntry
7069  * @setting: new value
7070  * 
7071  * Sets whether the entry has a beveled frame around it.
7072  **/
7073 void
7074 gtk_entry_set_has_frame (GtkEntry *entry,
7075                          gboolean  setting)
7076 {
7077   g_return_if_fail (GTK_IS_ENTRY (entry));
7078
7079   setting = (setting != FALSE);
7080
7081   if (entry->has_frame == setting)
7082     return;
7083
7084   gtk_widget_queue_resize (GTK_WIDGET (entry));
7085   entry->has_frame = setting;
7086   g_object_notify (G_OBJECT (entry), "has-frame");
7087 }
7088
7089 /**
7090  * gtk_entry_get_has_frame:
7091  * @entry: a #GtkEntry
7092  * 
7093  * Gets the value set by gtk_entry_set_has_frame().
7094  * 
7095  * Return value: whether the entry has a beveled frame
7096  **/
7097 gboolean
7098 gtk_entry_get_has_frame (GtkEntry *entry)
7099 {
7100   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7101
7102   return entry->has_frame;
7103 }
7104
7105 /**
7106  * gtk_entry_set_inner_border:
7107  * @entry: a #GtkEntry
7108  * @border: (allow-none): a #GtkBorder, or %NULL
7109  *
7110  * Sets %entry's inner-border property to %border, or clears it if %NULL
7111  * is passed. The inner-border is the area around the entry's text, but
7112  * inside its frame.
7113  *
7114  * If set, this property overrides the inner-border style property.
7115  * Overriding the style-provided border is useful when you want to do
7116  * in-place editing of some text in a canvas or list widget, where
7117  * pixel-exact positioning of the entry is important.
7118  *
7119  * Since: 2.10
7120  **/
7121 void
7122 gtk_entry_set_inner_border (GtkEntry        *entry,
7123                             const GtkBorder *border)
7124 {
7125   g_return_if_fail (GTK_IS_ENTRY (entry));
7126
7127   gtk_widget_queue_resize (GTK_WIDGET (entry));
7128
7129   if (border)
7130     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7131                              gtk_border_copy (border),
7132                              (GDestroyNotify) gtk_border_free);
7133   else
7134     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7135
7136   g_object_notify (G_OBJECT (entry), "inner-border");
7137 }
7138
7139 /**
7140  * gtk_entry_get_inner_border:
7141  * @entry: a #GtkEntry
7142  *
7143  * This function returns the entry's #GtkEntry:inner-border property. See
7144  * gtk_entry_set_inner_border() for more information.
7145  *
7146  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7147  *
7148  * Since: 2.10
7149  **/
7150 G_CONST_RETURN GtkBorder *
7151 gtk_entry_get_inner_border (GtkEntry *entry)
7152 {
7153   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7154
7155   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7156 }
7157
7158 /**
7159  * gtk_entry_get_layout:
7160  * @entry: a #GtkEntry
7161  * 
7162  * Gets the #PangoLayout used to display the entry.
7163  * The layout is useful to e.g. convert text positions to
7164  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7165  * The returned layout is owned by the entry and must not be 
7166  * modified or freed by the caller.
7167  *
7168  * Keep in mind that the layout text may contain a preedit string, so
7169  * gtk_entry_layout_index_to_text_index() and
7170  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7171  * indices in the layout to byte indices in the entry contents.
7172  *
7173  * Return value: (transfer none): the #PangoLayout for this entry
7174  **/
7175 PangoLayout*
7176 gtk_entry_get_layout (GtkEntry *entry)
7177 {
7178   PangoLayout *layout;
7179   
7180   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7181
7182   layout = gtk_entry_ensure_layout (entry, TRUE);
7183
7184   return layout;
7185 }
7186
7187
7188 /**
7189  * gtk_entry_layout_index_to_text_index:
7190  * @entry: a #GtkEntry
7191  * @layout_index: byte index into the entry layout text
7192  * 
7193  * Converts from a position in the entry contents (returned
7194  * by gtk_entry_get_text()) to a position in the
7195  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7196  * with text retrieved via pango_layout_get_text()).
7197  * 
7198  * Return value: byte index into the entry contents
7199  **/
7200 gint
7201 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7202                                       gint      layout_index)
7203 {
7204   PangoLayout *layout;
7205   const gchar *text;
7206   gint cursor_index;
7207   
7208   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7209
7210   layout = gtk_entry_ensure_layout (entry, TRUE);
7211   text = pango_layout_get_text (layout);
7212   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7213   
7214   if (layout_index >= cursor_index && entry->preedit_length)
7215     {
7216       if (layout_index >= cursor_index + entry->preedit_length)
7217         layout_index -= entry->preedit_length;
7218       else
7219         layout_index = cursor_index;
7220     }
7221
7222   return layout_index;
7223 }
7224
7225 /**
7226  * gtk_entry_text_index_to_layout_index:
7227  * @entry: a #GtkEntry
7228  * @text_index: byte index into the entry contents
7229  * 
7230  * Converts from a position in the entry's #PangoLayout (returned by
7231  * gtk_entry_get_layout()) to a position in the entry contents
7232  * (returned by gtk_entry_get_text()).
7233  * 
7234  * Return value: byte index into the entry layout text
7235  **/
7236 gint
7237 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7238                                       gint      text_index)
7239 {
7240   PangoLayout *layout;
7241   const gchar *text;
7242   gint cursor_index;
7243   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7244
7245   layout = gtk_entry_ensure_layout (entry, TRUE);
7246   text = pango_layout_get_text (layout);
7247   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7248   
7249   if (text_index > cursor_index)
7250     text_index += entry->preedit_length;
7251
7252   return text_index;
7253 }
7254
7255 /**
7256  * gtk_entry_get_layout_offsets:
7257  * @entry: a #GtkEntry
7258  * @x: (allow-none): location to store X offset of layout, or %NULL
7259  * @y: (allow-none): location to store Y offset of layout, or %NULL
7260  *
7261  *
7262  * Obtains the position of the #PangoLayout used to render text
7263  * in the entry, in widget coordinates. Useful if you want to line
7264  * up the text in an entry with some other text, e.g. when using the
7265  * entry to implement editable cells in a sheet widget.
7266  *
7267  * Also useful to convert mouse events into coordinates inside the
7268  * #PangoLayout, e.g. to take some action if some part of the entry text
7269  * is clicked.
7270  * 
7271  * Note that as the user scrolls around in the entry the offsets will
7272  * change; you'll need to connect to the "notify::scroll-offset"
7273  * signal to track this. Remember when using the #PangoLayout
7274  * functions you need to convert to and from pixels using
7275  * PANGO_PIXELS() or #PANGO_SCALE.
7276  *
7277  * Keep in mind that the layout text may contain a preedit string, so
7278  * gtk_entry_layout_index_to_text_index() and
7279  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7280  * indices in the layout to byte indices in the entry contents.
7281  **/
7282 void
7283 gtk_entry_get_layout_offsets (GtkEntry *entry,
7284                               gint     *x,
7285                               gint     *y)
7286 {
7287   gint text_area_x, text_area_y;
7288   
7289   g_return_if_fail (GTK_IS_ENTRY (entry));
7290
7291   /* this gets coords relative to text area */
7292   get_layout_position (entry, x, y);
7293
7294   /* convert to widget coords */
7295   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7296   
7297   if (x)
7298     *x += text_area_x;
7299
7300   if (y)
7301     *y += text_area_y;
7302 }
7303
7304
7305 /**
7306  * gtk_entry_set_alignment:
7307  * @entry: a #GtkEntry
7308  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7309  *          Reversed for RTL layouts
7310  * 
7311  * Sets the alignment for the contents of the entry. This controls
7312  * the horizontal positioning of the contents when the displayed
7313  * text is shorter than the width of the entry.
7314  *
7315  * Since: 2.4
7316  **/
7317 void
7318 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7319 {
7320   GtkEntryPrivate *priv;
7321   
7322   g_return_if_fail (GTK_IS_ENTRY (entry));
7323
7324   priv = GTK_ENTRY_GET_PRIVATE (entry);
7325
7326   if (xalign < 0.0)
7327     xalign = 0.0;
7328   else if (xalign > 1.0)
7329     xalign = 1.0;
7330
7331   if (xalign != priv->xalign)
7332     {
7333       priv->xalign = xalign;
7334
7335       gtk_entry_recompute (entry);
7336
7337       g_object_notify (G_OBJECT (entry), "xalign");
7338     }
7339 }
7340
7341 /**
7342  * gtk_entry_get_alignment:
7343  * @entry: a #GtkEntry
7344  * 
7345  * Gets the value set by gtk_entry_set_alignment().
7346  * 
7347  * Return value: the alignment
7348  *
7349  * Since: 2.4
7350  **/
7351 gfloat
7352 gtk_entry_get_alignment (GtkEntry *entry)
7353 {
7354   GtkEntryPrivate *priv;
7355   
7356   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7357
7358   priv = GTK_ENTRY_GET_PRIVATE (entry);
7359
7360   return priv->xalign;
7361 }
7362
7363 /**
7364  * gtk_entry_set_icon_from_pixbuf:
7365  * @entry: a #GtkEntry
7366  * @icon_pos: Icon position
7367  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7368  *
7369  * Sets the icon shown in the specified position using a pixbuf.
7370  *
7371  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7372  *
7373  * Since: 2.16
7374  */
7375 void
7376 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7377                                 GtkEntryIconPosition  icon_pos,
7378                                 GdkPixbuf            *pixbuf)
7379 {
7380   GtkEntryPrivate *priv;
7381   EntryIconInfo *icon_info;
7382
7383   g_return_if_fail (GTK_IS_ENTRY (entry));
7384   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7385
7386   priv = GTK_ENTRY_GET_PRIVATE (entry);
7387
7388   if ((icon_info = priv->icons[icon_pos]) == NULL)
7389     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7390
7391   g_object_freeze_notify (G_OBJECT (entry));
7392
7393   if (pixbuf)
7394     g_object_ref (pixbuf);
7395
7396   gtk_entry_clear (entry, icon_pos);
7397
7398   if (pixbuf)
7399     {
7400       icon_info->storage_type = GTK_IMAGE_PIXBUF;
7401       icon_info->pixbuf = pixbuf;
7402
7403       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7404         {
7405           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7406           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7407         }
7408       else
7409         {
7410           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7411           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7412         }
7413
7414       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7415           gdk_window_show_unraised (icon_info->window);
7416     }
7417
7418   gtk_entry_ensure_pixbuf (entry, icon_pos);
7419   
7420   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7421     gtk_widget_queue_resize (GTK_WIDGET (entry));
7422
7423   g_object_thaw_notify (G_OBJECT (entry));
7424 }
7425
7426 /**
7427  * gtk_entry_set_icon_from_stock:
7428  * @entry: A #GtkEntry
7429  * @icon_pos: Icon position
7430  * @stock_id: (allow-none): The name of the stock item, or %NULL
7431  *
7432  * Sets the icon shown in the entry at the specified position from
7433  * a stock image.
7434  *
7435  * If @stock_id is %NULL, no icon will be shown in the specified position.
7436  *
7437  * Since: 2.16
7438  */
7439 void
7440 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7441                                GtkEntryIconPosition  icon_pos,
7442                                const gchar          *stock_id)
7443 {
7444   GtkEntryPrivate *priv;
7445   EntryIconInfo *icon_info;
7446   gchar *new_id;
7447
7448   g_return_if_fail (GTK_IS_ENTRY (entry));
7449   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7450
7451   priv = GTK_ENTRY_GET_PRIVATE (entry);
7452
7453   if ((icon_info = priv->icons[icon_pos]) == NULL)
7454     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7455
7456   g_object_freeze_notify (G_OBJECT (entry));
7457
7458   gtk_widget_ensure_style (GTK_WIDGET (entry));
7459
7460   /* need to dup before clearing */
7461   new_id = g_strdup (stock_id);
7462
7463   gtk_entry_clear (entry, icon_pos);
7464
7465   if (new_id != NULL)
7466     {
7467       icon_info->storage_type = GTK_IMAGE_STOCK;
7468       icon_info->stock_id = new_id;
7469
7470       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7471         {
7472           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7473           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7474         }
7475       else
7476         {
7477           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7478           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7479         }
7480
7481       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7482           gdk_window_show_unraised (icon_info->window);
7483     }
7484
7485   gtk_entry_ensure_pixbuf (entry, icon_pos);
7486
7487   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7488     gtk_widget_queue_resize (GTK_WIDGET (entry));
7489
7490   g_object_thaw_notify (G_OBJECT (entry));
7491 }
7492
7493 /**
7494  * gtk_entry_set_icon_from_icon_name:
7495  * @entry: A #GtkEntry
7496  * @icon_pos: The position at which to set the icon
7497  * @icon_name: (allow-none): An icon name, or %NULL
7498  *
7499  * Sets the icon shown in the entry at the specified position
7500  * from the current icon theme.
7501  *
7502  * If the icon name isn't known, a "broken image" icon will be displayed
7503  * instead.
7504  *
7505  * If @icon_name is %NULL, no icon will be shown in the specified position.
7506  *
7507  * Since: 2.16
7508  */
7509 void
7510 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7511                                    GtkEntryIconPosition  icon_pos,
7512                                    const gchar          *icon_name)
7513 {
7514   GtkEntryPrivate *priv;
7515   EntryIconInfo *icon_info;
7516   gchar *new_name;
7517
7518   g_return_if_fail (GTK_IS_ENTRY (entry));
7519   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7520
7521   priv = GTK_ENTRY_GET_PRIVATE (entry);
7522
7523   if ((icon_info = priv->icons[icon_pos]) == NULL)
7524     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7525
7526   g_object_freeze_notify (G_OBJECT (entry));
7527
7528   gtk_widget_ensure_style (GTK_WIDGET (entry));
7529
7530   /* need to dup before clearing */
7531   new_name = g_strdup (icon_name);
7532
7533   gtk_entry_clear (entry, icon_pos);
7534
7535   if (new_name != NULL)
7536     {
7537       icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7538       icon_info->icon_name = new_name;
7539
7540       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7541         {
7542           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7543           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7544         }
7545       else
7546         {
7547           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7548           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7549         }
7550
7551       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7552           gdk_window_show_unraised (icon_info->window);
7553     }
7554
7555   gtk_entry_ensure_pixbuf (entry, icon_pos);
7556
7557   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7558     gtk_widget_queue_resize (GTK_WIDGET (entry));
7559
7560   g_object_thaw_notify (G_OBJECT (entry));
7561 }
7562
7563 /**
7564  * gtk_entry_set_icon_from_gicon:
7565  * @entry: A #GtkEntry
7566  * @icon_pos: The position at which to set the icon
7567  * @icon: (allow-none): The icon to set, or %NULL
7568  *
7569  * Sets the icon shown in the entry at the specified position
7570  * from the current icon theme.
7571  * If the icon isn't known, a "broken image" icon will be displayed
7572  * instead.
7573  *
7574  * If @icon is %NULL, no icon will be shown in the specified position.
7575  *
7576  * Since: 2.16
7577  */
7578 void
7579 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7580                                GtkEntryIconPosition  icon_pos,
7581                                GIcon                *icon)
7582 {
7583   GtkEntryPrivate *priv;
7584   EntryIconInfo *icon_info;
7585
7586   g_return_if_fail (GTK_IS_ENTRY (entry));
7587   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7588
7589   priv = GTK_ENTRY_GET_PRIVATE (entry);
7590
7591   if ((icon_info = priv->icons[icon_pos]) == NULL)
7592     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7593
7594   g_object_freeze_notify (G_OBJECT (entry));
7595
7596   /* need to ref before clearing */
7597   if (icon)
7598     g_object_ref (icon);
7599
7600   gtk_entry_clear (entry, icon_pos);
7601
7602   if (icon)
7603     {
7604       icon_info->storage_type = GTK_IMAGE_GICON;
7605       icon_info->gicon = icon;
7606
7607       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7608         {
7609           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7610           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7611         }
7612       else
7613         {
7614           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7615           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7616         }
7617
7618       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7619           gdk_window_show_unraised (icon_info->window);
7620     }
7621
7622   gtk_entry_ensure_pixbuf (entry, icon_pos);
7623
7624   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7625     gtk_widget_queue_resize (GTK_WIDGET (entry));
7626
7627   g_object_thaw_notify (G_OBJECT (entry));
7628 }
7629
7630 /**
7631  * gtk_entry_set_icon_activatable:
7632  * @entry: A #GtkEntry
7633  * @icon_pos: Icon position
7634  * @activatable: %TRUE if the icon should be activatable
7635  *
7636  * Sets whether the icon is activatable.
7637  *
7638  * Since: 2.16
7639  */
7640 void
7641 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7642                                 GtkEntryIconPosition  icon_pos,
7643                                 gboolean              activatable)
7644 {
7645   GtkEntryPrivate *priv;
7646   EntryIconInfo *icon_info;
7647
7648   g_return_if_fail (GTK_IS_ENTRY (entry));
7649   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7650
7651   priv = GTK_ENTRY_GET_PRIVATE (entry);
7652
7653   if ((icon_info = priv->icons[icon_pos]) == NULL)
7654     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7655
7656   activatable = activatable != FALSE;
7657
7658   if (icon_info->nonactivatable != !activatable)
7659     {
7660       icon_info->nonactivatable = !activatable;
7661
7662       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7663         update_cursors (GTK_WIDGET (entry));
7664
7665       g_object_notify (G_OBJECT (entry),
7666                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7667     }
7668 }
7669
7670 /**
7671  * gtk_entry_get_icon_activatable:
7672  * @entry: a #GtkEntry
7673  * @icon_pos: Icon position
7674  *
7675  * Returns whether the icon is activatable.
7676  *
7677  * Returns: %TRUE if the icon is activatable.
7678  *
7679  * Since: 2.16
7680  */
7681 gboolean
7682 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7683                                 GtkEntryIconPosition  icon_pos)
7684 {
7685   GtkEntryPrivate *priv;
7686   EntryIconInfo *icon_info;
7687
7688   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7689   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7690
7691   priv = GTK_ENTRY_GET_PRIVATE (entry);
7692   icon_info = priv->icons[icon_pos];
7693
7694   return (icon_info != NULL && !icon_info->nonactivatable);
7695 }
7696
7697 /**
7698  * gtk_entry_get_icon_pixbuf:
7699  * @entry: A #GtkEntry
7700  * @icon_pos: Icon position
7701  *
7702  * Retrieves the image used for the icon.
7703  *
7704  * Unlike the other methods of setting and getting icon data, this
7705  * method will work regardless of whether the icon was set using a
7706  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7707  *
7708  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7709  *     set for this position.
7710  *
7711  * Since: 2.16
7712  */
7713 GdkPixbuf *
7714 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7715                            GtkEntryIconPosition  icon_pos)
7716 {
7717   GtkEntryPrivate *priv;
7718   EntryIconInfo *icon_info;
7719
7720   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7721   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7722
7723   priv = GTK_ENTRY_GET_PRIVATE (entry);
7724   icon_info = priv->icons[icon_pos];
7725
7726   if (!icon_info)
7727     return NULL;
7728
7729   gtk_entry_ensure_pixbuf (entry, icon_pos);
7730
7731   return icon_info->pixbuf;
7732 }
7733
7734 /**
7735  * gtk_entry_get_icon_gicon:
7736  * @entry: A #GtkEntry
7737  * @icon_pos: Icon position
7738  *
7739  * Retrieves the #GIcon used for the icon, or %NULL if there is
7740  * no icon or if the icon was set by some other method (e.g., by
7741  * stock, pixbuf, or icon name).
7742  *
7743  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
7744  *     or if the icon is not a #GIcon
7745  *
7746  * Since: 2.16
7747  */
7748 GIcon *
7749 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7750                           GtkEntryIconPosition  icon_pos)
7751 {
7752   GtkEntryPrivate *priv;
7753   EntryIconInfo *icon_info;
7754
7755   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7756   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7757
7758   priv = GTK_ENTRY_GET_PRIVATE (entry);
7759   icon_info = priv->icons[icon_pos];
7760
7761   if (!icon_info)
7762     return NULL;
7763
7764   return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
7765 }
7766
7767 /**
7768  * gtk_entry_get_icon_stock:
7769  * @entry: A #GtkEntry
7770  * @icon_pos: Icon position
7771  *
7772  * Retrieves the stock id used for the icon, or %NULL if there is
7773  * no icon or if the icon was set by some other method (e.g., by
7774  * pixbuf, icon name or gicon).
7775  *
7776  * Returns: A stock id, or %NULL if no icon is set or if the icon
7777  *          wasn't set from a stock id
7778  *
7779  * Since: 2.16
7780  */
7781 const gchar *
7782 gtk_entry_get_icon_stock (GtkEntry             *entry,
7783                           GtkEntryIconPosition  icon_pos)
7784 {
7785   GtkEntryPrivate *priv;
7786   EntryIconInfo *icon_info;
7787
7788   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7789   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7790
7791   priv = GTK_ENTRY_GET_PRIVATE (entry);
7792   icon_info = priv->icons[icon_pos];
7793
7794   if (!icon_info)
7795     return NULL;
7796
7797   return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
7798 }
7799
7800 /**
7801  * gtk_entry_get_icon_name:
7802  * @entry: A #GtkEntry
7803  * @icon_pos: Icon position
7804  *
7805  * Retrieves the icon name used for the icon, or %NULL if there is
7806  * no icon or if the icon was set by some other method (e.g., by
7807  * pixbuf, stock or gicon).
7808  *
7809  * Returns: An icon name, or %NULL if no icon is set or if the icon
7810  *          wasn't set from an icon name
7811  *
7812  * Since: 2.16
7813  */
7814 const gchar *
7815 gtk_entry_get_icon_name (GtkEntry             *entry,
7816                          GtkEntryIconPosition  icon_pos)
7817 {
7818   GtkEntryPrivate *priv;
7819   EntryIconInfo *icon_info;
7820
7821   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7822   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7823
7824   priv = GTK_ENTRY_GET_PRIVATE (entry);
7825   icon_info = priv->icons[icon_pos];
7826
7827   if (!icon_info)
7828     return NULL;
7829
7830   return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
7831 }
7832
7833 /**
7834  * gtk_entry_set_icon_sensitive:
7835  * @entry: A #GtkEntry
7836  * @icon_pos: Icon position
7837  * @sensitive: Specifies whether the icon should appear
7838  *             sensitive or insensitive
7839  *
7840  * Sets the sensitivity for the specified icon.
7841  *
7842  * Since: 2.16
7843  */
7844 void
7845 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
7846                               GtkEntryIconPosition  icon_pos,
7847                               gboolean              sensitive)
7848 {
7849   GtkEntryPrivate *priv;
7850   EntryIconInfo *icon_info;
7851
7852   g_return_if_fail (GTK_IS_ENTRY (entry));
7853   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7854
7855   priv = GTK_ENTRY_GET_PRIVATE (entry);
7856
7857   if ((icon_info = priv->icons[icon_pos]) == NULL)
7858     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7859
7860   if (icon_info->insensitive != !sensitive)
7861     {
7862       icon_info->insensitive = !sensitive;
7863
7864       icon_info->pressed = FALSE;
7865       icon_info->prelight = FALSE;
7866
7867       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7868         update_cursors (GTK_WIDGET (entry));
7869
7870       gtk_widget_queue_draw (GTK_WIDGET (entry));
7871
7872       g_object_notify (G_OBJECT (entry),
7873                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
7874     }
7875 }
7876
7877 /**
7878  * gtk_entry_get_icon_sensitive:
7879  * @entry: a #GtkEntry
7880  * @icon_pos: Icon position
7881  *
7882  * Returns whether the icon appears sensitive or insensitive.
7883  *
7884  * Returns: %TRUE if the icon is sensitive.
7885  *
7886  * Since: 2.16
7887  */
7888 gboolean
7889 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
7890                               GtkEntryIconPosition  icon_pos)
7891 {
7892   GtkEntryPrivate *priv;
7893   EntryIconInfo *icon_info;
7894
7895   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
7896   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
7897
7898   priv = GTK_ENTRY_GET_PRIVATE (entry);
7899   icon_info = priv->icons[icon_pos];
7900
7901   return (!icon_info || !icon_info->insensitive);
7902
7903 }
7904
7905 /**
7906  * gtk_entry_get_icon_storage_type:
7907  * @entry: a #GtkEntry
7908  * @icon_pos: Icon position
7909  *
7910  * Gets the type of representation being used by the icon
7911  * to store image data. If the icon has no image data,
7912  * the return value will be %GTK_IMAGE_EMPTY.
7913  *
7914  * Return value: image representation being used
7915  *
7916  * Since: 2.16
7917  **/
7918 GtkImageType
7919 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
7920                                  GtkEntryIconPosition  icon_pos)
7921 {
7922   GtkEntryPrivate *priv;
7923   EntryIconInfo *icon_info;
7924
7925   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
7926   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
7927
7928   priv = GTK_ENTRY_GET_PRIVATE (entry);
7929   icon_info = priv->icons[icon_pos];
7930
7931   if (!icon_info)
7932     return GTK_IMAGE_EMPTY;
7933
7934   return icon_info->storage_type;
7935 }
7936
7937 /**
7938  * gtk_entry_get_icon_at_pos:
7939  * @entry: a #GtkEntry
7940  * @x: the x coordinate of the position to find
7941  * @y: the y coordinate of the position to find
7942  *
7943  * Finds the icon at the given position and return its index.
7944  * If @x, @y doesn't lie inside an icon, -1 is returned.
7945  * This function is intended for use in a #GtkWidget::query-tooltip
7946  * signal handler.
7947  *
7948  * Returns: the index of the icon at the given position, or -1
7949  *
7950  * Since: 2.16
7951  */
7952 gint
7953 gtk_entry_get_icon_at_pos (GtkEntry *entry,
7954                            gint      x,
7955                            gint      y)
7956 {
7957   GtkAllocation primary;
7958   GtkAllocation secondary;
7959   gint frame_x, frame_y;
7960
7961   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
7962
7963   get_frame_size (entry, &frame_x, &frame_y, NULL, NULL);
7964   x -= frame_x;
7965   y -= frame_y;
7966
7967   get_icon_allocations (entry, &primary, &secondary);
7968
7969   if (primary.x <= x && x < primary.x + primary.width &&
7970       primary.y <= y && y < primary.y + primary.height)
7971     return GTK_ENTRY_ICON_PRIMARY;
7972
7973   if (secondary.x <= x && x < secondary.x + secondary.width &&
7974       secondary.y <= y && y < secondary.y + secondary.height)
7975     return GTK_ENTRY_ICON_SECONDARY;
7976
7977   return -1;
7978 }
7979
7980 /**
7981  * gtk_entry_set_icon_drag_source:
7982  * @entry: a #GtkIconEntry
7983  * @icon_pos: icon position
7984  * @target_list: the targets (data formats) in which the data can be provided
7985  * @actions: a bitmask of the allowed drag actions
7986  *
7987  * Sets up the icon at the given position so that GTK+ will start a drag
7988  * operation when the user clicks and drags the icon.
7989  *
7990  * To handle the drag operation, you need to connect to the usual
7991  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
7992  * signal, and use gtk_entry_get_current_icon_drag_source() in
7993  * your signal handler to find out if the drag was started from
7994  * an icon.
7995  *
7996  * By default, GTK+ uses the icon as the drag icon. You can use the 
7997  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
7998  * have to use g_signal_connect_after() to ensure that your signal handler
7999  * gets executed after the default handler.
8000  *
8001  * Since: 2.16
8002  */
8003 void
8004 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8005                                 GtkEntryIconPosition  icon_pos,
8006                                 GtkTargetList        *target_list,
8007                                 GdkDragAction         actions)
8008 {
8009   GtkEntryPrivate *priv;
8010   EntryIconInfo *icon_info;
8011
8012   g_return_if_fail (GTK_IS_ENTRY (entry));
8013   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8014
8015   priv = GTK_ENTRY_GET_PRIVATE (entry);
8016
8017   if ((icon_info = priv->icons[icon_pos]) == NULL)
8018     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8019
8020   if (icon_info->target_list)
8021     gtk_target_list_unref (icon_info->target_list);
8022   icon_info->target_list = target_list;
8023   if (icon_info->target_list)
8024     gtk_target_list_ref (icon_info->target_list);
8025
8026   icon_info->actions = actions;
8027 }
8028
8029 /**
8030  * gtk_entry_get_current_icon_drag_source:
8031  * @entry: a #GtkIconEntry
8032  *
8033  * Returns the index of the icon which is the source of the current
8034  * DND operation, or -1.
8035  *
8036  * This function is meant to be used in a #GtkWidget::drag-data-get
8037  * callback.
8038  *
8039  * Returns: index of the icon which is the source of the current
8040  *          DND operation, or -1.
8041  *
8042  * Since: 2.16
8043  */
8044 gint
8045 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8046 {
8047   GtkEntryPrivate *priv;
8048   EntryIconInfo *icon_info = NULL;
8049   gint i;
8050
8051   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8052
8053   priv = GTK_ENTRY_GET_PRIVATE (entry);
8054
8055   for (i = 0; i < MAX_ICONS; i++)
8056     {
8057       if ((icon_info = priv->icons[i]))
8058         {
8059           if (icon_info->in_drag)
8060             return i;
8061         }
8062     }
8063
8064   return -1;
8065 }
8066
8067 static void
8068 ensure_has_tooltip (GtkEntry *entry)
8069 {
8070   GtkEntryPrivate *priv;
8071   EntryIconInfo *icon_info;
8072   int i;
8073   gboolean has_tooltip = FALSE;
8074
8075   priv = GTK_ENTRY_GET_PRIVATE (entry);
8076
8077   for (i = 0; i < MAX_ICONS; i++)
8078     {
8079       if ((icon_info = priv->icons[i]) != NULL)
8080         {
8081           if (icon_info->tooltip != NULL)
8082             {
8083               has_tooltip = TRUE;
8084               break;
8085             }
8086         }
8087     }
8088
8089   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8090 }
8091
8092 /**
8093  * gtk_entry_get_icon_tooltip_text:
8094  * @entry: a #GtkEntry
8095  * @icon_pos: the icon position
8096  *
8097  * Gets the contents of the tooltip on the icon at the specified 
8098  * position in @entry.
8099  * 
8100  * Returns: the tooltip text, or %NULL. Free the returned string
8101  *     with g_free() when done.
8102  * 
8103  * Since: 2.16
8104  */
8105 gchar *
8106 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8107                                  GtkEntryIconPosition  icon_pos)
8108 {
8109   GtkEntryPrivate *priv;
8110   EntryIconInfo *icon_info;
8111   gchar *text = NULL;
8112
8113   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8114   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8115
8116   priv = GTK_ENTRY_GET_PRIVATE (entry);
8117   icon_info = priv->icons[icon_pos];
8118
8119   if (!icon_info)
8120     return NULL;
8121  
8122   if (icon_info->tooltip && 
8123       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8124     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8125
8126   return text;
8127 }
8128
8129 /**
8130  * gtk_entry_set_icon_tooltip_text:
8131  * @entry: a #GtkEntry
8132  * @icon_pos: the icon position
8133  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8134  *
8135  * Sets @tooltip as the contents of the tooltip for the icon
8136  * at the specified position.
8137  *
8138  * Use %NULL for @tooltip to remove an existing tooltip.
8139  *
8140  * See also gtk_widget_set_tooltip_text() and 
8141  * gtk_entry_set_icon_tooltip_markup().
8142  *
8143  * Since: 2.16
8144  */
8145 void
8146 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8147                                  GtkEntryIconPosition  icon_pos,
8148                                  const gchar          *tooltip)
8149 {
8150   GtkEntryPrivate *priv;
8151   EntryIconInfo *icon_info;
8152
8153   g_return_if_fail (GTK_IS_ENTRY (entry));
8154   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8155
8156   priv = GTK_ENTRY_GET_PRIVATE (entry);
8157
8158   if (!(icon_info = priv->icons[icon_pos]))
8159     icon_info = priv->icons[icon_pos];
8160
8161   if (icon_info->tooltip)
8162     g_free (icon_info->tooltip);
8163
8164   /* Treat an empty string as a NULL string,
8165    * because an empty string would be useless for a tooltip:
8166    */
8167   if (tooltip && tooltip[0] == '\0')
8168     tooltip = NULL;
8169
8170   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8171
8172   ensure_has_tooltip (entry);
8173 }
8174
8175 /**
8176  * gtk_entry_get_icon_tooltip_markup:
8177  * @entry: a #GtkEntry
8178  * @icon_pos: the icon position
8179  *
8180  * Gets the contents of the tooltip on the icon at the specified 
8181  * position in @entry.
8182  * 
8183  * Returns: the tooltip text, or %NULL. Free the returned string
8184  *     with g_free() when done.
8185  * 
8186  * Since: 2.16
8187  */
8188 gchar *
8189 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8190                                    GtkEntryIconPosition  icon_pos)
8191 {
8192   GtkEntryPrivate *priv;
8193   EntryIconInfo *icon_info;
8194
8195   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8196   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8197
8198   priv = GTK_ENTRY_GET_PRIVATE (entry);
8199   icon_info = priv->icons[icon_pos];
8200
8201   if (!icon_info)
8202     return NULL;
8203  
8204   return g_strdup (icon_info->tooltip);
8205 }
8206
8207 /**
8208  * gtk_entry_set_icon_tooltip_markup:
8209  * @entry: a #GtkEntry
8210  * @icon_pos: the icon position
8211  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8212  *
8213  * Sets @tooltip as the contents of the tooltip for the icon at
8214  * the specified position. @tooltip is assumed to be marked up with
8215  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8216  *
8217  * Use %NULL for @tooltip to remove an existing tooltip.
8218  *
8219  * See also gtk_widget_set_tooltip_markup() and 
8220  * gtk_enty_set_icon_tooltip_text().
8221  *
8222  * Since: 2.16
8223  */
8224 void
8225 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8226                                    GtkEntryIconPosition  icon_pos,
8227                                    const gchar          *tooltip)
8228 {
8229   GtkEntryPrivate *priv;
8230   EntryIconInfo *icon_info;
8231
8232   g_return_if_fail (GTK_IS_ENTRY (entry));
8233   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8234
8235   priv = GTK_ENTRY_GET_PRIVATE (entry);
8236
8237   if (!(icon_info = priv->icons[icon_pos]))
8238     icon_info = priv->icons[icon_pos];
8239
8240   if (icon_info->tooltip)
8241     g_free (icon_info->tooltip);
8242
8243   /* Treat an empty string as a NULL string,
8244    * because an empty string would be useless for a tooltip:
8245    */
8246   if (tooltip && tooltip[0] == '\0')
8247     tooltip = NULL;
8248
8249   icon_info->tooltip = g_strdup (tooltip);
8250
8251   ensure_has_tooltip (entry);
8252 }
8253
8254 static gboolean
8255 gtk_entry_query_tooltip (GtkWidget  *widget,
8256                          gint        x,
8257                          gint        y,
8258                          gboolean    keyboard_tip,
8259                          GtkTooltip *tooltip)
8260 {
8261   GtkEntry *entry;
8262   GtkEntryPrivate *priv;
8263   EntryIconInfo *icon_info;
8264   gint icon_pos;
8265
8266   entry = GTK_ENTRY (widget);
8267   priv = GTK_ENTRY_GET_PRIVATE (entry);
8268
8269   if (!keyboard_tip)
8270     {
8271       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8272       if (icon_pos != -1)
8273         {
8274           if ((icon_info = priv->icons[icon_pos]) != NULL)
8275             {
8276               if (icon_info->tooltip)
8277                 {
8278                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8279                   return TRUE;
8280                 }
8281
8282               return FALSE;
8283             }
8284         }
8285     }
8286
8287   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8288                                                                    x, y,
8289                                                                    keyboard_tip,
8290                                                                    tooltip);
8291 }
8292
8293
8294 /* Quick hack of a popup menu
8295  */
8296 static void
8297 activate_cb (GtkWidget *menuitem,
8298              GtkEntry  *entry)
8299 {
8300   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8301   g_signal_emit_by_name (entry, signal);
8302 }
8303
8304
8305 static gboolean
8306 gtk_entry_mnemonic_activate (GtkWidget *widget,
8307                              gboolean   group_cycling)
8308 {
8309   gtk_widget_grab_focus (widget);
8310   return TRUE;
8311 }
8312
8313 static void
8314 append_action_signal (GtkEntry     *entry,
8315                       GtkWidget    *menu,
8316                       const gchar  *stock_id,
8317                       const gchar  *signal,
8318                       gboolean      sensitive)
8319 {
8320   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8321
8322   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8323   g_signal_connect (menuitem, "activate",
8324                     G_CALLBACK (activate_cb), entry);
8325
8326   gtk_widget_set_sensitive (menuitem, sensitive);
8327   
8328   gtk_widget_show (menuitem);
8329   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8330 }
8331         
8332 static void
8333 popup_menu_detach (GtkWidget *attach_widget,
8334                    GtkMenu   *menu)
8335 {
8336   GTK_ENTRY (attach_widget)->popup_menu = NULL;
8337 }
8338
8339 static void
8340 popup_position_func (GtkMenu   *menu,
8341                      gint      *x,
8342                      gint      *y,
8343                      gboolean  *push_in,
8344                      gpointer   user_data)
8345 {
8346   GtkEntry *entry = GTK_ENTRY (user_data);
8347   GtkWidget *widget = GTK_WIDGET (entry);
8348   GdkScreen *screen;
8349   GtkRequisition menu_req;
8350   GdkRectangle monitor;
8351   GtkBorder inner_border;
8352   gint monitor_num, strong_x, height;
8353  
8354   g_return_if_fail (gtk_widget_get_realized (widget));
8355
8356   gdk_window_get_origin (entry->text_area, x, y);
8357
8358   screen = gtk_widget_get_screen (widget);
8359   monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
8360   if (monitor_num < 0)
8361     monitor_num = 0;
8362   gtk_menu_set_monitor (menu, monitor_num);
8363
8364   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8365   gtk_widget_get_preferred_size (entry->popup_menu,
8366                                  &menu_req, NULL);
8367   height = gdk_window_get_height (entry->text_area);
8368   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8369   _gtk_entry_effective_inner_border (entry, &inner_border);
8370
8371   *x += inner_border.left + strong_x - entry->scroll_offset;
8372   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8373     *x -= menu_req.width;
8374
8375   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8376     *y += height;
8377   else if ((*y - menu_req.height) >= monitor.y)
8378     *y -= menu_req.height;
8379   else if (monitor.y + monitor.height - (*y + height) > *y)
8380     *y += height;
8381   else
8382     *y -= menu_req.height;
8383
8384   *push_in = FALSE;
8385 }
8386
8387 static void
8388 unichar_chosen_func (const char *text,
8389                      gpointer    data)
8390 {
8391   GtkEntry *entry = GTK_ENTRY (data);
8392
8393   if (entry->editable)
8394     gtk_entry_enter_text (entry, text);
8395 }
8396
8397 typedef struct
8398 {
8399   GtkEntry *entry;
8400   gint button;
8401   guint time;
8402 } PopupInfo;
8403
8404 static void
8405 popup_targets_received (GtkClipboard     *clipboard,
8406                         GtkSelectionData *data,
8407                         gpointer          user_data)
8408 {
8409   PopupInfo *info = user_data;
8410   GtkEntry *entry = info->entry;
8411   
8412   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8413     {
8414       DisplayMode mode;
8415       gboolean clipboard_contains_text;
8416       GtkWidget *menuitem;
8417       GtkWidget *submenu;
8418       gboolean show_input_method_menu;
8419       gboolean show_unicode_menu;
8420       
8421       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8422       if (entry->popup_menu)
8423         gtk_widget_destroy (entry->popup_menu);
8424       
8425       entry->popup_menu = gtk_menu_new ();
8426       
8427       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
8428                                  GTK_WIDGET (entry),
8429                                  popup_menu_detach);
8430       
8431       mode = gtk_entry_get_display_mode (entry);
8432       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8433                             entry->editable && mode == DISPLAY_NORMAL &&
8434                             entry->current_pos != entry->selection_bound);
8435
8436       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8437                             mode == DISPLAY_NORMAL &&
8438                             entry->current_pos != entry->selection_bound);
8439
8440       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8441                             entry->editable && clipboard_contains_text);
8442
8443       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8444       gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
8445       g_signal_connect_swapped (menuitem, "activate",
8446                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8447       gtk_widget_show (menuitem);
8448       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8449
8450       menuitem = gtk_separator_menu_item_new ();
8451       gtk_widget_show (menuitem);
8452       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8453       
8454       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8455       g_signal_connect_swapped (menuitem, "activate",
8456                                 G_CALLBACK (gtk_entry_select_all), entry);
8457       gtk_widget_show (menuitem);
8458       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8459       
8460       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8461                     "gtk-show-input-method-menu", &show_input_method_menu,
8462                     "gtk-show-unicode-menu", &show_unicode_menu,
8463                     NULL);
8464
8465       if (show_input_method_menu || show_unicode_menu)
8466         {
8467           menuitem = gtk_separator_menu_item_new ();
8468           gtk_widget_show (menuitem);
8469           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8470         }
8471       
8472       if (show_input_method_menu)
8473         {
8474           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8475           gtk_widget_set_sensitive (menuitem, entry->editable);      
8476           gtk_widget_show (menuitem);
8477           submenu = gtk_menu_new ();
8478           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8479           
8480           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8481       
8482           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
8483                                                 GTK_MENU_SHELL (submenu));
8484         }
8485       
8486       if (show_unicode_menu)
8487         {
8488           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8489           gtk_widget_set_sensitive (menuitem, entry->editable);      
8490           gtk_widget_show (menuitem);
8491           
8492           submenu = gtk_menu_new ();
8493           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8494           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
8495           
8496           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8497                                                         unichar_chosen_func,
8498                                                         entry);
8499         }
8500       
8501       g_signal_emit (entry,
8502                      signals[POPULATE_POPUP],
8503                      0,
8504                      entry->popup_menu);
8505   
8506
8507       if (info->button)
8508         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8509                         NULL, NULL,
8510                         info->button, info->time);
8511       else
8512         {
8513           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8514                           popup_position_func, entry,
8515                           info->button, info->time);
8516           gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
8517         }
8518     }
8519
8520   g_object_unref (entry);
8521   g_slice_free (PopupInfo, info);
8522 }
8523                         
8524 static void
8525 gtk_entry_do_popup (GtkEntry       *entry,
8526                     GdkEventButton *event)
8527 {
8528   PopupInfo *info = g_slice_new (PopupInfo);
8529
8530   /* In order to know what entries we should make sensitive, we
8531    * ask for the current targets of the clipboard, and when
8532    * we get them, then we actually pop up the menu.
8533    */
8534   info->entry = g_object_ref (entry);
8535   
8536   if (event)
8537     {
8538       info->button = event->button;
8539       info->time = event->time;
8540     }
8541   else
8542     {
8543       info->button = 0;
8544       info->time = gtk_get_current_event_time ();
8545     }
8546
8547   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8548                                   gdk_atom_intern_static_string ("TARGETS"),
8549                                   popup_targets_received,
8550                                   info);
8551 }
8552
8553 static gboolean
8554 gtk_entry_popup_menu (GtkWidget *widget)
8555 {
8556   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8557   return TRUE;
8558 }
8559
8560 static void
8561 gtk_entry_drag_begin (GtkWidget      *widget,
8562                       GdkDragContext *context)
8563 {
8564   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8565   gint i;
8566
8567   for (i = 0; i < MAX_ICONS; i++)
8568     {
8569       EntryIconInfo *icon_info = priv->icons[i];
8570       
8571       if (icon_info != NULL)
8572         {
8573           if (icon_info->in_drag) 
8574             {
8575               switch (icon_info->storage_type)
8576                 {
8577                 case GTK_IMAGE_STOCK:
8578                   gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
8579                   break;
8580
8581                 case GTK_IMAGE_ICON_NAME:
8582                   gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
8583                   break;
8584
8585                   /* FIXME: No GIcon support for dnd icons */
8586                 case GTK_IMAGE_GICON:
8587                 case GTK_IMAGE_PIXBUF:
8588                   gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
8589                   break;
8590                 default:
8591                   g_assert_not_reached ();
8592                 }
8593             }
8594         }
8595     }
8596 }
8597
8598 static void
8599 gtk_entry_drag_end (GtkWidget      *widget,
8600                     GdkDragContext *context)
8601 {
8602   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8603   gint i;
8604
8605   for (i = 0; i < MAX_ICONS; i++)
8606     {
8607       EntryIconInfo *icon_info = priv->icons[i];
8608
8609       if (icon_info != NULL)
8610         icon_info->in_drag = 0;
8611     }
8612 }
8613
8614 static void
8615 gtk_entry_drag_leave (GtkWidget        *widget,
8616                       GdkDragContext   *context,
8617                       guint             time)
8618 {
8619   GtkEntry *entry = GTK_ENTRY (widget);
8620
8621   entry->dnd_position = -1;
8622   gtk_widget_queue_draw (widget);
8623 }
8624
8625 static gboolean
8626 gtk_entry_drag_drop  (GtkWidget        *widget,
8627                       GdkDragContext   *context,
8628                       gint              x,
8629                       gint              y,
8630                       guint             time)
8631 {
8632   GtkEntry *entry = GTK_ENTRY (widget);
8633   GdkAtom target = GDK_NONE;
8634   
8635   if (entry->editable)
8636     target = gtk_drag_dest_find_target (widget, context, NULL);
8637
8638   if (target != GDK_NONE)
8639     gtk_drag_get_data (widget, context, target, time);
8640   else
8641     gtk_drag_finish (context, FALSE, FALSE, time);
8642   
8643   return TRUE;
8644 }
8645
8646 static gboolean
8647 gtk_entry_drag_motion (GtkWidget        *widget,
8648                        GdkDragContext   *context,
8649                        gint              x,
8650                        gint              y,
8651                        guint             time)
8652 {
8653   GtkEntry *entry = GTK_ENTRY (widget);
8654   GtkStyle *style;
8655   GtkWidget *source_widget;
8656   GdkDragAction suggested_action;
8657   gint new_position, old_position;
8658   gint sel1, sel2;
8659
8660   style = gtk_widget_get_style (widget);
8661   x -= style->xthickness;
8662   y -= style->ythickness;
8663
8664   old_position = entry->dnd_position;
8665   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8666
8667   if (entry->editable &&
8668       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8669     {
8670       source_widget = gtk_drag_get_source_widget (context);
8671       suggested_action = context->suggested_action;
8672
8673       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8674           new_position < sel1 || new_position > sel2)
8675         {
8676           if (source_widget == widget)
8677             {
8678               /* Default to MOVE, unless the user has
8679                * pressed ctrl or alt to affect available actions
8680                */
8681               if ((context->actions & GDK_ACTION_MOVE) != 0)
8682                 suggested_action = GDK_ACTION_MOVE;
8683             }
8684               
8685           entry->dnd_position = new_position;
8686         }
8687       else
8688         {
8689           if (source_widget == widget)
8690             suggested_action = 0;       /* Can't drop in selection where drag started */
8691           
8692           entry->dnd_position = -1;
8693         }
8694     }
8695   else
8696     {
8697       /* Entry not editable, or no text */
8698       suggested_action = 0;
8699       entry->dnd_position = -1;
8700     }
8701   
8702   gdk_drag_status (context, suggested_action, time);
8703   
8704   if (entry->dnd_position != old_position)
8705     gtk_widget_queue_draw (widget);
8706
8707   return TRUE;
8708 }
8709
8710 static void
8711 gtk_entry_drag_data_received (GtkWidget        *widget,
8712                               GdkDragContext   *context,
8713                               gint              x,
8714                               gint              y,
8715                               GtkSelectionData *selection_data,
8716                               guint             info,
8717                               guint             time)
8718 {
8719   GtkEntry *entry = GTK_ENTRY (widget);
8720   GtkEditable *editable = GTK_EDITABLE (widget);
8721   GtkStyle *style;
8722   gchar *str;
8723
8724   str = (gchar *) gtk_selection_data_get_text (selection_data);
8725
8726   style = gtk_widget_get_style (widget);
8727   x -= style->xthickness;
8728   y -= style->ythickness;
8729
8730   if (str && entry->editable)
8731     {
8732       gint new_position;
8733       gint sel1, sel2;
8734       gint length = -1;
8735
8736       if (entry->truncate_multiline)
8737         length = truncate_multiline (str);
8738
8739       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8740
8741       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8742           new_position < sel1 || new_position > sel2)
8743         {
8744           gtk_editable_insert_text (editable, str, length, &new_position);
8745         }
8746       else
8747         {
8748           /* Replacing selection */
8749           begin_change (entry);
8750           g_object_freeze_notify (G_OBJECT (entry));
8751           gtk_editable_delete_text (editable, sel1, sel2);
8752           gtk_editable_insert_text (editable, str, length, &sel1);
8753           g_object_thaw_notify (G_OBJECT (entry));
8754           end_change (entry);
8755         }
8756       
8757       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
8758     }
8759   else
8760     {
8761       /* Drag and drop didn't happen! */
8762       gtk_drag_finish (context, FALSE, FALSE, time);
8763     }
8764
8765   g_free (str);
8766 }
8767
8768 static void
8769 gtk_entry_drag_data_get (GtkWidget        *widget,
8770                          GdkDragContext   *context,
8771                          GtkSelectionData *selection_data,
8772                          guint             info,
8773                          guint             time)
8774 {
8775   gint sel_start, sel_end;
8776   gint i;
8777
8778   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8779   GtkEditable *editable = GTK_EDITABLE (widget);
8780
8781   /* If there is an icon drag going on, exit early. */
8782   for (i = 0; i < MAX_ICONS; i++)
8783     {
8784       EntryIconInfo *icon_info = priv->icons[i];
8785
8786       if (icon_info != NULL)
8787         {
8788           if (icon_info->in_drag)
8789             return;
8790         }
8791     }
8792
8793   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8794     {
8795       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
8796
8797       gtk_selection_data_set_text (selection_data, str, -1);
8798       
8799       g_free (str);
8800     }
8801
8802 }
8803
8804 static void
8805 gtk_entry_drag_data_delete (GtkWidget      *widget,
8806                             GdkDragContext *context)
8807 {
8808   gint sel_start, sel_end;
8809   gint i;
8810
8811   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8812   GtkEditable *editable = GTK_EDITABLE (widget);
8813
8814   /* If there is an icon drag going on, exit early. */
8815   for (i = 0; i < MAX_ICONS; i++)
8816     {
8817       EntryIconInfo *icon_info = priv->icons[i];
8818
8819       if (icon_info != NULL)
8820         {
8821           if (icon_info->in_drag)
8822             return;
8823         }
8824     }
8825   
8826   if (GTK_ENTRY (widget)->editable &&
8827       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8828     gtk_editable_delete_text (editable, sel_start, sel_end);
8829 }
8830
8831 /* We display the cursor when
8832  *
8833  *  - the selection is empty, AND
8834  *  - the widget has focus
8835  */
8836
8837 #define CURSOR_ON_MULTIPLIER 2
8838 #define CURSOR_OFF_MULTIPLIER 1
8839 #define CURSOR_PEND_MULTIPLIER 3
8840 #define CURSOR_DIVIDER 3
8841
8842 static gboolean
8843 cursor_blinks (GtkEntry *entry)
8844 {
8845   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
8846       entry->editable &&
8847       entry->selection_bound == entry->current_pos)
8848     {
8849       GtkSettings *settings;
8850       gboolean blink;
8851
8852       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8853       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
8854
8855       return blink;
8856     }
8857   else
8858     return FALSE;
8859 }
8860
8861 static gint
8862 get_cursor_time (GtkEntry *entry)
8863 {
8864   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8865   gint time;
8866
8867   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
8868
8869   return time;
8870 }
8871
8872 static gint
8873 get_cursor_blink_timeout (GtkEntry *entry)
8874 {
8875   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8876   gint timeout;
8877
8878   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
8879
8880   return timeout;
8881 }
8882
8883 static void
8884 show_cursor (GtkEntry *entry)
8885 {
8886   GtkWidget *widget;
8887
8888   if (!entry->cursor_visible)
8889     {
8890       entry->cursor_visible = TRUE;
8891
8892       widget = GTK_WIDGET (entry);
8893       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
8894         gtk_widget_queue_draw (widget);
8895     }
8896 }
8897
8898 static void
8899 hide_cursor (GtkEntry *entry)
8900 {
8901   GtkWidget *widget;
8902
8903   if (entry->cursor_visible)
8904     {
8905       entry->cursor_visible = FALSE;
8906
8907       widget = GTK_WIDGET (entry);
8908       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
8909         gtk_widget_queue_draw (widget);
8910     }
8911 }
8912
8913 /*
8914  * Blink!
8915  */
8916 static gint
8917 blink_cb (gpointer data)
8918 {
8919   GtkEntry *entry;
8920   GtkEntryPrivate *priv; 
8921   gint blink_timeout;
8922
8923   entry = GTK_ENTRY (data);
8924   priv = GTK_ENTRY_GET_PRIVATE (entry);
8925  
8926   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
8927     {
8928       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
8929                  "connect a handler to this signal, it must return\n"
8930                  "FALSE so the entry gets the event as well");
8931
8932       gtk_entry_check_cursor_blink (entry);
8933
8934       return FALSE;
8935     }
8936   
8937   g_assert (entry->selection_bound == entry->current_pos);
8938   
8939   blink_timeout = get_cursor_blink_timeout (entry);
8940   if (priv->blink_time > 1000 * blink_timeout && 
8941       blink_timeout < G_MAXINT/1000) 
8942     {
8943       /* we've blinked enough without the user doing anything, stop blinking */
8944       show_cursor (entry);
8945       entry->blink_timeout = 0;
8946     } 
8947   else if (entry->cursor_visible)
8948     {
8949       hide_cursor (entry);
8950       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
8951                                             blink_cb,
8952                                             entry);
8953     }
8954   else
8955     {
8956       show_cursor (entry);
8957       priv->blink_time += get_cursor_time (entry);
8958       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
8959                                             blink_cb,
8960                                             entry);
8961     }
8962
8963   /* Remove ourselves */
8964   return FALSE;
8965 }
8966
8967 static void
8968 gtk_entry_check_cursor_blink (GtkEntry *entry)
8969 {
8970   GtkEntryPrivate *priv; 
8971   
8972   priv = GTK_ENTRY_GET_PRIVATE (entry);
8973
8974   if (cursor_blinks (entry))
8975     {
8976       if (!entry->blink_timeout)
8977         {
8978           show_cursor (entry);
8979           entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
8980                                                 blink_cb,
8981                                                 entry);
8982         }
8983     }
8984   else
8985     {
8986       if (entry->blink_timeout)  
8987         { 
8988           g_source_remove (entry->blink_timeout);
8989           entry->blink_timeout = 0;
8990         }
8991       
8992       entry->cursor_visible = TRUE;
8993     }
8994   
8995 }
8996
8997 static void
8998 gtk_entry_pend_cursor_blink (GtkEntry *entry)
8999 {
9000   if (cursor_blinks (entry))
9001     {
9002       if (entry->blink_timeout != 0)
9003         g_source_remove (entry->blink_timeout);
9004       
9005       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9006                                             blink_cb,
9007                                             entry);
9008       show_cursor (entry);
9009     }
9010 }
9011
9012 static void
9013 gtk_entry_reset_blink_time (GtkEntry *entry)
9014 {
9015   GtkEntryPrivate *priv; 
9016   
9017   priv = GTK_ENTRY_GET_PRIVATE (entry);
9018   
9019   priv->blink_time = 0;
9020 }
9021
9022
9023 /* completion */
9024 static gint
9025 gtk_entry_completion_timeout (gpointer data)
9026 {
9027   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9028   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (completion->priv->entry);
9029
9030   completion->priv->completion_timeout = 0;
9031
9032   if (completion->priv->filter_model &&
9033       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9034       >= completion->priv->minimum_key_length)
9035     {
9036       gint matches;
9037       gint actions;
9038       GtkTreeSelection *s;
9039       gboolean popup_single;
9040
9041       gtk_entry_completion_complete (completion);
9042       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9043
9044       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9045
9046       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9047
9048       gtk_tree_selection_unselect_all (s);
9049
9050       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9051
9052       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9053       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9054         { 
9055           if (gtk_widget_get_visible (completion->priv->popup_window))
9056             _gtk_entry_completion_resize_popup (completion);
9057           else
9058             _gtk_entry_completion_popup (completion, priv->completion_device);
9059         }
9060       else 
9061         _gtk_entry_completion_popdown (completion);
9062     }
9063   else if (gtk_widget_get_visible (completion->priv->popup_window))
9064     _gtk_entry_completion_popdown (completion);
9065
9066   return FALSE;
9067 }
9068
9069 static inline gboolean
9070 keyval_is_cursor_move (guint keyval)
9071 {
9072   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9073     return TRUE;
9074
9075   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9076     return TRUE;
9077
9078   if (keyval == GDK_KEY_Page_Up)
9079     return TRUE;
9080
9081   if (keyval == GDK_KEY_Page_Down)
9082     return TRUE;
9083
9084   return FALSE;
9085 }
9086
9087 static gboolean
9088 gtk_entry_completion_key_press (GtkWidget   *widget,
9089                                 GdkEventKey *event,
9090                                 gpointer     user_data)
9091 {
9092   gint matches, actions = 0;
9093   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9094
9095   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9096     return FALSE;
9097
9098   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9099
9100   if (completion->priv->actions)
9101     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9102
9103   if (keyval_is_cursor_move (event->keyval))
9104     {
9105       GtkTreePath *path = NULL;
9106       
9107       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9108         {
9109           if (completion->priv->current_selected < 0)
9110             completion->priv->current_selected = matches + actions - 1;
9111           else
9112             completion->priv->current_selected--;
9113         }
9114       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9115         {
9116           if (completion->priv->current_selected < matches + actions - 1)
9117             completion->priv->current_selected++;
9118           else
9119             completion->priv->current_selected = -1;
9120         }
9121       else if (event->keyval == GDK_KEY_Page_Up)
9122         {
9123           if (completion->priv->current_selected < 0)
9124             completion->priv->current_selected = matches + actions - 1;
9125           else if (completion->priv->current_selected == 0)
9126             completion->priv->current_selected = -1;
9127           else if (completion->priv->current_selected < matches) 
9128             {
9129               completion->priv->current_selected -= 14;
9130               if (completion->priv->current_selected < 0)
9131                 completion->priv->current_selected = 0;
9132             }
9133           else 
9134             {
9135               completion->priv->current_selected -= 14;
9136               if (completion->priv->current_selected < matches - 1)
9137                 completion->priv->current_selected = matches - 1;
9138             }
9139         }
9140       else if (event->keyval == GDK_KEY_Page_Down)
9141         {
9142           if (completion->priv->current_selected < 0)
9143             completion->priv->current_selected = 0;
9144           else if (completion->priv->current_selected < matches - 1)
9145             {
9146               completion->priv->current_selected += 14;
9147               if (completion->priv->current_selected > matches - 1)
9148                 completion->priv->current_selected = matches - 1;
9149             }
9150           else if (completion->priv->current_selected == matches + actions - 1)
9151             {
9152               completion->priv->current_selected = -1;
9153             }
9154           else
9155             {
9156               completion->priv->current_selected += 14;
9157               if (completion->priv->current_selected > matches + actions - 1)
9158                 completion->priv->current_selected = matches + actions - 1;
9159             }
9160         }
9161
9162       if (completion->priv->current_selected < 0)
9163         {
9164           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9165           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9166
9167           if (completion->priv->inline_selection &&
9168               completion->priv->completion_prefix)
9169             {
9170               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9171                                   completion->priv->completion_prefix);
9172               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9173             }
9174         }
9175       else if (completion->priv->current_selected < matches)
9176         {
9177           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9178
9179           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9180           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9181                                     path, NULL, FALSE);
9182
9183           if (completion->priv->inline_selection)
9184             {
9185
9186               GtkTreeIter iter;
9187               GtkTreeIter child_iter;
9188               GtkTreeModel *model = NULL;
9189               GtkTreeSelection *sel;
9190               gboolean entry_set;
9191
9192               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9193               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9194                 return FALSE;
9195
9196               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9197               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9198               
9199               if (completion->priv->completion_prefix == NULL)
9200                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9201
9202               g_signal_emit_by_name (completion, "cursor-on-match", model,
9203                                      &child_iter, &entry_set);
9204             }
9205         }
9206       else if (completion->priv->current_selected - matches >= 0)
9207         {
9208           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9209
9210           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9211           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9212                                     path, NULL, FALSE);
9213
9214           if (completion->priv->inline_selection &&
9215               completion->priv->completion_prefix)
9216             {
9217               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9218                                   completion->priv->completion_prefix);
9219               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9220             }
9221         }
9222
9223       gtk_tree_path_free (path);
9224
9225       return TRUE;
9226     }
9227   else if (event->keyval == GDK_KEY_Escape ||
9228            event->keyval == GDK_KEY_Left ||
9229            event->keyval == GDK_KEY_KP_Left ||
9230            event->keyval == GDK_KEY_Right ||
9231            event->keyval == GDK_KEY_KP_Right) 
9232     {
9233       gboolean retval = TRUE;
9234
9235       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9236       _gtk_entry_completion_popdown (completion);
9237
9238       if (completion->priv->current_selected < 0)
9239         {
9240           retval = FALSE;
9241           goto keypress_completion_out;
9242         }
9243       else if (completion->priv->inline_selection)
9244         {
9245           /* Escape rejects the tentative completion */
9246           if (event->keyval == GDK_KEY_Escape)
9247             {
9248               if (completion->priv->completion_prefix)
9249                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9250                                     completion->priv->completion_prefix);
9251               else 
9252                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9253             }
9254
9255           /* Move the cursor to the end for Right/Esc, to the
9256              beginning for Left */
9257           if (event->keyval == GDK_KEY_Right ||
9258               event->keyval == GDK_KEY_KP_Right ||
9259               event->keyval == GDK_KEY_Escape)
9260             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9261           else
9262             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9263         }
9264
9265 keypress_completion_out:
9266       if (completion->priv->inline_selection)
9267         {
9268           g_free (completion->priv->completion_prefix);
9269           completion->priv->completion_prefix = NULL;
9270         }
9271
9272       return retval;
9273     }
9274   else if (event->keyval == GDK_KEY_Tab || 
9275            event->keyval == GDK_KEY_KP_Tab ||
9276            event->keyval == GDK_KEY_ISO_Left_Tab) 
9277     {
9278       GtkDirectionType dir = event->keyval == GDK_KEY_ISO_Left_Tab ? 
9279         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
9280       
9281       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9282       _gtk_entry_completion_popdown (completion);
9283
9284       g_free (completion->priv->completion_prefix);
9285       completion->priv->completion_prefix = NULL;
9286
9287       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
9288
9289       return TRUE;
9290     }
9291   else if (event->keyval == GDK_KEY_ISO_Enter ||
9292            event->keyval == GDK_KEY_KP_Enter ||
9293            event->keyval == GDK_KEY_Return)
9294     {
9295       GtkTreeIter iter;
9296       GtkTreeModel *model = NULL;
9297       GtkTreeSelection *sel;
9298       gboolean retval = TRUE;
9299
9300       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9301       _gtk_entry_completion_popdown (completion);
9302
9303       if (completion->priv->current_selected < matches)
9304         {
9305           gboolean entry_set;
9306
9307           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9308           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9309             {
9310               g_signal_handler_block (widget, completion->priv->changed_id);
9311               g_signal_emit_by_name (completion, "match-selected",
9312                                      model, &iter, &entry_set);
9313               g_signal_handler_unblock (widget, completion->priv->changed_id);
9314
9315               if (!entry_set)
9316                 {
9317                   gchar *str = NULL;
9318
9319                   gtk_tree_model_get (model, &iter,
9320                                       completion->priv->text_column, &str,
9321                                       -1);
9322
9323                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9324
9325                   /* move the cursor to the end */
9326                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9327
9328                   g_free (str);
9329                 }
9330             }
9331           else
9332             retval = FALSE;
9333         }
9334       else if (completion->priv->current_selected - matches >= 0)
9335         {
9336           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9337           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9338             {
9339               GtkTreePath *path;
9340
9341               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9342               g_signal_emit_by_name (completion, "action-activated",
9343                                      gtk_tree_path_get_indices (path)[0]);
9344               gtk_tree_path_free (path);
9345             }
9346           else
9347             retval = FALSE;
9348         }
9349
9350       g_free (completion->priv->completion_prefix);
9351       completion->priv->completion_prefix = NULL;
9352
9353       return retval;
9354     }
9355
9356   return FALSE;
9357 }
9358
9359 static void
9360 gtk_entry_completion_changed (GtkWidget *entry,
9361                               gpointer   user_data)
9362 {
9363   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9364   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9365   GdkDevice *device;
9366
9367   /* (re)install completion timeout */
9368   if (completion->priv->completion_timeout)
9369     g_source_remove (completion->priv->completion_timeout);
9370
9371   if (!gtk_entry_get_text (GTK_ENTRY (entry)))
9372     return;
9373
9374   /* no need to normalize for this test */
9375   if (completion->priv->minimum_key_length > 0 &&
9376       strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
9377     {
9378       if (gtk_widget_get_visible (completion->priv->popup_window))
9379         _gtk_entry_completion_popdown (completion);
9380       return;
9381     }
9382
9383   device = gtk_get_current_event_device ();
9384
9385   if (device && device->source == GDK_SOURCE_KEYBOARD)
9386     device = gdk_device_get_associated_device (device);
9387
9388   if (device)
9389     priv->completion_device = device;
9390
9391   completion->priv->completion_timeout =
9392     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9393                    gtk_entry_completion_timeout,
9394                    completion);
9395 }
9396
9397 static gboolean
9398 check_completion_callback (GtkEntryCompletion *completion)
9399 {
9400   completion->priv->check_completion_idle = NULL;
9401   
9402   gtk_entry_completion_complete (completion);
9403   gtk_entry_completion_insert_prefix (completion);
9404
9405   return FALSE;
9406 }
9407
9408 static void
9409 clear_completion_callback (GtkEntry   *entry,
9410                            GParamSpec *pspec)
9411 {
9412   if (pspec->name == I_("cursor-position") ||
9413       pspec->name == I_("selection-bound"))
9414     {
9415       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9416       
9417       completion->priv->has_completion = FALSE;
9418     }
9419 }
9420
9421 static gboolean
9422 accept_completion_callback (GtkEntry *entry)
9423 {
9424   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9425
9426   if (completion->priv->has_completion)
9427     gtk_editable_set_position (GTK_EDITABLE (entry),
9428                                gtk_entry_buffer_get_length (get_buffer (entry)));
9429
9430   return FALSE;
9431 }
9432
9433 static void
9434 completion_insert_text_callback (GtkEntry           *entry,
9435                                  const gchar        *text,
9436                                  gint                length,
9437                                  gint                position,
9438                                  GtkEntryCompletion *completion)
9439 {
9440   /* idle to update the selection based on the file list */
9441   if (completion->priv->check_completion_idle == NULL)
9442     {
9443       completion->priv->check_completion_idle = g_idle_source_new ();
9444       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9445       g_source_set_closure (completion->priv->check_completion_idle,
9446                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9447                                                    G_OBJECT (completion)));
9448       g_source_attach (completion->priv->check_completion_idle, NULL);
9449     }
9450 }
9451
9452 static void
9453 completion_changed (GtkEntryCompletion *completion,
9454                     GParamSpec         *pspec,
9455                     gpointer            data)
9456 {
9457   GtkEntry *entry = GTK_ENTRY (data);
9458
9459   if (pspec->name == I_("popup-completion") ||
9460       pspec->name == I_("inline-completion"))
9461     {
9462       disconnect_completion_signals (entry, completion);
9463       connect_completion_signals (entry, completion);
9464     }
9465 }
9466
9467 static void
9468 disconnect_completion_signals (GtkEntry           *entry,
9469                                GtkEntryCompletion *completion)
9470 {
9471   g_signal_handlers_disconnect_by_func (completion, 
9472                                        G_CALLBACK (completion_changed), entry);
9473   if (completion->priv->changed_id > 0 &&
9474       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9475     {
9476       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9477       completion->priv->changed_id = 0;
9478     }
9479   g_signal_handlers_disconnect_by_func (entry, 
9480                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9481   if (completion->priv->insert_text_id > 0 &&
9482       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9483     {
9484       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9485       completion->priv->insert_text_id = 0;
9486     }
9487   g_signal_handlers_disconnect_by_func (entry, 
9488                                         G_CALLBACK (completion_insert_text_callback), completion);
9489   g_signal_handlers_disconnect_by_func (entry, 
9490                                         G_CALLBACK (clear_completion_callback), completion);
9491   g_signal_handlers_disconnect_by_func (entry, 
9492                                         G_CALLBACK (accept_completion_callback), completion);
9493 }
9494
9495 static void
9496 connect_completion_signals (GtkEntry           *entry,
9497                             GtkEntryCompletion *completion)
9498 {
9499   if (completion->priv->popup_completion)
9500     {
9501       completion->priv->changed_id =
9502         g_signal_connect (entry, "changed",
9503                           G_CALLBACK (gtk_entry_completion_changed), completion);
9504       g_signal_connect (entry, "key-press-event",
9505                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9506     }
9507  
9508   if (completion->priv->inline_completion)
9509     {
9510       completion->priv->insert_text_id =
9511         g_signal_connect (entry, "insert-text",
9512                           G_CALLBACK (completion_insert_text_callback), completion);
9513       g_signal_connect (entry, "notify",
9514                         G_CALLBACK (clear_completion_callback), completion);
9515       g_signal_connect (entry, "activate",
9516                         G_CALLBACK (accept_completion_callback), completion);
9517       g_signal_connect (entry, "focus-out-event",
9518                         G_CALLBACK (accept_completion_callback), completion);
9519     }
9520
9521   g_signal_connect (completion, "notify",
9522                     G_CALLBACK (completion_changed), entry);
9523 }
9524
9525 /**
9526  * gtk_entry_set_completion:
9527  * @entry: A #GtkEntry
9528  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9529  *
9530  * Sets @completion to be the auxiliary completion object to use with @entry.
9531  * All further configuration of the completion mechanism is done on
9532  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9533  * @completion is set to %NULL.
9534  *
9535  * Since: 2.4
9536  */
9537 void
9538 gtk_entry_set_completion (GtkEntry           *entry,
9539                           GtkEntryCompletion *completion)
9540 {
9541   GtkEntryCompletion *old;
9542
9543   g_return_if_fail (GTK_IS_ENTRY (entry));
9544   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9545
9546   old = gtk_entry_get_completion (entry);
9547
9548   if (old == completion)
9549     return;
9550   
9551   if (old)
9552     {
9553       if (old->priv->completion_timeout)
9554         {
9555           g_source_remove (old->priv->completion_timeout);
9556           old->priv->completion_timeout = 0;
9557         }
9558
9559       if (gtk_widget_get_mapped (old->priv->popup_window))
9560         _gtk_entry_completion_popdown (old);
9561
9562       disconnect_completion_signals (entry, old);
9563       old->priv->entry = NULL;
9564
9565       g_object_unref (old);
9566     }
9567
9568   if (!completion)
9569     {
9570       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9571       return;
9572     }
9573
9574   /* hook into the entry */
9575   g_object_ref (completion);
9576
9577   connect_completion_signals (entry, completion);    
9578   completion->priv->entry = GTK_WIDGET (entry);
9579   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9580 }
9581
9582 /**
9583  * gtk_entry_get_completion:
9584  * @entry: A #GtkEntry
9585  *
9586  * Returns the auxiliary completion object currently in use by @entry.
9587  *
9588  * Return value: (transfer none): The auxiliary completion object currently
9589  *     in use by @entry.
9590  *
9591  * Since: 2.4
9592  */
9593 GtkEntryCompletion *
9594 gtk_entry_get_completion (GtkEntry *entry)
9595 {
9596   GtkEntryCompletion *completion;
9597
9598   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9599
9600   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9601                                      GTK_ENTRY_COMPLETION_KEY));
9602
9603   return completion;
9604 }
9605
9606 /**
9607  * gtk_entry_set_cursor_hadjustment:
9608  * @entry: a #GtkEntry
9609  * @adjustment: an adjustment which should be adjusted when the cursor 
9610  *              is moved, or %NULL
9611  *
9612  * Hooks up an adjustment to the cursor position in an entry, so that when 
9613  * the cursor is moved, the adjustment is scrolled to show that position. 
9614  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9615  * the adjustment.
9616  *
9617  * The adjustment has to be in pixel units and in the same coordinate system 
9618  * as the entry. 
9619  * 
9620  * Since: 2.12
9621  */
9622 void
9623 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9624                                   GtkAdjustment *adjustment)
9625 {
9626   g_return_if_fail (GTK_IS_ENTRY (entry));
9627   if (adjustment)
9628     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9629
9630   if (adjustment)
9631     g_object_ref (adjustment);
9632
9633   g_object_set_qdata_full (G_OBJECT (entry), 
9634                            quark_cursor_hadjustment,
9635                            adjustment, 
9636                            g_object_unref);
9637 }
9638
9639 /**
9640  * gtk_entry_get_cursor_hadjustment:
9641  * @entry: a #GtkEntry
9642  *
9643  * Retrieves the horizontal cursor adjustment for the entry. 
9644  * See gtk_entry_set_cursor_hadjustment().
9645  *
9646  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9647  *   if none has been set.
9648  *
9649  * Since: 2.12
9650  */
9651 GtkAdjustment*
9652 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9653 {
9654   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9655
9656   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9657 }
9658
9659 /**
9660  * gtk_entry_set_progress_fraction:
9661  * @entry: a #GtkEntry
9662  * @fraction: fraction of the task that's been completed
9663  *
9664  * Causes the entry's progress indicator to "fill in" the given
9665  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9666  * inclusive.
9667  *
9668  * Since: 2.16
9669  */
9670 void
9671 gtk_entry_set_progress_fraction (GtkEntry *entry,
9672                                  gdouble   fraction)
9673 {
9674   GtkWidget       *widget;
9675   GtkEntryPrivate *private;
9676   gdouble          old_fraction;
9677   gint x, y, width, height;
9678   gint old_x, old_y, old_width, old_height;
9679
9680   g_return_if_fail (GTK_IS_ENTRY (entry));
9681
9682   widget = GTK_WIDGET (entry);
9683   private = GTK_ENTRY_GET_PRIVATE (entry);
9684
9685   if (private->progress_pulse_mode)
9686     old_fraction = -1;
9687   else
9688     old_fraction = private->progress_fraction;
9689
9690   if (gtk_widget_is_drawable (widget))
9691     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9692
9693   fraction = CLAMP (fraction, 0.0, 1.0);
9694
9695   private->progress_fraction = fraction;
9696   private->progress_pulse_mode = FALSE;
9697   private->progress_pulse_current = 0.0;
9698
9699   if (gtk_widget_is_drawable (widget))
9700     {
9701       get_progress_area (widget, &x, &y, &width, &height);
9702
9703       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9704         gtk_widget_queue_draw (widget);
9705     }
9706
9707   if (fraction != old_fraction)
9708     g_object_notify (G_OBJECT (entry), "progress-fraction");
9709 }
9710
9711 /**
9712  * gtk_entry_get_progress_fraction:
9713  * @entry: a #GtkEntry
9714  *
9715  * Returns the current fraction of the task that's been completed.
9716  * See gtk_entry_set_progress_fraction().
9717  *
9718  * Return value: a fraction from 0.0 to 1.0
9719  *
9720  * Since: 2.16
9721  */
9722 gdouble
9723 gtk_entry_get_progress_fraction (GtkEntry *entry)
9724 {
9725   GtkEntryPrivate *private;
9726
9727   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9728
9729   private = GTK_ENTRY_GET_PRIVATE (entry);
9730
9731   return private->progress_fraction;
9732 }
9733
9734 /**
9735  * gtk_entry_set_progress_pulse_step:
9736  * @entry: a #GtkEntry
9737  * @fraction: fraction between 0.0 and 1.0
9738  *
9739  * Sets the fraction of total entry width to move the progress
9740  * bouncing block for each call to gtk_entry_progress_pulse().
9741  *
9742  * Since: 2.16
9743  */
9744 void
9745 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9746                                    gdouble   fraction)
9747 {
9748   GtkEntryPrivate *private;
9749
9750   g_return_if_fail (GTK_IS_ENTRY (entry));
9751
9752   private = GTK_ENTRY_GET_PRIVATE (entry);
9753
9754   fraction = CLAMP (fraction, 0.0, 1.0);
9755
9756   if (fraction != private->progress_pulse_fraction)
9757     {
9758       private->progress_pulse_fraction = fraction;
9759
9760       gtk_widget_queue_draw (GTK_WIDGET (entry));
9761
9762       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
9763     }
9764 }
9765
9766 /**
9767  * gtk_entry_get_progress_pulse_step:
9768  * @entry: a #GtkEntry
9769  *
9770  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
9771  *
9772  * Return value: a fraction from 0.0 to 1.0
9773  *
9774  * Since: 2.16
9775  */
9776 gdouble
9777 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
9778 {
9779   GtkEntryPrivate *private;
9780
9781   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9782
9783   private = GTK_ENTRY_GET_PRIVATE (entry);
9784
9785   return private->progress_pulse_fraction;
9786 }
9787
9788 /**
9789  * gtk_entry_progress_pulse:
9790  * @entry: a #GtkEntry
9791  *
9792  * Indicates that some progress is made, but you don't know how much.
9793  * Causes the entry's progress indicator to enter "activity mode,"
9794  * where a block bounces back and forth. Each call to
9795  * gtk_entry_progress_pulse() causes the block to move by a little bit
9796  * (the amount of movement per pulse is determined by
9797  * gtk_entry_set_progress_pulse_step()).
9798  *
9799  * Since: 2.16
9800  */
9801 void
9802 gtk_entry_progress_pulse (GtkEntry *entry)
9803 {
9804   GtkEntryPrivate *private;
9805
9806   g_return_if_fail (GTK_IS_ENTRY (entry));
9807
9808   private = GTK_ENTRY_GET_PRIVATE (entry);
9809
9810   if (private->progress_pulse_mode)
9811     {
9812       if (private->progress_pulse_way_back)
9813         {
9814           private->progress_pulse_current -= private->progress_pulse_fraction;
9815
9816           if (private->progress_pulse_current < 0.0)
9817             {
9818               private->progress_pulse_current = 0.0;
9819               private->progress_pulse_way_back = FALSE;
9820             }
9821         }
9822       else
9823         {
9824           private->progress_pulse_current += private->progress_pulse_fraction;
9825
9826           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
9827             {
9828               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
9829               private->progress_pulse_way_back = TRUE;
9830             }
9831         }
9832     }
9833   else
9834     {
9835       private->progress_fraction = 0.0;
9836       private->progress_pulse_mode = TRUE;
9837       private->progress_pulse_way_back = FALSE;
9838       private->progress_pulse_current = 0.0;
9839     }
9840
9841   gtk_widget_queue_draw (GTK_WIDGET (entry));
9842 }
9843
9844 /* Caps Lock warning for password entries */
9845
9846 static void
9847 show_capslock_feedback (GtkEntry    *entry,
9848                         const gchar *text)
9849 {
9850   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9851
9852   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
9853     {
9854       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
9855       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
9856       priv->caps_lock_warning_shown = TRUE;
9857     }
9858
9859   if (priv->caps_lock_warning_shown)
9860     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
9861   else
9862     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
9863 }
9864
9865 static void
9866 remove_capslock_feedback (GtkEntry *entry)
9867 {
9868   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9869
9870   if (priv->caps_lock_warning_shown)
9871     {
9872       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
9873       priv->caps_lock_warning_shown = FALSE;
9874     } 
9875 }
9876
9877 static void
9878 keymap_state_changed (GdkKeymap *keymap, 
9879                       GtkEntry  *entry)
9880 {
9881   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9882   char *text = NULL;
9883
9884   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
9885     { 
9886       if (gdk_keymap_get_num_lock_state (keymap)
9887           && gdk_keymap_get_caps_lock_state (keymap))
9888         text = _("Caps Lock and Num Lock are on");
9889       else if (gdk_keymap_get_num_lock_state (keymap))
9890         text = _("Num Lock is on");
9891       else if (gdk_keymap_get_caps_lock_state (keymap))
9892         text = _("Caps Lock is on");
9893     }
9894
9895   if (text)
9896     show_capslock_feedback (entry, text);
9897   else
9898     remove_capslock_feedback (entry);
9899 }