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