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