]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
Fix tests
[~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
2467   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2468   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2469   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2470   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2471
2472   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2473 }
2474
2475 static void
2476 gtk_entry_finalize (GObject *object)
2477 {
2478   GtkEntry *entry = GTK_ENTRY (object);
2479   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2480   EntryIconInfo *icon_info = NULL;
2481   gint i;
2482
2483   for (i = 0; i < MAX_ICONS; i++)
2484     {
2485       if ((icon_info = priv->icons[i]) != NULL)
2486         {
2487           if (icon_info->target_list != NULL)
2488             {
2489               gtk_target_list_unref (icon_info->target_list);
2490               icon_info->target_list = NULL;
2491             }
2492
2493           g_slice_free (EntryIconInfo, icon_info);
2494           priv->icons[i] = NULL;
2495         }
2496     }
2497
2498   gtk_entry_set_completion (entry, NULL);
2499
2500   if (entry->cached_layout)
2501     g_object_unref (entry->cached_layout);
2502
2503   g_object_unref (entry->im_context);
2504
2505   if (entry->blink_timeout)
2506     g_source_remove (entry->blink_timeout);
2507
2508   if (entry->recompute_idle)
2509     g_source_remove (entry->recompute_idle);
2510
2511   g_free (priv->im_module);
2512
2513   /* COMPAT: entry->text is a deprecated field, and the allocation 
2514      is owned by the buffer. */
2515
2516   gtk_entry_set_buffer (entry, NULL);
2517
2518   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2519 }
2520
2521 static DisplayMode
2522 gtk_entry_get_display_mode (GtkEntry *entry)
2523 {
2524   GtkEntryPrivate *priv;
2525   if (entry->visible)
2526     return DISPLAY_NORMAL;
2527   priv = GTK_ENTRY_GET_PRIVATE (entry);
2528   if (entry->invisible_char == 0 && priv->invisible_char_set)
2529     return DISPLAY_BLANK;
2530   return DISPLAY_INVISIBLE;
2531 }
2532
2533 static gchar*
2534 gtk_entry_get_display_text (GtkEntry *entry,
2535                             gint      start_pos,
2536                             gint      end_pos)
2537 {
2538   GtkEntryPasswordHint *password_hint;
2539   GtkEntryPrivate *priv;
2540   gunichar invisible_char;
2541   const gchar *start;
2542   const gchar *end;
2543   const gchar *text;
2544   gchar char_str[7];
2545   gint char_len;
2546   GString *str;
2547   guint length;
2548   gint i;
2549
2550   priv = GTK_ENTRY_GET_PRIVATE (entry);
2551   text = gtk_entry_buffer_get_text (get_buffer (entry));
2552   length = gtk_entry_buffer_get_length (get_buffer (entry));
2553
2554   if (end_pos < 0)
2555     end_pos = length;
2556   if (start_pos > length)
2557     start_pos = length;
2558
2559   if (end_pos <= start_pos)
2560       return g_strdup ("");
2561   else if (entry->visible)
2562     {
2563       start = g_utf8_offset_to_pointer (text, start_pos);
2564       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2565       return g_strndup (start, end - start);
2566     }
2567   else
2568     {
2569       str = g_string_sized_new (length * 2);
2570
2571       /* Figure out what our invisible char is and encode it */
2572       if (!entry->invisible_char)
2573           invisible_char = priv->invisible_char_set ? ' ' : '*';
2574       else
2575           invisible_char = entry->invisible_char;
2576       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2577
2578       /*
2579        * Add hidden characters for each character in the text
2580        * buffer. If there is a password hint, then keep that
2581        * character visible.
2582        */
2583
2584       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2585       for (i = start_pos; i < end_pos; ++i)
2586         {
2587           if (password_hint && i == password_hint->position)
2588             {
2589               start = g_utf8_offset_to_pointer (text, i);
2590               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2591             }
2592           else
2593             {
2594               g_string_append_len (str, char_str, char_len);
2595             }
2596         }
2597
2598       return g_string_free (str, FALSE);
2599     }
2600
2601 }
2602
2603 static void
2604 update_cursors (GtkWidget *widget)
2605 {
2606   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2607   EntryIconInfo *icon_info = NULL;
2608   GdkDisplay *display;
2609   GdkCursor *cursor;
2610   gint i;
2611
2612   for (i = 0; i < MAX_ICONS; i++)
2613     {
2614       if ((icon_info = priv->icons[i]) != NULL)
2615         {
2616           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2617             gdk_window_show_unraised (icon_info->window);
2618
2619           /* The icon windows are not children of the visible entry window,
2620            * thus we can't just inherit the xterm cursor. Slight complication 
2621            * here is that for the entry, insensitive => arrow cursor, but for 
2622            * an icon in a sensitive entry, insensitive => xterm cursor.
2623            */
2624           if (gtk_widget_is_sensitive (widget) &&
2625               (icon_info->insensitive || 
2626                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2627             {
2628               display = gtk_widget_get_display (widget);
2629               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2630               gdk_window_set_cursor (icon_info->window, cursor);
2631               gdk_cursor_unref (cursor);
2632             }
2633           else
2634             {
2635               gdk_window_set_cursor (icon_info->window, NULL);
2636             }
2637         }
2638     }
2639 }
2640
2641 static void
2642 realize_icon_info (GtkWidget            *widget, 
2643                    GtkEntryIconPosition  icon_pos)
2644 {
2645   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2646   EntryIconInfo *icon_info = priv->icons[icon_pos];
2647   GdkWindowAttr attributes;
2648   gint attributes_mask;
2649
2650   g_return_if_fail (icon_info != NULL);
2651
2652   attributes.x = 0;
2653   attributes.y = 0;
2654   attributes.width = 1;
2655   attributes.height = 1;
2656   attributes.window_type = GDK_WINDOW_CHILD;
2657   attributes.wclass = GDK_INPUT_OUTPUT;
2658   attributes.visual = gtk_widget_get_visual (widget);
2659   attributes.colormap = gtk_widget_get_colormap (widget);
2660   attributes.event_mask = gtk_widget_get_events (widget);
2661   attributes.event_mask |= (GDK_EXPOSURE_MASK |
2662                                 GDK_BUTTON_PRESS_MASK |
2663                                 GDK_BUTTON_RELEASE_MASK |
2664                                 GDK_BUTTON1_MOTION_MASK |
2665                                 GDK_BUTTON3_MOTION_MASK |
2666                                 GDK_POINTER_MOTION_HINT_MASK |
2667                                 GDK_POINTER_MOTION_MASK |
2668                                 GDK_ENTER_NOTIFY_MASK |
2669                             GDK_LEAVE_NOTIFY_MASK);
2670   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2671
2672   icon_info->window = gdk_window_new (widget->window,
2673                                       &attributes,
2674                                       attributes_mask);
2675   gdk_window_set_user_data (icon_info->window, widget);
2676   gdk_window_set_background (icon_info->window,
2677                              &widget->style->base[gtk_widget_get_state (widget)]);
2678
2679   gtk_widget_queue_resize (widget);
2680 }
2681
2682 static EntryIconInfo*
2683 construct_icon_info (GtkWidget            *widget, 
2684                      GtkEntryIconPosition  icon_pos)
2685 {
2686   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2687   EntryIconInfo *icon_info;
2688
2689   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2690
2691   icon_info = g_slice_new0 (EntryIconInfo);
2692   priv->icons[icon_pos] = icon_info;
2693
2694   if (gtk_widget_get_realized (widget))
2695     realize_icon_info (widget, icon_pos);
2696
2697   return icon_info;
2698 }
2699
2700 static void
2701 gtk_entry_map (GtkWidget *widget)
2702 {
2703   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2704   EntryIconInfo *icon_info = NULL;
2705   gint i;
2706
2707   if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
2708     {
2709       GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2710
2711       for (i = 0; i < MAX_ICONS; i++)
2712         {
2713           if ((icon_info = priv->icons[i]) != NULL)
2714             {
2715               if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2716                 gdk_window_show (icon_info->window);
2717             }
2718         }
2719
2720       update_cursors (widget);
2721     }
2722 }
2723
2724 static void
2725 gtk_entry_unmap (GtkWidget *widget)
2726 {
2727   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2728   EntryIconInfo *icon_info = NULL;
2729   gint i;
2730
2731   if (gtk_widget_get_mapped (widget))
2732     {
2733       for (i = 0; i < MAX_ICONS; i++)
2734         {
2735           if ((icon_info = priv->icons[i]) != NULL)
2736             {
2737               if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2738                 gdk_window_hide (icon_info->window);
2739             }
2740         }
2741
2742       GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2743     }
2744 }
2745
2746 static void
2747 gtk_entry_realize (GtkWidget *widget)
2748 {
2749   GtkEntry *entry;
2750   GtkEntryPrivate *priv;
2751   EntryIconInfo *icon_info;
2752   GdkWindowAttr attributes;
2753   gint attributes_mask;
2754   int i;
2755
2756   gtk_widget_set_realized (widget, TRUE);
2757   entry = GTK_ENTRY (widget);
2758   priv = GTK_ENTRY_GET_PRIVATE (entry);
2759
2760   attributes.window_type = GDK_WINDOW_CHILD;
2761   
2762   get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2763
2764   attributes.wclass = GDK_INPUT_OUTPUT;
2765   attributes.visual = gtk_widget_get_visual (widget);
2766   attributes.colormap = gtk_widget_get_colormap (widget);
2767   attributes.event_mask = gtk_widget_get_events (widget);
2768   attributes.event_mask |= (GDK_EXPOSURE_MASK |
2769                             GDK_BUTTON_PRESS_MASK |
2770                             GDK_BUTTON_RELEASE_MASK |
2771                             GDK_BUTTON1_MOTION_MASK |
2772                             GDK_BUTTON3_MOTION_MASK |
2773                             GDK_POINTER_MOTION_HINT_MASK |
2774                             GDK_POINTER_MOTION_MASK |
2775                             GDK_ENTER_NOTIFY_MASK |
2776                             GDK_LEAVE_NOTIFY_MASK);
2777   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2778
2779   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2780   gdk_window_set_user_data (widget->window, entry);
2781
2782   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2783  
2784   if (gtk_widget_is_sensitive (widget))
2785     {
2786       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2787       attributes_mask |= GDK_WA_CURSOR;
2788     }
2789
2790   entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
2791
2792   gdk_window_set_user_data (entry->text_area, entry);
2793
2794   if (attributes_mask & GDK_WA_CURSOR)
2795     gdk_cursor_unref (attributes.cursor);
2796
2797   widget->style = gtk_style_attach (widget->style, widget->window);
2798
2799   gdk_window_set_background (widget->window, &widget->style->base[gtk_widget_get_state (widget)]);
2800   gdk_window_set_background (entry->text_area, &widget->style->base[gtk_widget_get_state (widget)]);
2801
2802   gdk_window_show (entry->text_area);
2803
2804   gtk_im_context_set_client_window (entry->im_context, entry->text_area);
2805
2806   gtk_entry_adjust_scroll (entry);
2807   gtk_entry_update_primary_selection (entry);
2808
2809
2810   /* If the icon positions are already setup, create their windows.
2811    * Otherwise if they don't exist yet, then construct_icon_info()
2812    * will create the windows once the widget is already realized.
2813    */
2814   for (i = 0; i < MAX_ICONS; i++)
2815     {
2816       if ((icon_info = priv->icons[i]) != NULL)
2817         {
2818           if (icon_info->window == NULL)
2819             realize_icon_info (widget, i);
2820         }
2821     }
2822 }
2823
2824 static void
2825 gtk_entry_unrealize (GtkWidget *widget)
2826 {
2827   GtkEntry *entry = GTK_ENTRY (widget);
2828   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2829   GtkClipboard *clipboard;
2830   EntryIconInfo *icon_info;
2831   gint i;
2832
2833   gtk_entry_reset_layout (entry);
2834   
2835   gtk_im_context_set_client_window (entry->im_context, NULL);
2836
2837   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2838   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2839     gtk_clipboard_clear (clipboard);
2840   
2841   if (entry->text_area)
2842     {
2843       gdk_window_set_user_data (entry->text_area, NULL);
2844       gdk_window_destroy (entry->text_area);
2845       entry->text_area = NULL;
2846     }
2847
2848   if (entry->popup_menu)
2849     {
2850       gtk_widget_destroy (entry->popup_menu);
2851       entry->popup_menu = NULL;
2852     }
2853
2854   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
2855
2856   for (i = 0; i < MAX_ICONS; i++)
2857     {
2858       if ((icon_info = priv->icons[i]) != NULL)
2859         {
2860           if (icon_info->window != NULL)
2861             {
2862               gdk_window_destroy (icon_info->window);
2863               icon_info->window = NULL;
2864             }
2865         }
2866     }
2867 }
2868
2869 void
2870 _gtk_entry_get_borders (GtkEntry *entry,
2871                         gint     *xborder,
2872                         gint     *yborder)
2873 {
2874   GtkWidget *widget = GTK_WIDGET (entry);
2875   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2876
2877   if (entry->has_frame)
2878     {
2879       *xborder = widget->style->xthickness;
2880       *yborder = widget->style->ythickness;
2881     }
2882   else
2883     {
2884       *xborder = 0;
2885       *yborder = 0;
2886     }
2887
2888   if (!priv->interior_focus)
2889     {
2890       *xborder += priv->focus_width;
2891       *yborder += priv->focus_width;
2892     }
2893 }
2894
2895 static void
2896 gtk_entry_size_request (GtkWidget      *widget,
2897                         GtkRequisition *requisition)
2898 {
2899   GtkEntry *entry = GTK_ENTRY (widget);
2900   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2901   PangoFontMetrics *metrics;
2902   gint xborder, yborder;
2903   GtkBorder inner_border;
2904   PangoContext *context;
2905   int icon_widths = 0;
2906   int icon_width, i;
2907   
2908   gtk_widget_ensure_style (widget);
2909   context = gtk_widget_get_pango_context (widget);
2910   metrics = pango_context_get_metrics (context,
2911                                        widget->style->font_desc,
2912                                        pango_context_get_language (context));
2913
2914   entry->ascent = pango_font_metrics_get_ascent (metrics);
2915   entry->descent = pango_font_metrics_get_descent (metrics);
2916   
2917   _gtk_entry_get_borders (entry, &xborder, &yborder);
2918   _gtk_entry_effective_inner_border (entry, &inner_border);
2919
2920   if (entry->width_chars < 0)
2921     requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
2922   else
2923     {
2924       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
2925       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2926       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
2927       
2928       requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
2929     }
2930     
2931   requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
2932
2933   for (i = 0; i < MAX_ICONS; i++)
2934     {
2935       icon_width = get_icon_width (entry, i);
2936       if (icon_width > 0)
2937         icon_widths += icon_width + 2 * priv->icon_margin;
2938     }
2939
2940   if (icon_widths > requisition->width)
2941     requisition->width += icon_widths;
2942
2943   pango_font_metrics_unref (metrics);
2944 }
2945
2946 static void
2947 place_windows (GtkEntry *entry)
2948 {
2949   GtkWidget *widget = GTK_WIDGET (entry);
2950   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2951   gint x, y, width, height;
2952   GtkAllocation primary;
2953   GtkAllocation secondary;
2954   EntryIconInfo *icon_info = NULL;
2955
2956   get_text_area_size (entry, &x, &y, &width, &height);
2957   get_icon_allocations (entry, &primary, &secondary);
2958
2959   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
2960     y += priv->focus_width;
2961
2962   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
2963     x += secondary.width;
2964   else
2965     x += primary.width;
2966   width -= primary.width + secondary.width;
2967
2968   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
2969     gdk_window_move_resize (icon_info->window,
2970                             primary.x, primary.y,
2971                             primary.width, primary.height);
2972
2973   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
2974     gdk_window_move_resize (icon_info->window,
2975                             secondary.x, secondary.y,
2976                             secondary.width, secondary.height);
2977
2978   gdk_window_move_resize (entry->text_area, x, y, width, height);
2979 }
2980
2981 static void
2982 gtk_entry_get_text_area_size (GtkEntry *entry,
2983                               gint     *x,
2984                               gint     *y,
2985                               gint     *width,
2986                               gint     *height)
2987 {
2988   GtkWidget *widget = GTK_WIDGET (entry);
2989   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2990   gint frame_height;
2991   gint xborder, yborder;
2992   GtkRequisition requisition;
2993
2994   gtk_widget_get_child_requisition (widget, &requisition);
2995   _gtk_entry_get_borders (entry, &xborder, &yborder);
2996
2997   if (gtk_widget_get_realized (widget))
2998     gdk_drawable_get_size (widget->window, NULL, &frame_height);
2999   else
3000     frame_height = requisition.height;
3001
3002   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3003     frame_height -= 2 * priv->focus_width;
3004
3005   if (x)
3006     *x = xborder;
3007
3008   if (y)
3009     *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
3010
3011   if (width)
3012     *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
3013
3014   if (height)
3015     *height = requisition.height - yborder * 2;
3016 }
3017
3018 static void
3019 get_text_area_size (GtkEntry *entry,
3020                     gint     *x,
3021                     gint     *y,
3022                     gint     *width,
3023                     gint     *height)
3024 {
3025   GtkEntryClass *class;
3026
3027   g_return_if_fail (GTK_IS_ENTRY (entry));
3028
3029   class = GTK_ENTRY_GET_CLASS (entry);
3030
3031   if (class->get_text_area_size)
3032     class->get_text_area_size (entry, x, y, width, height);
3033 }
3034
3035
3036 static void
3037 get_widget_window_size (GtkEntry *entry,
3038                         gint     *x,
3039                         gint     *y,
3040                         gint     *width,
3041                         gint     *height)
3042 {
3043   GtkRequisition requisition;
3044   GtkWidget *widget = GTK_WIDGET (entry);
3045       
3046   gtk_widget_get_child_requisition (widget, &requisition);
3047
3048   if (x)
3049     *x = widget->allocation.x;
3050
3051   if (y)
3052     {
3053       if (entry->is_cell_renderer)
3054         *y = widget->allocation.y;
3055       else
3056         *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
3057     }
3058
3059   if (width)
3060     *width = widget->allocation.width;
3061
3062   if (height)
3063     {
3064       if (entry->is_cell_renderer)
3065         *height = widget->allocation.height;
3066       else
3067         *height = requisition.height;
3068     }
3069 }
3070
3071 void
3072 _gtk_entry_effective_inner_border (GtkEntry  *entry,
3073                                    GtkBorder *border)
3074 {
3075   GtkBorder *tmp_border;
3076
3077   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
3078
3079   if (tmp_border)
3080     {
3081       *border = *tmp_border;
3082       return;
3083     }
3084
3085   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
3086
3087   if (tmp_border)
3088     {
3089       *border = *tmp_border;
3090       gtk_border_free (tmp_border);
3091       return;
3092     }
3093
3094   *border = default_inner_border;
3095 }
3096
3097 static void
3098 gtk_entry_size_allocate (GtkWidget     *widget,
3099                          GtkAllocation *allocation)
3100 {
3101   GtkEntry *entry = GTK_ENTRY (widget);
3102   
3103   widget->allocation = *allocation;
3104   
3105   if (gtk_widget_get_realized (widget))
3106     {
3107       /* We call gtk_widget_get_child_requisition, since we want (for
3108        * backwards compatibility reasons) the realization here to
3109        * be affected by the usize of the entry, if set
3110        */
3111       gint x, y, width, height;
3112       GtkEntryCompletion* completion;
3113
3114       get_widget_window_size (entry, &x, &y, &width, &height);
3115       gdk_window_move_resize (widget->window, x, y, width, height);
3116
3117       place_windows (entry);
3118       gtk_entry_recompute (entry);
3119
3120       completion = gtk_entry_get_completion (entry);
3121       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3122         _gtk_entry_completion_resize_popup (completion);
3123     }
3124 }
3125
3126 /* Kudos to the gnome-panel guys. */
3127 static void
3128 colorshift_pixbuf (GdkPixbuf *dest,
3129                    GdkPixbuf *src,
3130                    gint       shift)
3131 {
3132   gint i, j;
3133   gint width, height, has_alpha, src_rowstride, dest_rowstride;
3134   guchar *target_pixels;
3135   guchar *original_pixels;
3136   guchar *pix_src;
3137   guchar *pix_dest;
3138   int val;
3139   guchar r, g, b;
3140
3141   has_alpha       = gdk_pixbuf_get_has_alpha (src);
3142   width           = gdk_pixbuf_get_width (src);
3143   height          = gdk_pixbuf_get_height (src);
3144   src_rowstride   = gdk_pixbuf_get_rowstride (src);
3145   dest_rowstride  = gdk_pixbuf_get_rowstride (dest);
3146   original_pixels = gdk_pixbuf_get_pixels (src);
3147   target_pixels   = gdk_pixbuf_get_pixels (dest);
3148
3149   for (i = 0; i < height; i++)
3150     {
3151       pix_dest = target_pixels   + i * dest_rowstride;
3152       pix_src  = original_pixels + i * src_rowstride;
3153
3154       for (j = 0; j < width; j++)
3155         {
3156           r = *(pix_src++);
3157           g = *(pix_src++);
3158           b = *(pix_src++);
3159
3160           val = r + shift;
3161           *(pix_dest++) = CLAMP (val, 0, 255);
3162
3163           val = g + shift;
3164           *(pix_dest++) = CLAMP (val, 0, 255);
3165
3166           val = b + shift;
3167           *(pix_dest++) = CLAMP (val, 0, 255);
3168
3169           if (has_alpha)
3170             *(pix_dest++) = *(pix_src++);
3171         }
3172     }
3173 }
3174
3175 static gboolean
3176 should_prelight (GtkEntry             *entry,
3177                  GtkEntryIconPosition  icon_pos)
3178 {
3179   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3180   EntryIconInfo *icon_info = priv->icons[icon_pos];
3181   gboolean prelight;
3182
3183   if (!icon_info) 
3184     return FALSE;
3185
3186   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3187     return FALSE;
3188
3189   if (icon_info->pressed)
3190     return FALSE;
3191
3192   gtk_widget_style_get (GTK_WIDGET (entry),
3193                         "icon-prelight", &prelight,
3194                         NULL);
3195
3196   return prelight;
3197 }
3198
3199 static void
3200 draw_icon (GtkWidget            *widget,
3201            GtkEntryIconPosition  icon_pos)
3202 {
3203   GtkEntry *entry = GTK_ENTRY (widget);
3204   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3205   EntryIconInfo *icon_info = priv->icons[icon_pos];
3206   GdkPixbuf *pixbuf;
3207   gint x, y, width, height;
3208
3209   if (!icon_info)
3210     return;
3211
3212   gtk_entry_ensure_pixbuf (entry, icon_pos);
3213
3214   if (icon_info->pixbuf == NULL)
3215     return;
3216
3217   gdk_drawable_get_size (icon_info->window, &width, &height);
3218
3219   /* size_allocate hasn't been called yet. These are the default values.
3220    */
3221   if (width == 1 || height == 1)
3222     return;
3223
3224   pixbuf = icon_info->pixbuf;
3225   g_object_ref (pixbuf);
3226
3227   if (gdk_pixbuf_get_height (pixbuf) > height)
3228     {
3229       GdkPixbuf *temp_pixbuf;
3230       gint scale;
3231
3232       scale = height - 2 * priv->icon_margin;
3233       temp_pixbuf = gdk_pixbuf_scale_simple (pixbuf, scale, scale,
3234                                              GDK_INTERP_BILINEAR);
3235       g_object_unref (pixbuf);
3236       pixbuf = temp_pixbuf;
3237     }
3238
3239   x = (width  - gdk_pixbuf_get_width (pixbuf)) / 2;
3240   y = (height - gdk_pixbuf_get_height (pixbuf)) / 2;
3241
3242   if (!gtk_widget_is_sensitive (widget) ||
3243       icon_info->insensitive)
3244     {
3245       GdkPixbuf *temp_pixbuf;
3246
3247       temp_pixbuf = gdk_pixbuf_copy (pixbuf);
3248       gdk_pixbuf_saturate_and_pixelate (pixbuf,
3249                                         temp_pixbuf,
3250                                         0.8f,
3251                                         TRUE);
3252       g_object_unref (pixbuf);
3253       pixbuf = temp_pixbuf;
3254     }
3255   else if (icon_info->prelight)
3256     {
3257       GdkPixbuf *temp_pixbuf;
3258
3259       temp_pixbuf = gdk_pixbuf_copy (pixbuf);
3260       colorshift_pixbuf (temp_pixbuf, pixbuf, 30);
3261       g_object_unref (pixbuf);
3262       pixbuf = temp_pixbuf;
3263     }
3264
3265   gdk_draw_pixbuf (icon_info->window, widget->style->black_gc, pixbuf,
3266                    0, 0, x, y, -1, -1,
3267                    GDK_RGB_DITHER_NORMAL, 0, 0);
3268
3269   g_object_unref (pixbuf);
3270 }
3271
3272
3273 static void
3274 gtk_entry_draw_frame (GtkWidget      *widget,
3275                       GdkEventExpose *event)
3276 {
3277   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
3278   gint x = 0, y = 0, width, height;
3279   gboolean state_hint;
3280   GtkStateType state;
3281
3282   gdk_drawable_get_size (widget->window, &width, &height);
3283
3284   /* Fix a problem with some themes which assume that entry->text_area's
3285    * width equals widget->window's width */
3286   if (GTK_IS_SPIN_BUTTON (widget))
3287     {
3288       gint xborder, yborder;
3289
3290       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3291       _gtk_entry_get_borders (GTK_ENTRY (widget), &xborder, &yborder);
3292
3293       x -= xborder;
3294       width += xborder * 2;
3295     }
3296
3297   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3298     {
3299       x += priv->focus_width;
3300       y += priv->focus_width;
3301       width -= 2 * priv->focus_width;
3302       height -= 2 * priv->focus_width;
3303     }
3304
3305   gtk_widget_style_get (widget, "state-hint", &state_hint, NULL);
3306   if (state_hint)
3307       state = gtk_widget_has_focus (widget) ?
3308         GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
3309   else
3310       state = GTK_STATE_NORMAL;
3311
3312   gtk_paint_shadow (widget->style, widget->window,
3313                     state, priv->shadow_type,
3314                     &event->area, widget, "entry", x, y, width, height);
3315
3316
3317   gtk_entry_draw_progress (widget, event);
3318
3319   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3320     {
3321       x -= priv->focus_width;
3322       y -= priv->focus_width;
3323       width += 2 * priv->focus_width;
3324       height += 2 * priv->focus_width;
3325       
3326       gtk_paint_focus (widget->style, widget->window,
3327                        gtk_widget_get_state (widget),
3328                        &event->area, widget, "entry",
3329                        0, 0, width, height);
3330     }
3331 }
3332
3333 static void
3334 gtk_entry_get_progress_border (GtkWidget *widget,
3335                                GtkBorder *progress_border)
3336 {
3337   GtkBorder *tmp_border;
3338
3339   gtk_widget_style_get (widget, "progress-border", &tmp_border, NULL);
3340   if (tmp_border)
3341     {
3342       *progress_border = *tmp_border;
3343       gtk_border_free (tmp_border);
3344     }
3345   else
3346     {
3347       progress_border->left = widget->style->xthickness;
3348       progress_border->right = widget->style->xthickness;
3349       progress_border->top = widget->style->ythickness;
3350       progress_border->bottom = widget->style->ythickness;
3351     }
3352 }
3353
3354 static void
3355 get_progress_area (GtkWidget *widget,
3356                   gint       *x,
3357                   gint       *y,
3358                   gint       *width,
3359                   gint       *height)
3360 {
3361   GtkEntryPrivate *private = GTK_ENTRY_GET_PRIVATE (widget);
3362   GtkEntry *entry = GTK_ENTRY (widget);
3363   GtkBorder progress_border;
3364
3365   gtk_entry_get_progress_border (widget, &progress_border);
3366
3367   *x = progress_border.left;
3368   *y = progress_border.top;
3369
3370   gdk_drawable_get_size (widget->window, width, height);
3371
3372   *width -= progress_border.left + progress_border.right;
3373   *height -= progress_border.top + progress_border.bottom;
3374
3375   if (gtk_widget_has_focus (widget) && !private->interior_focus)
3376     {
3377       *x += private->focus_width;
3378       *y += private->focus_width;
3379       *width -= 2 * private->focus_width;
3380       *height -= 2 * private->focus_width;
3381     }
3382
3383   if (private->progress_pulse_mode)
3384     {
3385       gdouble value = private->progress_pulse_current;
3386
3387       *x += (gint) floor(value * (*width));
3388       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3389     }
3390   else if (private->progress_fraction > 0)
3391     {
3392       gdouble value = private->progress_fraction;
3393
3394       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3395         {
3396           gint bar_width;
3397
3398           bar_width = floor(value * (*width) + 0.5);
3399           *x += *width - bar_width;
3400           *width = bar_width;
3401         }
3402       else
3403         {
3404           *width = (gint) floor(value * (*width) + 0.5);
3405         }
3406     }
3407   else
3408     {
3409       *width = 0;
3410       *height = 0;
3411     }
3412 }
3413
3414 static void
3415 gtk_entry_draw_progress (GtkWidget      *widget,
3416                          GdkEventExpose *event)
3417 {
3418   gint x, y, width, height;
3419   GtkStateType state;
3420
3421   get_progress_area (widget, &x, &y, &width, &height);
3422
3423   if ((width <= 0) || (height <= 0))
3424     return;
3425
3426   if (event->window != widget->window)
3427     {
3428       gint pos_x, pos_y;
3429
3430       gdk_window_get_position (event->window, &pos_x, &pos_y);
3431
3432       x -= pos_x;
3433       y -= pos_y;
3434     }
3435
3436   state = GTK_STATE_SELECTED;
3437   if (!gtk_widget_get_sensitive (widget))
3438     state = GTK_STATE_INSENSITIVE;
3439
3440   gtk_paint_box (widget->style, event->window,
3441                  state, GTK_SHADOW_OUT,
3442                  &event->area, widget, "entry-progress",
3443                  x, y,
3444                  width, height);
3445 }
3446
3447 static gint
3448 gtk_entry_expose (GtkWidget      *widget,
3449                   GdkEventExpose *event)
3450 {
3451   GtkEntry *entry = GTK_ENTRY (widget);
3452   gboolean state_hint;
3453   GtkStateType state;
3454   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3455
3456   gtk_widget_style_get (widget, "state-hint", &state_hint, NULL);
3457   if (state_hint)
3458     state = gtk_widget_has_focus (widget) ?
3459       GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
3460   else
3461     state = gtk_widget_get_state(widget);
3462
3463   if (widget->window == event->window)
3464     {
3465       gtk_entry_draw_frame (widget, event);
3466     }
3467   else if (entry->text_area == event->window)
3468     {
3469       gint width, height;
3470
3471       gdk_drawable_get_size (entry->text_area, &width, &height);
3472
3473       gtk_paint_flat_box (widget->style, entry->text_area, 
3474                           state, GTK_SHADOW_NONE,
3475                           &event->area, widget, "entry_bg",
3476                           0, 0, width, height);
3477
3478       gtk_entry_draw_progress (widget, event);
3479
3480       if (entry->dnd_position != -1)
3481         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
3482       
3483       gtk_entry_draw_text (GTK_ENTRY (widget));
3484
3485       /* When no text is being displayed at all, don't show the cursor */
3486       if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3487           gtk_widget_has_focus (widget) &&
3488           entry->selection_bound == entry->current_pos && entry->cursor_visible) 
3489         gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
3490     }
3491   else
3492     {
3493       int i;
3494
3495       for (i = 0; i < MAX_ICONS; i++)
3496         {
3497           EntryIconInfo *icon_info = priv->icons[i];
3498
3499           if (icon_info != NULL && event->window == icon_info->window)
3500             {
3501               gint width, height;
3502
3503               gdk_drawable_get_size (icon_info->window, &width, &height);
3504
3505               gtk_paint_flat_box (widget->style, icon_info->window,
3506                                   gtk_widget_get_state (widget), GTK_SHADOW_NONE,
3507                                   NULL, widget, "entry_bg",
3508                                   0, 0, width, height);
3509
3510               gtk_entry_draw_progress (widget, event);
3511               draw_icon (widget, i);
3512
3513               break;
3514             }
3515         }
3516     }
3517
3518   return FALSE;
3519 }
3520
3521 static gint
3522 gtk_entry_enter_notify (GtkWidget *widget,
3523                         GdkEventCrossing *event)
3524 {
3525   GtkEntry *entry = GTK_ENTRY (widget);
3526   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3527   gint i;
3528
3529   for (i = 0; i < MAX_ICONS; i++)
3530     {
3531       EntryIconInfo *icon_info = priv->icons[i];
3532
3533       if (icon_info != NULL && event->window == icon_info->window)
3534         {
3535           if (should_prelight (entry, i))
3536             {
3537               icon_info->prelight = TRUE;
3538               gtk_widget_queue_draw (widget);
3539             }
3540
3541           break;
3542         }
3543     }
3544
3545     return FALSE;
3546 }
3547
3548 static gint
3549 gtk_entry_leave_notify (GtkWidget        *widget,
3550                         GdkEventCrossing *event)
3551 {
3552   GtkEntry *entry = GTK_ENTRY (widget);
3553   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3554   gint i;
3555
3556   for (i = 0; i < MAX_ICONS; i++)
3557     {
3558       EntryIconInfo *icon_info = priv->icons[i];
3559
3560       if (icon_info != NULL && event->window == icon_info->window)
3561         {
3562           /* a grab means that we may never see the button release */
3563           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3564             icon_info->pressed = FALSE;
3565
3566           if (should_prelight (entry, i))
3567             {
3568               icon_info->prelight = FALSE;
3569               gtk_widget_queue_draw (widget);
3570             }
3571
3572           break;
3573         }
3574     }
3575
3576   return FALSE;
3577 }
3578
3579 static void
3580 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3581                             gint     **ranges,
3582                             gint      *n_ranges)
3583 {
3584   gint start_char, end_char;
3585
3586   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3587     {
3588       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3589       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3590       const char *text = pango_layout_get_text (layout);
3591       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3592       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3593       gint real_n_ranges, i;
3594
3595       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3596
3597       if (ranges)
3598         {
3599           gint *r = *ranges;
3600           
3601           for (i = 0; i < real_n_ranges; ++i)
3602             {
3603               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3604               r[2 * i] = r[2 * i] / PANGO_SCALE;
3605             }
3606         }
3607       
3608       if (n_ranges)
3609         *n_ranges = real_n_ranges;
3610     }
3611   else
3612     {
3613       if (n_ranges)
3614         *n_ranges = 0;
3615       if (ranges)
3616         *ranges = NULL;
3617     }
3618 }
3619
3620 static gboolean
3621 in_selection (GtkEntry *entry,
3622               gint      x)
3623 {
3624   gint *ranges;
3625   gint n_ranges, i;
3626   gint retval = FALSE;
3627
3628   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3629
3630   for (i = 0; i < n_ranges; ++i)
3631     {
3632       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3633         {
3634           retval = TRUE;
3635           break;
3636         }
3637     }
3638
3639   g_free (ranges);
3640   return retval;
3641 }
3642               
3643 static gint
3644 gtk_entry_button_press (GtkWidget      *widget,
3645                         GdkEventButton *event)
3646 {
3647   GtkEntry *entry = GTK_ENTRY (widget);
3648   GtkEditable *editable = GTK_EDITABLE (widget);
3649   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3650   EntryIconInfo *icon_info = NULL;
3651   gint tmp_pos;
3652   gint sel_start, sel_end;
3653   gint i;
3654
3655   for (i = 0; i < MAX_ICONS; i++)
3656     {
3657       icon_info = priv->icons[i];
3658
3659       if (!icon_info || icon_info->insensitive)
3660         continue;
3661
3662       if (event->window == icon_info->window)
3663         {
3664           if (should_prelight (entry, i))
3665             {
3666               icon_info->prelight = FALSE;
3667               gtk_widget_queue_draw (widget);
3668             }
3669
3670           priv->start_x = event->x;
3671           priv->start_y = event->y;
3672           icon_info->pressed = TRUE;
3673
3674           if (!icon_info->nonactivatable)
3675             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3676
3677           return TRUE;
3678         }
3679     }
3680
3681   if (event->window != entry->text_area ||
3682       (entry->button && event->button != entry->button))
3683     return FALSE;
3684
3685   gtk_entry_reset_blink_time (entry);
3686
3687   entry->button = event->button;
3688   
3689   if (!gtk_widget_has_focus (widget))
3690     {
3691       entry->in_click = TRUE;
3692       gtk_widget_grab_focus (widget);
3693       entry->in_click = FALSE;
3694     }
3695   
3696   tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
3697     
3698   if (event->button == 1)
3699     {
3700       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3701       
3702       entry->select_words = FALSE;
3703       entry->select_lines = FALSE;
3704
3705       if (event->state & GDK_SHIFT_MASK)
3706         {
3707           _gtk_entry_reset_im_context (entry);
3708           
3709           if (!have_selection) /* select from the current position to the clicked position */
3710             sel_start = sel_end = entry->current_pos;
3711           
3712           if (tmp_pos > sel_start && tmp_pos < sel_end)
3713             {
3714               /* Truncate current selection, but keep it as big as possible */
3715               if (tmp_pos - sel_start > sel_end - tmp_pos)
3716                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3717               else
3718                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3719             }
3720           else
3721             {
3722               gboolean extend_to_left;
3723               gint start, end;
3724
3725               /* Figure out what click selects and extend current selection */
3726               switch (event->type)
3727                 {
3728                 case GDK_BUTTON_PRESS:
3729                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3730                   break;
3731                   
3732                 case GDK_2BUTTON_PRESS:
3733                   entry->select_words = TRUE;
3734                   gtk_entry_select_word (entry);
3735                   break;
3736                   
3737                 case GDK_3BUTTON_PRESS:
3738                   entry->select_lines = TRUE;
3739                   gtk_entry_select_line (entry);
3740                   break;
3741
3742                 default:
3743                   break;
3744                 }
3745
3746               start = MIN (entry->current_pos, entry->selection_bound);
3747               start = MIN (sel_start, start);
3748               
3749               end = MAX (entry->current_pos, entry->selection_bound);
3750               end = MAX (sel_end, end);
3751
3752               if (tmp_pos == sel_start || tmp_pos == sel_end)
3753                 extend_to_left = (tmp_pos == start);
3754               else
3755                 extend_to_left = (end == sel_end);
3756               
3757               if (extend_to_left)
3758                 gtk_entry_set_positions (entry, start, end);
3759               else
3760                 gtk_entry_set_positions (entry, end, start);
3761             }
3762         }
3763       else /* no shift key */
3764         switch (event->type)
3765         {
3766         case GDK_BUTTON_PRESS:
3767           if (in_selection (entry, event->x + entry->scroll_offset))
3768             {
3769               /* Click inside the selection - we'll either start a drag, or
3770                * clear the selection
3771                */
3772               entry->in_drag = TRUE;
3773               entry->drag_start_x = event->x + entry->scroll_offset;
3774               entry->drag_start_y = event->y;
3775             }
3776           else
3777             gtk_editable_set_position (editable, tmp_pos);
3778           break;
3779  
3780         case GDK_2BUTTON_PRESS:
3781           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3782            * receiving a GDK_2BUTTON_PRESS so we need to reset
3783            * entry->in_drag which may have been set above
3784            */
3785           entry->in_drag = FALSE;
3786           entry->select_words = TRUE;
3787           gtk_entry_select_word (entry);
3788           break;
3789         
3790         case GDK_3BUTTON_PRESS:
3791           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3792            * receiving a GDK_3BUTTON_PRESS so we need to reset
3793            * entry->in_drag which may have been set above
3794            */
3795           entry->in_drag = FALSE;
3796           entry->select_lines = TRUE;
3797           gtk_entry_select_line (entry);
3798           break;
3799
3800         default:
3801           break;
3802         }
3803
3804       return TRUE;
3805     }
3806   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3807     {
3808       if (entry->editable)
3809         {
3810           priv->insert_pos = tmp_pos;
3811           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3812           return TRUE;
3813         }
3814       else
3815         {
3816           gtk_widget_error_bell (widget);
3817         }
3818     }
3819   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3820     {
3821       gtk_entry_do_popup (entry, event);
3822       entry->button = 0;        /* Don't wait for release, since the menu will gtk_grab_add */
3823
3824       return TRUE;
3825     }
3826
3827   return FALSE;
3828 }
3829
3830 static gint
3831 gtk_entry_button_release (GtkWidget      *widget,
3832                           GdkEventButton *event)
3833 {
3834   GtkEntry *entry = GTK_ENTRY (widget);
3835   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
3836   EntryIconInfo *icon_info = NULL;
3837   gint i;
3838
3839   for (i = 0; i < MAX_ICONS; i++)
3840     {
3841       icon_info = priv->icons[i];
3842
3843       if (!icon_info || icon_info->insensitive)
3844         continue;
3845
3846       if (event->window == icon_info->window)
3847         {
3848           gint width, height;
3849
3850           gdk_drawable_get_size (icon_info->window, &width, &height);
3851
3852           icon_info->pressed = FALSE;
3853
3854           if (should_prelight (entry, i) &&
3855               event->x >= 0 && event->y >= 0 &&
3856               event->x < width && event->y < height)
3857             {
3858               icon_info->prelight = TRUE;
3859               gtk_widget_queue_draw (widget);
3860             }
3861
3862           if (!icon_info->nonactivatable)
3863             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3864
3865           return TRUE;
3866         }
3867     }
3868
3869   if (event->window != entry->text_area || entry->button != event->button)
3870     return FALSE;
3871
3872   if (entry->in_drag)
3873     {
3874       gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
3875
3876       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3877
3878       entry->in_drag = 0;
3879     }
3880   
3881   entry->button = 0;
3882   
3883   gtk_entry_update_primary_selection (entry);
3884               
3885   return TRUE;
3886 }
3887
3888 static gchar *
3889 _gtk_entry_get_selected_text (GtkEntry *entry)
3890 {
3891   GtkEditable *editable = GTK_EDITABLE (entry);
3892   gint         start_text, end_text;
3893   gchar       *text = NULL;
3894
3895   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3896     text = gtk_editable_get_chars (editable, start_text, end_text);
3897
3898   return text;
3899 }
3900
3901 static gint
3902 gtk_entry_motion_notify (GtkWidget      *widget,
3903                          GdkEventMotion *event)
3904 {
3905   GtkEntry *entry = GTK_ENTRY (widget);
3906   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3907   EntryIconInfo *icon_info = NULL;
3908   GdkDragContext *context;
3909   gint tmp_pos;
3910   gint i;
3911
3912   for (i = 0; i < MAX_ICONS; i++)
3913     {
3914       icon_info = priv->icons[i];
3915
3916       if (!icon_info || icon_info->insensitive)
3917         continue;
3918
3919       if (event->window == icon_info->window)
3920         {
3921           if (icon_info->pressed &&
3922               icon_info->target_list != NULL &&
3923               gtk_drag_check_threshold (widget,
3924                                         priv->start_x,
3925                                         priv->start_y,
3926                                         event->x,
3927                                         event->y))
3928             {
3929               icon_info->in_drag = TRUE;
3930               icon_info->pressed = FALSE;
3931               context = gtk_drag_begin (widget,
3932                                         icon_info->target_list,
3933                                         icon_info->actions,
3934                                         1,
3935                                         (GdkEvent*)event);
3936             }
3937
3938           return TRUE;
3939         }
3940     }
3941
3942   if (entry->mouse_cursor_obscured)
3943     {
3944       GdkCursor *cursor;
3945       
3946       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3947       gdk_window_set_cursor (entry->text_area, cursor);
3948       gdk_cursor_unref (cursor);
3949       entry->mouse_cursor_obscured = FALSE;
3950     }
3951
3952   if (event->window != entry->text_area || entry->button != 1)
3953     return FALSE;
3954
3955   if (entry->select_lines)
3956     return TRUE;
3957
3958   gdk_event_request_motions (event);
3959
3960   if (entry->in_drag)
3961     {
3962       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
3963           gtk_drag_check_threshold (widget,
3964                                     entry->drag_start_x, entry->drag_start_y,
3965                                     event->x + entry->scroll_offset, event->y))
3966         {
3967           GdkDragContext *context;
3968           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
3969           guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
3970           gchar *text = NULL;
3971           GdkPixmap *pixmap = NULL;
3972
3973           gtk_target_list_add_text_targets (target_list, 0);
3974
3975           text = _gtk_entry_get_selected_text (entry);
3976           pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
3977
3978           context = gtk_drag_begin (widget, target_list, actions,
3979                                     entry->button, (GdkEvent *)event);
3980           
3981           if (pixmap)
3982             gtk_drag_set_icon_pixmap (context,
3983                                       gdk_drawable_get_colormap (pixmap),
3984                                       pixmap,
3985                                       NULL,
3986                                       -2, -2);
3987           else
3988             gtk_drag_set_icon_default (context);
3989           
3990           if (pixmap)
3991             g_object_unref (pixmap);
3992           g_free (text);
3993
3994           entry->in_drag = FALSE;
3995           entry->button = 0;
3996           
3997           gtk_target_list_unref (target_list);
3998         }
3999     }
4000   else
4001     {
4002       gint height;
4003       gdk_drawable_get_size (entry->text_area, NULL, &height);
4004
4005       if (event->y < 0)
4006         tmp_pos = 0;
4007       else if (event->y >= height)
4008         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4009       else
4010         tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
4011       
4012       if (entry->select_words) 
4013         {
4014           gint min, max;
4015           gint old_min, old_max;
4016           gint pos, bound;
4017           
4018           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4019           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4020           
4021           pos = entry->current_pos;
4022           bound = entry->selection_bound;
4023
4024           old_min = MIN(entry->current_pos, entry->selection_bound);
4025           old_max = MAX(entry->current_pos, entry->selection_bound);
4026           
4027           if (min < old_min)
4028             {
4029               pos = min;
4030               bound = old_max;
4031             }
4032           else if (old_max < max) 
4033             {
4034               pos = max;
4035               bound = old_min;
4036             }
4037           else if (pos == old_min) 
4038             {
4039               if (entry->current_pos != min)
4040                 pos = max;
4041             }
4042           else 
4043             {
4044               if (entry->current_pos != max)
4045                 pos = min;
4046             }
4047         
4048           gtk_entry_set_positions (entry, pos, bound);
4049         }
4050       else
4051         gtk_entry_set_positions (entry, tmp_pos, -1);
4052     }
4053       
4054   return TRUE;
4055 }
4056
4057 static void
4058 set_invisible_cursor (GdkWindow *window)
4059 {
4060   GdkDisplay *display;
4061   GdkCursor *cursor;
4062
4063   display = gdk_drawable_get_display (window);
4064   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4065
4066   gdk_window_set_cursor (window, cursor);
4067
4068   gdk_cursor_unref (cursor);
4069 }
4070
4071 static void
4072 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4073 {
4074   if (entry->mouse_cursor_obscured)
4075     return;
4076
4077   set_invisible_cursor (entry->text_area);
4078   
4079   entry->mouse_cursor_obscured = TRUE;  
4080 }
4081
4082 static gint
4083 gtk_entry_key_press (GtkWidget   *widget,
4084                      GdkEventKey *event)
4085 {
4086   GtkEntry *entry = GTK_ENTRY (widget);
4087
4088   gtk_entry_reset_blink_time (entry);
4089   gtk_entry_pend_cursor_blink (entry);
4090
4091   if (entry->editable)
4092     {
4093       if (gtk_im_context_filter_keypress (entry->im_context, event))
4094         {
4095           gtk_entry_obscure_mouse_cursor (entry);
4096           entry->need_im_reset = TRUE;
4097           return TRUE;
4098         }
4099     }
4100
4101   if (event->keyval == GDK_Return || 
4102       event->keyval == GDK_KP_Enter || 
4103       event->keyval == GDK_ISO_Enter || 
4104       event->keyval == GDK_Escape)
4105     {
4106       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4107       
4108       if (completion && completion->priv->completion_timeout)
4109         {
4110           g_source_remove (completion->priv->completion_timeout);
4111           completion->priv->completion_timeout = 0;
4112         }
4113
4114       _gtk_entry_reset_im_context (entry);
4115     }
4116
4117   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4118     /* Activate key bindings
4119      */
4120     return TRUE;
4121
4122   if (!entry->editable && event->length)
4123     gtk_widget_error_bell (widget);
4124
4125   return FALSE;
4126 }
4127
4128 static gint
4129 gtk_entry_key_release (GtkWidget   *widget,
4130                        GdkEventKey *event)
4131 {
4132   GtkEntry *entry = GTK_ENTRY (widget);
4133
4134   if (entry->editable)
4135     {
4136       if (gtk_im_context_filter_keypress (entry->im_context, event))
4137         {
4138           entry->need_im_reset = TRUE;
4139           return TRUE;
4140         }
4141     }
4142
4143   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4144 }
4145
4146 static gint
4147 gtk_entry_focus_in (GtkWidget     *widget,
4148                     GdkEventFocus *event)
4149 {
4150   GtkEntry *entry = GTK_ENTRY (widget);
4151   GdkKeymap *keymap;
4152
4153   gtk_widget_queue_draw (widget);
4154
4155   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4156
4157   if (entry->editable)
4158     {
4159       entry->need_im_reset = TRUE;
4160       gtk_im_context_focus_in (entry->im_context);
4161       keymap_state_changed (keymap, entry);
4162       g_signal_connect (keymap, "state-changed", 
4163                         G_CALLBACK (keymap_state_changed), entry);
4164     }
4165
4166   g_signal_connect (keymap, "direction-changed",
4167                     G_CALLBACK (keymap_direction_changed), entry);
4168
4169   gtk_entry_reset_blink_time (entry);
4170   gtk_entry_check_cursor_blink (entry);
4171
4172   return FALSE;
4173 }
4174
4175 static gint
4176 gtk_entry_focus_out (GtkWidget     *widget,
4177                      GdkEventFocus *event)
4178 {
4179   GtkEntry *entry = GTK_ENTRY (widget);
4180   GtkEntryCompletion *completion;
4181   GdkKeymap *keymap;
4182
4183   gtk_widget_queue_draw (widget);
4184
4185   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4186
4187   if (entry->editable)
4188     {
4189       entry->need_im_reset = TRUE;
4190       gtk_im_context_focus_out (entry->im_context);
4191       remove_capslock_feedback (entry);
4192     }
4193
4194   gtk_entry_check_cursor_blink (entry);
4195   
4196   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4197   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4198
4199   completion = gtk_entry_get_completion (entry);
4200   if (completion)
4201     _gtk_entry_completion_popdown (completion);
4202   
4203   return FALSE;
4204 }
4205
4206 static void
4207 gtk_entry_grab_focus (GtkWidget        *widget)
4208 {
4209   GtkEntry *entry = GTK_ENTRY (widget);
4210   gboolean select_on_focus;
4211   
4212   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4213
4214   if (entry->editable && !entry->in_click)
4215     {
4216       g_object_get (gtk_widget_get_settings (widget),
4217                     "gtk-entry-select-on-focus",
4218                     &select_on_focus,
4219                     NULL);
4220   
4221       if (select_on_focus)
4222         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4223     }
4224 }
4225
4226 static void 
4227 gtk_entry_direction_changed (GtkWidget        *widget,
4228                              GtkTextDirection  previous_dir)
4229 {
4230   GtkEntry *entry = GTK_ENTRY (widget);
4231
4232   gtk_entry_recompute (entry);
4233       
4234   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4235 }
4236
4237 static void
4238 gtk_entry_state_changed (GtkWidget      *widget,
4239                          GtkStateType    previous_state)
4240 {
4241   GtkEntry *entry = GTK_ENTRY (widget);
4242   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
4243   GdkCursor *cursor;
4244   gint i;
4245   
4246   if (gtk_widget_get_realized (widget))
4247     {
4248       gdk_window_set_background (widget->window, &widget->style->base[gtk_widget_get_state (widget)]);
4249       gdk_window_set_background (entry->text_area, &widget->style->base[gtk_widget_get_state (widget)]);
4250       for (i = 0; i < MAX_ICONS; i++) 
4251         {
4252           EntryIconInfo *icon_info = priv->icons[i];
4253           if (icon_info && icon_info->window)
4254             gdk_window_set_background (icon_info->window, &widget->style->base[gtk_widget_get_state (widget)]);
4255         }
4256
4257       if (gtk_widget_is_sensitive (widget))
4258         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4259       else 
4260         cursor = NULL;
4261       
4262       gdk_window_set_cursor (entry->text_area, cursor);
4263
4264       if (cursor)
4265         gdk_cursor_unref (cursor);
4266
4267       entry->mouse_cursor_obscured = FALSE;
4268
4269       update_cursors (widget);
4270     }
4271
4272   if (!gtk_widget_is_sensitive (widget))
4273     {
4274       /* Clear any selection */
4275       gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);      
4276     }
4277   
4278   gtk_widget_queue_draw (widget);
4279 }
4280
4281 static void
4282 gtk_entry_screen_changed (GtkWidget *widget,
4283                           GdkScreen *old_screen)
4284 {
4285   gtk_entry_recompute (GTK_ENTRY (widget));
4286 }
4287
4288 /* GtkEditable method implementations
4289  */
4290 static void
4291 gtk_entry_insert_text (GtkEditable *editable,
4292                        const gchar *new_text,
4293                        gint         new_text_length,
4294                        gint        *position)
4295 {
4296   g_object_ref (editable);
4297
4298   /*
4299    * The incoming text may a password or other secret. We make sure
4300    * not to copy it into temporary buffers.
4301    */
4302
4303   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4304
4305   g_object_unref (editable);
4306 }
4307
4308 static void
4309 gtk_entry_delete_text (GtkEditable *editable,
4310                        gint         start_pos,
4311                        gint         end_pos)
4312 {
4313   g_object_ref (editable);
4314
4315   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4316
4317   g_object_unref (editable);
4318 }
4319
4320 static gchar *    
4321 gtk_entry_get_chars      (GtkEditable   *editable,
4322                           gint           start_pos,
4323                           gint           end_pos)
4324 {
4325   GtkEntry *entry = GTK_ENTRY (editable);
4326   const gchar *text;
4327   gint text_length;
4328   gint start_index, end_index;
4329
4330   text = gtk_entry_buffer_get_text (get_buffer (entry));
4331   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4332
4333   if (end_pos < 0)
4334     end_pos = text_length;
4335
4336   start_pos = MIN (text_length, start_pos);
4337   end_pos = MIN (text_length, end_pos);
4338
4339   start_index = g_utf8_offset_to_pointer (text, start_pos) - entry->text;
4340   end_index = g_utf8_offset_to_pointer (text, end_pos) - entry->text;
4341
4342   return g_strndup (text + start_index, end_index - start_index);
4343 }
4344
4345 static void
4346 gtk_entry_real_set_position (GtkEditable *editable,
4347                              gint         position)
4348 {
4349   GtkEntry *entry = GTK_ENTRY (editable);
4350
4351   guint length;
4352
4353   length = gtk_entry_buffer_get_length (get_buffer (entry));
4354   if (position < 0 || position > length)
4355     position = length;
4356
4357   if (position != entry->current_pos ||
4358       position != entry->selection_bound)
4359     {
4360       _gtk_entry_reset_im_context (entry);
4361       gtk_entry_set_positions (entry, position, position);
4362     }
4363 }
4364
4365 static gint
4366 gtk_entry_get_position (GtkEditable *editable)
4367 {
4368   return GTK_ENTRY (editable)->current_pos;
4369 }
4370
4371 static void
4372 gtk_entry_set_selection_bounds (GtkEditable *editable,
4373                                 gint         start,
4374                                 gint         end)
4375 {
4376   GtkEntry *entry = GTK_ENTRY (editable);
4377   guint length;
4378
4379   length = gtk_entry_buffer_get_length (get_buffer (entry));
4380   if (start < 0)
4381     start = length;
4382   if (end < 0)
4383     end = length;
4384   
4385   _gtk_entry_reset_im_context (entry);
4386
4387   gtk_entry_set_positions (entry,
4388                            MIN (end, length),
4389                            MIN (start, length));
4390
4391   gtk_entry_update_primary_selection (entry);
4392 }
4393
4394 static gboolean
4395 gtk_entry_get_selection_bounds (GtkEditable *editable,
4396                                 gint        *start,
4397                                 gint        *end)
4398 {
4399   GtkEntry *entry = GTK_ENTRY (editable);
4400
4401   *start = entry->selection_bound;
4402   *end = entry->current_pos;
4403
4404   return (entry->selection_bound != entry->current_pos);
4405 }
4406
4407 static void
4408 icon_theme_changed (GtkEntry *entry)
4409 {
4410   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4411   gint i;
4412
4413   for (i = 0; i < MAX_ICONS; i++)
4414     {
4415       EntryIconInfo *icon_info = priv->icons[i];
4416       if (icon_info != NULL) 
4417         {
4418           if (icon_info->storage_type == GTK_IMAGE_ICON_NAME)
4419             gtk_entry_set_icon_from_icon_name (entry, i, icon_info->icon_name);
4420           else if (icon_info->storage_type == GTK_IMAGE_STOCK)
4421             gtk_entry_set_icon_from_stock (entry, i, icon_info->stock_id);
4422           else if (icon_info->storage_type == GTK_IMAGE_GICON)
4423             gtk_entry_set_icon_from_gicon (entry, i, icon_info->gicon);
4424         }
4425     }
4426
4427   gtk_widget_queue_draw (GTK_WIDGET (entry));
4428 }
4429
4430 static void
4431 icon_margin_changed (GtkEntry *entry)
4432 {
4433   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4434   GtkBorder border;
4435
4436   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4437
4438   priv->icon_margin = border.left;
4439 }
4440
4441 static void 
4442 gtk_entry_style_set (GtkWidget *widget,
4443                      GtkStyle  *previous_style)
4444 {
4445   GtkEntry *entry = GTK_ENTRY (widget);
4446   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4447   gint focus_width;
4448   gboolean interior_focus;
4449   gint i;
4450
4451   gtk_widget_style_get (widget,
4452                         "focus-line-width", &focus_width,
4453                         "interior-focus", &interior_focus,
4454                         NULL);
4455
4456   priv->focus_width = focus_width;
4457   priv->interior_focus = interior_focus;
4458
4459   if (!priv->invisible_char_set)
4460     entry->invisible_char = find_invisible_char (GTK_WIDGET (entry));
4461
4462   gtk_entry_recompute (entry);
4463
4464   if (previous_style && gtk_widget_get_realized (widget))
4465     {
4466       gdk_window_set_background (widget->window, &widget->style->base[gtk_widget_get_state (widget)]);
4467       gdk_window_set_background (entry->text_area, &widget->style->base[gtk_widget_get_state (widget)]);
4468       for (i = 0; i < MAX_ICONS; i++) 
4469         {
4470           EntryIconInfo *icon_info = priv->icons[i];
4471           if (icon_info && icon_info->window)
4472             gdk_window_set_background (icon_info->window, &widget->style->base[gtk_widget_get_state (widget)]);
4473         }
4474     }
4475
4476   icon_theme_changed (entry);
4477   icon_margin_changed (entry);
4478 }
4479
4480 /* GtkCellEditable method implementations
4481  */
4482 static void
4483 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4484 {
4485   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4486   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4487 }
4488
4489 static gboolean
4490 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4491                                    GdkEventKey *key_event,
4492                                    gpointer     data)
4493 {
4494   if (key_event->keyval == GDK_Escape)
4495     {
4496       entry->editing_canceled = TRUE;
4497       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4498       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4499
4500       return TRUE;
4501     }
4502
4503   /* override focus */
4504   if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
4505     {
4506       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4507       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4508
4509       return TRUE;
4510     }
4511
4512   return FALSE;
4513 }
4514
4515 static void
4516 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4517                          GdkEvent        *event)
4518 {
4519   GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
4520
4521   g_signal_connect (cell_editable, "activate",
4522                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4523   g_signal_connect (cell_editable, "key-press-event",
4524                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4525 }
4526
4527 static void
4528 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4529 {
4530   if (password_hint->source_id)
4531     g_source_remove (password_hint->source_id);
4532
4533   g_slice_free (GtkEntryPasswordHint, password_hint);
4534 }
4535
4536
4537 static gboolean
4538 gtk_entry_remove_password_hint (gpointer data)
4539 {
4540   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4541   password_hint->position = -1;
4542
4543   /* Force the string to be redrawn, but now without a visible character */
4544   gtk_entry_recompute (GTK_ENTRY (data));
4545   return FALSE;
4546 }
4547
4548 /* Default signal handlers
4549  */
4550 static void
4551 gtk_entry_real_insert_text (GtkEditable *editable,
4552                             const gchar *new_text,
4553                             gint         new_text_length,
4554                             gint        *position)
4555 {
4556   guint n_inserted;
4557   gint n_chars;
4558
4559   n_chars = g_utf8_strlen (new_text, new_text_length);
4560
4561   /*
4562    * The actual insertion into the buffer. This will end up firing the
4563    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4564    * buffer_notify_text(), buffer_notify_length()
4565    */
4566   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4567
4568   if (n_inserted != n_chars)
4569       gtk_widget_error_bell (GTK_WIDGET (editable));
4570
4571   *position += n_inserted;
4572 }
4573
4574 static void
4575 gtk_entry_real_delete_text (GtkEditable *editable,
4576                             gint         start_pos,
4577                             gint         end_pos)
4578 {
4579   /*
4580    * The actual deletion from the buffer. This will end up firing the
4581    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4582    * buffer_notify_text(), buffer_notify_length()
4583    */
4584
4585   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4586 }
4587
4588 /* GtkEntryBuffer signal handlers
4589  */
4590 static void
4591 buffer_inserted_text (GtkEntryBuffer *buffer,
4592                       guint           position,
4593                       const gchar    *chars,
4594                       guint           n_chars,
4595                       GtkEntry       *entry)
4596 {
4597   guint password_hint_timeout;
4598
4599   if (entry->current_pos > position)
4600     entry->current_pos += n_chars;
4601
4602   if (entry->selection_bound > position)
4603     entry->selection_bound += n_chars;
4604
4605   /* Calculate the password hint if it needs to be displayed. */
4606   if (n_chars == 1 && !entry->visible)
4607     {
4608       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4609                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4610                     NULL);
4611
4612       if (password_hint_timeout > 0)
4613         {
4614           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4615                                                                     quark_password_hint);
4616           if (!password_hint)
4617             {
4618               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4619               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4620                                        (GDestroyNotify)gtk_entry_password_hint_free);
4621             }
4622
4623           password_hint->position = position;
4624           if (password_hint->source_id)
4625             g_source_remove (password_hint->source_id);
4626           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4627                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4628         }
4629     }
4630 }
4631
4632 static void
4633 buffer_deleted_text (GtkEntryBuffer *buffer,
4634                      guint           position,
4635                      guint           n_chars,
4636                      GtkEntry       *entry)
4637 {
4638   guint end_pos = position + n_chars;
4639   gint selection_bound;
4640   guint current_pos;
4641
4642   current_pos = entry->current_pos;
4643   if (current_pos > position)
4644     current_pos -= MIN (current_pos, end_pos) - position;
4645
4646   selection_bound = entry->selection_bound;
4647   if (selection_bound > position)
4648     selection_bound -= MIN (selection_bound, end_pos) - position;
4649
4650   gtk_entry_set_positions (entry, current_pos, selection_bound);
4651
4652   /* We might have deleted the selection */
4653   gtk_entry_update_primary_selection (entry);
4654
4655   /* Disable the password hint if one exists. */
4656   if (!entry->visible)
4657     {
4658       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4659                                                                 quark_password_hint);
4660       if (password_hint)
4661         {
4662           if (password_hint->source_id)
4663             g_source_remove (password_hint->source_id);
4664           password_hint->source_id = 0;
4665           password_hint->position = -1;
4666         }
4667     }
4668 }
4669
4670 static void
4671 buffer_notify_text (GtkEntryBuffer *buffer,
4672                     GParamSpec     *spec,
4673                     GtkEntry       *entry)
4674 {
4675   /* COMPAT: Deprecated, not used. This struct field will be removed in GTK+ 3.x */
4676   entry->text = (gchar*)gtk_entry_buffer_get_text (buffer);
4677
4678   gtk_entry_recompute (entry);
4679   emit_changed (entry);
4680   g_object_notify (G_OBJECT (entry), "text");
4681 }
4682
4683 static void
4684 buffer_notify_length (GtkEntryBuffer *buffer,
4685                       GParamSpec     *spec,
4686                       GtkEntry       *entry)
4687 {
4688   /* COMPAT: Deprecated, not used. This struct field will be removed in GTK+ 3.x */
4689   entry->text_length = gtk_entry_buffer_get_length (buffer);
4690
4691   g_object_notify (G_OBJECT (entry), "text-length");
4692 }
4693
4694 static void
4695 buffer_notify_max_length (GtkEntryBuffer *buffer,
4696                           GParamSpec     *spec,
4697                           GtkEntry       *entry)
4698 {
4699   /* COMPAT: Deprecated, not used. This struct field will be removed in GTK+ 3.x */
4700   entry->text_max_length = gtk_entry_buffer_get_max_length (buffer);
4701
4702   g_object_notify (G_OBJECT (entry), "max-length");
4703 }
4704
4705 static void
4706 buffer_connect_signals (GtkEntry *entry)
4707 {
4708   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4709   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4710   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4711   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4712   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4713 }
4714
4715 static void
4716 buffer_disconnect_signals (GtkEntry *entry)
4717 {
4718   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4719   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4720   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4721   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4722   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4723 }
4724
4725 /* Compute the X position for an offset that corresponds to the "more important
4726  * cursor position for that offset. We use this when trying to guess to which
4727  * end of the selection we should go to when the user hits the left or
4728  * right arrow key.
4729  */
4730 static gint
4731 get_better_cursor_x (GtkEntry *entry,
4732                      gint      offset)
4733 {
4734   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4735   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4736   gboolean split_cursor;
4737   
4738   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4739   const gchar *text = pango_layout_get_text (layout);
4740   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4741   
4742   PangoRectangle strong_pos, weak_pos;
4743   
4744   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4745                 "gtk-split-cursor", &split_cursor,
4746                 NULL);
4747
4748   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4749
4750   if (split_cursor)
4751     return strong_pos.x / PANGO_SCALE;
4752   else
4753     return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4754 }
4755
4756 static void
4757 gtk_entry_move_cursor (GtkEntry       *entry,
4758                        GtkMovementStep step,
4759                        gint            count,
4760                        gboolean        extend_selection)
4761 {
4762   gint new_pos = entry->current_pos;
4763   GtkEntryPrivate *priv;
4764
4765   _gtk_entry_reset_im_context (entry);
4766
4767   if (entry->current_pos != entry->selection_bound && !extend_selection)
4768     {
4769       /* If we have a current selection and aren't extending it, move to the
4770        * start/or end of the selection as appropriate
4771        */
4772       switch (step)
4773         {
4774         case GTK_MOVEMENT_VISUAL_POSITIONS:
4775           {
4776             gint current_x = get_better_cursor_x (entry, entry->current_pos);
4777             gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
4778
4779             if (count <= 0)
4780               new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
4781             else 
4782               new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
4783             break;
4784           }
4785         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4786         case GTK_MOVEMENT_WORDS:
4787           if (count < 0)
4788             new_pos = MIN (entry->current_pos, entry->selection_bound);
4789           else
4790             new_pos = MAX (entry->current_pos, entry->selection_bound);
4791           break;
4792         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4793         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4794         case GTK_MOVEMENT_BUFFER_ENDS:
4795           priv = GTK_ENTRY_GET_PRIVATE (entry);
4796           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4797           break;
4798         case GTK_MOVEMENT_DISPLAY_LINES:
4799         case GTK_MOVEMENT_PARAGRAPHS:
4800         case GTK_MOVEMENT_PAGES:
4801         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4802           break;
4803         }
4804     }
4805   else
4806     {
4807       switch (step)
4808         {
4809         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4810           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4811           break;
4812         case GTK_MOVEMENT_VISUAL_POSITIONS:
4813           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4814           if (entry->current_pos == new_pos)
4815             {
4816               if (!extend_selection)
4817                 {
4818                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4819                                                  count > 0 ?
4820                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4821                     {
4822                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4823
4824                       if (toplevel)
4825                         gtk_widget_child_focus (toplevel,
4826                                                 count > 0 ?
4827                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4828                     }
4829                 }
4830               else
4831                 {
4832                   gtk_widget_error_bell (GTK_WIDGET (entry));
4833                 }
4834             }
4835           break;
4836         case GTK_MOVEMENT_WORDS:
4837           while (count > 0)
4838             {
4839               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4840               count--;
4841             }
4842           while (count < 0)
4843             {
4844               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4845               count++;
4846             }
4847           if (entry->current_pos == new_pos)
4848             gtk_widget_error_bell (GTK_WIDGET (entry));
4849           break;
4850         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4851         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4852         case GTK_MOVEMENT_BUFFER_ENDS:
4853           priv = GTK_ENTRY_GET_PRIVATE (entry);
4854           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4855           if (entry->current_pos == new_pos)
4856             gtk_widget_error_bell (GTK_WIDGET (entry));
4857           break;
4858         case GTK_MOVEMENT_DISPLAY_LINES:
4859         case GTK_MOVEMENT_PARAGRAPHS:
4860         case GTK_MOVEMENT_PAGES:
4861         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4862           break;
4863         }
4864     }
4865
4866   if (extend_selection)
4867     gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
4868   else
4869     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4870   
4871   gtk_entry_pend_cursor_blink (entry);
4872 }
4873
4874 static void
4875 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4876                             const gchar *str)
4877 {
4878   GtkEditable *editable = GTK_EDITABLE (entry);
4879   gint pos = entry->current_pos;
4880
4881   if (entry->editable)
4882     {
4883       _gtk_entry_reset_im_context (entry);
4884
4885       gtk_editable_insert_text (editable, str, -1, &pos);
4886       gtk_editable_set_position (editable, pos);
4887     }
4888 }
4889
4890 static void
4891 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4892                               GtkDeleteType   type,
4893                               gint            count)
4894 {
4895   GtkEditable *editable = GTK_EDITABLE (entry);
4896   gint start_pos = entry->current_pos;
4897   gint end_pos = entry->current_pos;
4898   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
4899   
4900   _gtk_entry_reset_im_context (entry);
4901
4902   if (!entry->editable)
4903     {
4904       gtk_widget_error_bell (GTK_WIDGET (entry));
4905       return;
4906     }
4907
4908   if (entry->selection_bound != entry->current_pos)
4909     {
4910       gtk_editable_delete_selection (editable);
4911       return;
4912     }
4913   
4914   switch (type)
4915     {
4916     case GTK_DELETE_CHARS:
4917       end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
4918       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
4919       break;
4920     case GTK_DELETE_WORDS:
4921       if (count < 0)
4922         {
4923           /* Move to end of current word, or if not on a word, end of previous word */
4924           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
4925           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4926         }
4927       else if (count > 0)
4928         {
4929           /* Move to beginning of current word, or if not on a word, begining of next word */
4930           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
4931           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4932         }
4933         
4934       /* Fall through */
4935     case GTK_DELETE_WORD_ENDS:
4936       while (count < 0)
4937         {
4938           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4939           count++;
4940         }
4941       while (count > 0)
4942         {
4943           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4944           count--;
4945         }
4946       gtk_editable_delete_text (editable, start_pos, end_pos);
4947       break;
4948     case GTK_DELETE_DISPLAY_LINE_ENDS:
4949     case GTK_DELETE_PARAGRAPH_ENDS:
4950       if (count < 0)
4951         gtk_editable_delete_text (editable, 0, entry->current_pos);
4952       else
4953         gtk_editable_delete_text (editable, entry->current_pos, -1);
4954       break;
4955     case GTK_DELETE_DISPLAY_LINES:
4956     case GTK_DELETE_PARAGRAPHS:
4957       gtk_editable_delete_text (editable, 0, -1);  
4958       break;
4959     case GTK_DELETE_WHITESPACE:
4960       gtk_entry_delete_whitespace (entry);
4961       break;
4962     }
4963
4964   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
4965     gtk_widget_error_bell (GTK_WIDGET (entry));
4966
4967   gtk_entry_pend_cursor_blink (entry);
4968 }
4969
4970 static void
4971 gtk_entry_backspace (GtkEntry *entry)
4972 {
4973   GtkEditable *editable = GTK_EDITABLE (entry);
4974   gint prev_pos;
4975
4976   _gtk_entry_reset_im_context (entry);
4977
4978   if (!entry->editable)
4979     {
4980       gtk_widget_error_bell (GTK_WIDGET (entry));
4981       return;
4982     }
4983
4984   if (entry->selection_bound != entry->current_pos)
4985     {
4986       gtk_editable_delete_selection (editable);
4987       return;
4988     }
4989
4990   prev_pos = gtk_entry_move_logically (entry, entry->current_pos, -1);
4991
4992   if (prev_pos < entry->current_pos)
4993     {
4994       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4995       PangoLogAttr *log_attrs;
4996       gint n_attrs;
4997
4998       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4999
5000       /* Deleting parts of characters */
5001       if (log_attrs[entry->current_pos].backspace_deletes_character)
5002         {
5003           gchar *cluster_text;
5004           gchar *normalized_text;
5005           glong  len;
5006
5007           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5008                                                      entry->current_pos);
5009           normalized_text = g_utf8_normalize (cluster_text,
5010                                               strlen (cluster_text),
5011                                               G_NORMALIZE_NFD);
5012           len = g_utf8_strlen (normalized_text, -1);
5013
5014           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
5015           if (len > 1)
5016             {
5017               gint pos = entry->current_pos;
5018
5019               gtk_editable_insert_text (editable, normalized_text,
5020                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5021                                         &pos);
5022               gtk_editable_set_position (editable, pos);
5023             }
5024
5025           g_free (normalized_text);
5026           g_free (cluster_text);
5027         }
5028       else
5029         {
5030           gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
5031         }
5032       
5033       g_free (log_attrs);
5034     }
5035   else
5036     {
5037       gtk_widget_error_bell (GTK_WIDGET (entry));
5038     }
5039
5040   gtk_entry_pend_cursor_blink (entry);
5041 }
5042
5043 static void
5044 gtk_entry_copy_clipboard (GtkEntry *entry)
5045 {
5046   GtkEditable *editable = GTK_EDITABLE (entry);
5047   gint start, end;
5048   gchar *str;
5049
5050   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5051     {
5052       if (!entry->visible)
5053         {
5054           gtk_widget_error_bell (GTK_WIDGET (entry));
5055           return;
5056         }
5057
5058       str = gtk_entry_get_display_text (entry, start, end);
5059       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5060                                                         GDK_SELECTION_CLIPBOARD),
5061                               str, -1);
5062       g_free (str);
5063     }
5064 }
5065
5066 static void
5067 gtk_entry_cut_clipboard (GtkEntry *entry)
5068 {
5069   GtkEditable *editable = GTK_EDITABLE (entry);
5070   gint start, end;
5071
5072   if (!entry->visible)
5073     {
5074       gtk_widget_error_bell (GTK_WIDGET (entry));
5075       return;
5076     }
5077
5078   gtk_entry_copy_clipboard (entry);
5079
5080   if (entry->editable)
5081     {
5082       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5083         gtk_editable_delete_text (editable, start, end);
5084     }
5085   else
5086     {
5087       gtk_widget_error_bell (GTK_WIDGET (entry));
5088     }
5089 }
5090
5091 static void
5092 gtk_entry_paste_clipboard (GtkEntry *entry)
5093 {
5094   if (entry->editable)
5095     gtk_entry_paste (entry, GDK_NONE);
5096   else
5097     gtk_widget_error_bell (GTK_WIDGET (entry));
5098 }
5099
5100 static void
5101 gtk_entry_delete_cb (GtkEntry *entry)
5102 {
5103   GtkEditable *editable = GTK_EDITABLE (entry);
5104   gint start, end;
5105
5106   if (entry->editable)
5107     {
5108       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5109         gtk_editable_delete_text (editable, start, end);
5110     }
5111 }
5112
5113 static void
5114 gtk_entry_toggle_overwrite (GtkEntry *entry)
5115 {
5116   entry->overwrite_mode = !entry->overwrite_mode;
5117   gtk_entry_pend_cursor_blink (entry);
5118   gtk_widget_queue_draw (GTK_WIDGET (entry));
5119 }
5120
5121 static void
5122 gtk_entry_select_all (GtkEntry *entry)
5123 {
5124   gtk_entry_select_line (entry);
5125 }
5126
5127 static void
5128 gtk_entry_real_activate (GtkEntry *entry)
5129 {
5130   GtkWindow *window;
5131   GtkWidget *toplevel;
5132   GtkWidget *widget;
5133
5134   widget = GTK_WIDGET (entry);
5135
5136   if (entry->activates_default)
5137     {
5138       toplevel = gtk_widget_get_toplevel (widget);
5139       if (GTK_IS_WINDOW (toplevel))
5140         {
5141           window = GTK_WINDOW (toplevel);
5142       
5143           if (window &&
5144               widget != window->default_widget &&
5145               !(widget == window->focus_widget &&
5146                 (!window->default_widget || !gtk_widget_get_sensitive (window->default_widget))))
5147             gtk_window_activate_default (window);
5148         }
5149     }
5150 }
5151
5152 static void
5153 keymap_direction_changed (GdkKeymap *keymap,
5154                           GtkEntry  *entry)
5155 {
5156   gtk_entry_recompute (entry);
5157 }
5158
5159 /* IM Context Callbacks
5160  */
5161
5162 static void
5163 gtk_entry_commit_cb (GtkIMContext *context,
5164                      const gchar  *str,
5165                      GtkEntry     *entry)
5166 {
5167   if (entry->editable)
5168     gtk_entry_enter_text (entry, str);
5169 }
5170
5171 static void 
5172 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5173                               GtkEntry     *entry)
5174 {
5175   if (entry->editable)
5176     {
5177       gchar *preedit_string;
5178       gint cursor_pos;
5179       
5180       gtk_im_context_get_preedit_string (entry->im_context,
5181                                          &preedit_string, NULL,
5182                                          &cursor_pos);
5183       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5184       entry->preedit_length = strlen (preedit_string);
5185       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5186       entry->preedit_cursor = cursor_pos;
5187       g_free (preedit_string);
5188     
5189       gtk_entry_recompute (entry);
5190     }
5191 }
5192
5193 static gboolean
5194 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5195                                GtkEntry         *entry)
5196 {
5197   gchar *text;
5198
5199   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5200   text = gtk_entry_get_display_text (entry, 0, -1);
5201   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5202                                   g_utf8_offset_to_pointer (text, entry->current_pos) - text);
5203   g_free (text);
5204
5205   return TRUE;
5206 }
5207
5208 static gboolean
5209 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5210                                  gint          offset,
5211                                  gint          n_chars,
5212                                  GtkEntry     *entry)
5213 {
5214   if (entry->editable)
5215     gtk_editable_delete_text (GTK_EDITABLE (entry),
5216                               entry->current_pos + offset,
5217                               entry->current_pos + offset + n_chars);
5218
5219   return TRUE;
5220 }
5221
5222 /* Internal functions
5223  */
5224
5225 /* Used for im_commit_cb and inserting Unicode chars */
5226 static void
5227 gtk_entry_enter_text (GtkEntry       *entry,
5228                       const gchar    *str)
5229 {
5230   GtkEditable *editable = GTK_EDITABLE (entry);
5231   gint tmp_pos;
5232   gboolean old_need_im_reset;
5233
5234   old_need_im_reset = entry->need_im_reset;
5235   entry->need_im_reset = FALSE;
5236
5237   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5238     gtk_editable_delete_selection (editable);
5239   else
5240     {
5241       if (entry->overwrite_mode)
5242         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5243     }
5244
5245   tmp_pos = entry->current_pos;
5246   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5247   gtk_editable_set_position (editable, tmp_pos);
5248
5249   entry->need_im_reset = old_need_im_reset;
5250 }
5251
5252 /* All changes to entry->current_pos and entry->selection_bound
5253  * should go through this function.
5254  */
5255 static void
5256 gtk_entry_set_positions (GtkEntry *entry,
5257                          gint      current_pos,
5258                          gint      selection_bound)
5259 {
5260   gboolean changed = FALSE;
5261
5262   g_object_freeze_notify (G_OBJECT (entry));
5263   
5264   if (current_pos != -1 &&
5265       entry->current_pos != current_pos)
5266     {
5267       entry->current_pos = current_pos;
5268       changed = TRUE;
5269
5270       g_object_notify (G_OBJECT (entry), "cursor-position");
5271     }
5272
5273   if (selection_bound != -1 &&
5274       entry->selection_bound != selection_bound)
5275     {
5276       entry->selection_bound = selection_bound;
5277       changed = TRUE;
5278       
5279       g_object_notify (G_OBJECT (entry), "selection-bound");
5280     }
5281
5282   g_object_thaw_notify (G_OBJECT (entry));
5283
5284   if (changed) 
5285     {
5286       gtk_entry_move_adjustments (entry);
5287       gtk_entry_recompute (entry);
5288     }
5289 }
5290
5291 static void
5292 gtk_entry_reset_layout (GtkEntry *entry)
5293 {
5294   if (entry->cached_layout)
5295     {
5296       g_object_unref (entry->cached_layout);
5297       entry->cached_layout = NULL;
5298     }
5299 }
5300
5301 static void
5302 update_im_cursor_location (GtkEntry *entry)
5303 {
5304   GdkRectangle area;
5305   gint strong_x;
5306   gint strong_xoffset;
5307   gint area_width, area_height;
5308
5309   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5310   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5311
5312   strong_xoffset = strong_x - entry->scroll_offset;
5313   if (strong_xoffset < 0)
5314     {
5315       strong_xoffset = 0;
5316     }
5317   else if (strong_xoffset > area_width)
5318     {
5319       strong_xoffset = area_width;
5320     }
5321   area.x = strong_xoffset;
5322   area.y = 0;
5323   area.width = 0;
5324   area.height = area_height;
5325
5326   gtk_im_context_set_cursor_location (entry->im_context, &area);
5327 }
5328
5329 static gboolean
5330 recompute_idle_func (gpointer data)
5331 {
5332   GtkEntry *entry;
5333
5334   entry = GTK_ENTRY (data);
5335
5336   entry->recompute_idle = 0;
5337   
5338   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5339     {
5340       gtk_entry_adjust_scroll (entry);
5341       gtk_entry_queue_draw (entry);
5342       
5343       update_im_cursor_location (entry);
5344     }
5345
5346   return FALSE;
5347 }
5348
5349 static void
5350 gtk_entry_recompute (GtkEntry *entry)
5351 {
5352   gtk_entry_reset_layout (entry);
5353   gtk_entry_check_cursor_blink (entry);
5354   
5355   if (!entry->recompute_idle)
5356     {
5357       entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5358                                                recompute_idle_func, entry, NULL); 
5359     }
5360 }
5361
5362 static PangoLayout *
5363 gtk_entry_create_layout (GtkEntry *entry,
5364                          gboolean  include_preedit)
5365 {
5366   GtkWidget *widget = GTK_WIDGET (entry);
5367   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5368   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5369
5370   gchar *preedit_string = NULL;
5371   gint preedit_length = 0;
5372   PangoAttrList *preedit_attrs = NULL;
5373
5374   gchar *display;
5375   guint n_bytes;
5376
5377   pango_layout_set_single_paragraph_mode (layout, TRUE);
5378
5379   display = gtk_entry_get_display_text (entry, 0, -1);
5380   n_bytes = strlen (display);
5381
5382   if (include_preedit)
5383     {
5384       gtk_im_context_get_preedit_string (entry->im_context,
5385                                          &preedit_string, &preedit_attrs, NULL);
5386       preedit_length = entry->preedit_length;
5387     }
5388
5389   if (preedit_length)
5390     {
5391       GString *tmp_string = g_string_new (display);
5392       gint cursor_index = g_utf8_offset_to_pointer (display, entry->current_pos) - display;
5393       
5394       g_string_insert (tmp_string, cursor_index, preedit_string);
5395       
5396       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5397       
5398       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5399                               cursor_index, preedit_length);
5400       
5401       g_string_free (tmp_string, TRUE);
5402     }
5403   else
5404     {
5405       PangoDirection pango_dir;
5406       
5407       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5408         pango_dir = pango_find_base_dir (display, n_bytes);
5409       else
5410         pango_dir = PANGO_DIRECTION_NEUTRAL;
5411
5412       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5413         {
5414           if (gtk_widget_has_focus (widget))
5415             {
5416               GdkDisplay *display = gtk_widget_get_display (widget);
5417               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5418               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5419                 pango_dir = PANGO_DIRECTION_RTL;
5420               else
5421                 pango_dir = PANGO_DIRECTION_LTR;
5422             }
5423           else
5424             {
5425               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5426                 pango_dir = PANGO_DIRECTION_RTL;
5427               else
5428                 pango_dir = PANGO_DIRECTION_LTR;
5429             }
5430         }
5431
5432       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5433                                   pango_dir);
5434
5435       entry->resolved_dir = pango_dir;
5436
5437       pango_layout_set_text (layout, display, n_bytes);
5438     }
5439       
5440   pango_layout_set_attributes (layout, tmp_attrs);
5441
5442   g_free (preedit_string);
5443   g_free (display);
5444
5445   if (preedit_attrs)
5446     pango_attr_list_unref (preedit_attrs);
5447       
5448   pango_attr_list_unref (tmp_attrs);
5449
5450   return layout;
5451 }
5452
5453 static PangoLayout *
5454 gtk_entry_ensure_layout (GtkEntry *entry,
5455                          gboolean  include_preedit)
5456 {
5457   if (entry->preedit_length > 0 &&
5458       !include_preedit != !entry->cache_includes_preedit)
5459     gtk_entry_reset_layout (entry);
5460
5461   if (!entry->cached_layout)
5462     {
5463       entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5464       entry->cache_includes_preedit = include_preedit;
5465     }
5466   
5467   return entry->cached_layout;
5468 }
5469
5470 static void
5471 get_layout_position (GtkEntry *entry,
5472                      gint     *x,
5473                      gint     *y)
5474 {
5475   PangoLayout *layout;
5476   PangoRectangle logical_rect;
5477   gint area_width, area_height;
5478   GtkBorder inner_border;
5479   gint y_pos;
5480   PangoLayoutLine *line;
5481   
5482   layout = gtk_entry_ensure_layout (entry, TRUE);
5483
5484   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5485   _gtk_entry_effective_inner_border (entry, &inner_border);
5486
5487   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5488
5489   line = pango_layout_get_lines_readonly (layout)->data;
5490   pango_layout_line_get_extents (line, NULL, &logical_rect);
5491   
5492   /* Align primarily for locale's ascent/descent */
5493   y_pos = ((area_height - entry->ascent - entry->descent) / 2 + 
5494            entry->ascent + logical_rect.y);
5495   
5496   /* Now see if we need to adjust to fit in actual drawn string */
5497   if (logical_rect.height > area_height)
5498     y_pos = (area_height - logical_rect.height) / 2;
5499   else if (y_pos < 0)
5500     y_pos = 0;
5501   else if (y_pos + logical_rect.height > area_height)
5502     y_pos = area_height - logical_rect.height;
5503   
5504   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5505
5506   if (x)
5507     *x = inner_border.left - entry->scroll_offset;
5508
5509   if (y)
5510     *y = y_pos;
5511 }
5512
5513 static void
5514 draw_text_with_color (GtkEntry *entry, cairo_t *cr, GdkColor *default_color)
5515 {
5516   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5517   GtkWidget *widget;
5518   gint x, y;
5519   gint start_pos, end_pos;
5520
5521   widget = GTK_WIDGET (entry);
5522
5523   cairo_save (cr);
5524
5525   get_layout_position (entry, &x, &y);
5526
5527   cairo_move_to (cr, x, y);
5528   gdk_cairo_set_source_color (cr, default_color);
5529   pango_cairo_show_layout (cr, layout);
5530
5531   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5532     {
5533       gint *ranges;
5534       gint n_ranges, i;
5535       PangoRectangle logical_rect;
5536       GdkColor *selection_color, *text_color;
5537       GtkBorder inner_border;
5538
5539       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5540       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5541
5542       if (gtk_widget_has_focus (widget))
5543         {
5544           selection_color = &widget->style->base [GTK_STATE_SELECTED];
5545           text_color = &widget->style->text [GTK_STATE_SELECTED];
5546         }
5547       else
5548         {
5549           selection_color = &widget->style->base [GTK_STATE_ACTIVE];
5550           text_color = &widget->style->text [GTK_STATE_ACTIVE];
5551         }
5552
5553       _gtk_entry_effective_inner_border (entry, &inner_border);
5554
5555       for (i = 0; i < n_ranges; ++i)
5556         cairo_rectangle (cr,
5557                          inner_border.left - entry->scroll_offset + ranges[2 * i],
5558                          y,
5559                          ranges[2 * i + 1],
5560                          logical_rect.height);
5561
5562       cairo_clip (cr);
5563           
5564       gdk_cairo_set_source_color (cr, selection_color);
5565       cairo_paint (cr);
5566
5567       cairo_move_to (cr, x, y);
5568       gdk_cairo_set_source_color (cr, text_color);
5569       pango_cairo_show_layout (cr, layout);
5570   
5571       g_free (ranges);
5572     }
5573   cairo_restore (cr);
5574 }
5575
5576 static void
5577 gtk_entry_draw_text (GtkEntry *entry)
5578 {
5579   GtkWidget *widget = GTK_WIDGET (entry);
5580   cairo_t *cr;
5581
5582   /* Nothing to display at all */
5583   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5584     return;
5585   
5586   if (gtk_widget_is_drawable (widget))
5587     {
5588       GdkColor text_color, bar_text_color;
5589       gint pos_x, pos_y;
5590       gint width, height;
5591       gint progress_x, progress_y, progress_width, progress_height;
5592       GtkStateType state;
5593
5594       state = GTK_STATE_SELECTED;
5595       if (!gtk_widget_get_sensitive (widget))
5596         state = GTK_STATE_INSENSITIVE;
5597       text_color = widget->style->text[widget->state];
5598       bar_text_color = widget->style->fg[state];
5599
5600       get_progress_area (widget,
5601                          &progress_x, &progress_y,
5602                          &progress_width, &progress_height);
5603
5604       cr = gdk_cairo_create (entry->text_area);
5605
5606       /* If the color is the same, or the progress area has a zero
5607        * size, then we only need to draw once. */
5608       if ((text_color.pixel == bar_text_color.pixel) ||
5609           ((progress_width == 0) || (progress_height == 0)))
5610         {
5611           draw_text_with_color (entry, cr, &text_color);
5612         }
5613       else
5614         {
5615           gdk_drawable_get_size (entry->text_area, &width, &height);
5616
5617           cairo_rectangle (cr, 0, 0, width, height);
5618           cairo_clip (cr);
5619           cairo_save (cr);
5620
5621           cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5622           cairo_rectangle (cr, 0, 0, width, height);
5623
5624           gdk_window_get_position (entry->text_area, &pos_x, &pos_y);
5625           progress_x -= pos_x;
5626           progress_y -= pos_y;
5627
5628           cairo_rectangle (cr, progress_x, progress_y,
5629                            progress_width, progress_height);
5630           cairo_clip (cr);
5631           cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5632       
5633           draw_text_with_color (entry, cr, &text_color);
5634           cairo_restore (cr);
5635
5636           cairo_rectangle (cr, progress_x, progress_y,
5637                            progress_width, progress_height);
5638           cairo_clip (cr);
5639
5640           draw_text_with_color (entry, cr, &bar_text_color);
5641         }
5642
5643       cairo_destroy (cr);
5644     }
5645 }
5646
5647 static void
5648 draw_insertion_cursor (GtkEntry      *entry,
5649                        GdkRectangle  *cursor_location,
5650                        gboolean       is_primary,
5651                        PangoDirection direction,
5652                        gboolean       draw_arrow)
5653 {
5654   GtkWidget *widget = GTK_WIDGET (entry);
5655   GtkTextDirection text_dir;
5656
5657   if (direction == PANGO_DIRECTION_LTR)
5658     text_dir = GTK_TEXT_DIR_LTR;
5659   else
5660     text_dir = GTK_TEXT_DIR_RTL;
5661
5662   gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
5663                              cursor_location,
5664                              is_primary, text_dir, draw_arrow);
5665 }
5666
5667 static void
5668 gtk_entry_draw_cursor (GtkEntry  *entry,
5669                        CursorType type)
5670 {
5671   GtkWidget *widget = GTK_WIDGET (entry);
5672   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5673   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5674   
5675   if (gtk_widget_is_drawable (widget))
5676     {
5677       GdkRectangle cursor_location;
5678       gboolean split_cursor;
5679       PangoRectangle cursor_rect;
5680       GtkBorder inner_border;
5681       gint xoffset;
5682       gint text_area_height;
5683       gint cursor_index;
5684       gboolean block;
5685       gboolean block_at_line_end;
5686       PangoLayout *layout;
5687       const char *text;
5688
5689       _gtk_entry_effective_inner_border (entry, &inner_border);
5690
5691       xoffset = inner_border.left - entry->scroll_offset;
5692
5693       gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
5694
5695       layout = gtk_entry_ensure_layout (entry, TRUE);
5696       text = pango_layout_get_text (layout);
5697       cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5698       if (!entry->overwrite_mode)
5699         block = FALSE;
5700       else
5701         block = _gtk_text_util_get_block_cursor_location (layout,
5702                                                           cursor_index, &cursor_rect, &block_at_line_end);
5703
5704       if (!block)
5705         {
5706           gint strong_x, weak_x;
5707           PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
5708           PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
5709           gint x1 = 0;
5710           gint x2 = 0;
5711
5712           gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
5713
5714           g_object_get (gtk_widget_get_settings (widget),
5715                         "gtk-split-cursor", &split_cursor,
5716                         NULL);
5717
5718           dir1 = entry->resolved_dir;
5719       
5720           if (split_cursor)
5721             {
5722               x1 = strong_x;
5723
5724               if (weak_x != strong_x)
5725                 {
5726                   dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
5727                   x2 = weak_x;
5728                 }
5729             }
5730           else
5731             {
5732               if (keymap_direction == entry->resolved_dir)
5733                 x1 = strong_x;
5734               else
5735                 x1 = weak_x;
5736             }
5737
5738           cursor_location.x = xoffset + x1;
5739           cursor_location.y = inner_border.top;
5740           cursor_location.width = 0;
5741           cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
5742
5743           draw_insertion_cursor (entry,
5744                                  &cursor_location, TRUE, dir1,
5745                                  dir2 != PANGO_DIRECTION_NEUTRAL);
5746       
5747           if (dir2 != PANGO_DIRECTION_NEUTRAL)
5748             {
5749               cursor_location.x = xoffset + x2;
5750               draw_insertion_cursor (entry,
5751                                      &cursor_location, FALSE, dir2,
5752                                      TRUE);
5753             }
5754         }
5755       else /* overwrite_mode */
5756         {
5757           GdkColor cursor_color;
5758           GdkRectangle rect;
5759           cairo_t *cr;
5760           gint x, y;
5761
5762           get_layout_position (entry, &x, &y);
5763
5764           rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5765           rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5766           rect.width = PANGO_PIXELS (cursor_rect.width);
5767           rect.height = PANGO_PIXELS (cursor_rect.height);
5768
5769           cr = gdk_cairo_create (entry->text_area);
5770
5771           _gtk_widget_get_cursor_color (widget, &cursor_color);
5772           gdk_cairo_set_source_color (cr, &cursor_color);
5773           gdk_cairo_rectangle (cr, &rect);
5774           cairo_fill (cr);
5775
5776           if (!block_at_line_end)
5777             {
5778               gdk_cairo_rectangle (cr, &rect);
5779               cairo_clip (cr);
5780               cairo_move_to (cr, x, y);
5781               gdk_cairo_set_source_color (cr, &widget->style->base[widget->state]);
5782               pango_cairo_show_layout (cr, layout);
5783             }
5784
5785           cairo_destroy (cr);
5786         }
5787     }
5788 }
5789
5790 static void
5791 gtk_entry_queue_draw (GtkEntry *entry)
5792 {
5793   if (gtk_widget_is_drawable (GTK_WIDGET (entry)))
5794     gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
5795 }
5796
5797 void
5798 _gtk_entry_reset_im_context (GtkEntry *entry)
5799 {
5800   if (entry->need_im_reset)
5801     {
5802       entry->need_im_reset = FALSE;
5803       gtk_im_context_reset (entry->im_context);
5804     }
5805 }
5806
5807 static gint
5808 gtk_entry_find_position (GtkEntry *entry,
5809                          gint      x)
5810 {
5811   PangoLayout *layout;
5812   PangoLayoutLine *line;
5813   gint index;
5814   gint pos;
5815   gint trailing;
5816   const gchar *text;
5817   gint cursor_index;
5818   
5819   layout = gtk_entry_ensure_layout (entry, TRUE);
5820   text = pango_layout_get_text (layout);
5821   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
5822   
5823   line = pango_layout_get_lines_readonly (layout)->data;
5824   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
5825
5826   if (index >= cursor_index && entry->preedit_length)
5827     {
5828       if (index >= cursor_index + entry->preedit_length)
5829         index -= entry->preedit_length;
5830       else
5831         {
5832           index = cursor_index;
5833           trailing = 0;
5834         }
5835     }
5836
5837   pos = g_utf8_pointer_to_offset (text, text + index);
5838   pos += trailing;
5839
5840   return pos;
5841 }
5842
5843 static void
5844 gtk_entry_get_cursor_locations (GtkEntry   *entry,
5845                                 CursorType  type,
5846                                 gint       *strong_x,
5847                                 gint       *weak_x)
5848 {
5849   DisplayMode mode = gtk_entry_get_display_mode (entry);
5850
5851   /* Nothing to display at all, so no cursor is relevant */
5852   if (mode == DISPLAY_BLANK)
5853     {
5854       if (strong_x)
5855         *strong_x = 0;
5856       
5857       if (weak_x)
5858         *weak_x = 0;
5859     }
5860   else
5861     {
5862       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5863       const gchar *text = pango_layout_get_text (layout);
5864       PangoRectangle strong_pos, weak_pos;
5865       gint index;
5866   
5867       if (type == CURSOR_STANDARD)
5868         {
5869           index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5870         }
5871       else /* type == CURSOR_DND */
5872         {
5873           index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
5874
5875           if (entry->dnd_position > entry->current_pos)
5876             {
5877               if (mode == DISPLAY_NORMAL)
5878                 index += entry->preedit_length;
5879               else
5880                 {
5881                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
5882                   index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
5883                 }
5884             }
5885         }
5886       
5887       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
5888       
5889       if (strong_x)
5890         *strong_x = strong_pos.x / PANGO_SCALE;
5891       
5892       if (weak_x)
5893         *weak_x = weak_pos.x / PANGO_SCALE;
5894     }
5895 }
5896
5897 static void
5898 gtk_entry_adjust_scroll (GtkEntry *entry)
5899 {
5900   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5901   gint min_offset, max_offset;
5902   gint text_area_width, text_width;
5903   GtkBorder inner_border;
5904   gint strong_x, weak_x;
5905   gint strong_xoffset, weak_xoffset;
5906   gfloat xalign;
5907   PangoLayout *layout;
5908   PangoLayoutLine *line;
5909   PangoRectangle logical_rect;
5910
5911   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
5912     return;
5913
5914   _gtk_entry_effective_inner_border (entry, &inner_border);
5915
5916   gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
5917   text_area_width -= inner_border.left + inner_border.right;
5918   if (text_area_width < 0)
5919     text_area_width = 0;
5920
5921   layout = gtk_entry_ensure_layout (entry, TRUE);
5922   line = pango_layout_get_lines_readonly (layout)->data;
5923
5924   pango_layout_line_get_extents (line, NULL, &logical_rect);
5925
5926   /* Display as much text as we can */
5927
5928   if (entry->resolved_dir == PANGO_DIRECTION_LTR)
5929       xalign = priv->xalign;
5930   else
5931       xalign = 1.0 - priv->xalign;
5932
5933   text_width = PANGO_PIXELS(logical_rect.width);
5934
5935   if (text_width > text_area_width)
5936     {
5937       min_offset = 0;
5938       max_offset = text_width - text_area_width;
5939     }
5940   else
5941     {
5942       min_offset = (text_width - text_area_width) * xalign;
5943       max_offset = min_offset;
5944     }
5945
5946   entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
5947
5948   /* And make sure cursors are on screen. Note that the cursor is
5949    * actually drawn one pixel into the INNER_BORDER space on
5950    * the right, when the scroll is at the utmost right. This
5951    * looks better to to me than confining the cursor inside the
5952    * border entirely, though it means that the cursor gets one
5953    * pixel closer to the edge of the widget on the right than
5954    * on the left. This might need changing if one changed
5955    * INNER_BORDER from 2 to 1, as one would do on a
5956    * small-screen-real-estate display.
5957    *
5958    * We always make sure that the strong cursor is on screen, and
5959    * put the weak cursor on screen if possible.
5960    */
5961
5962   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
5963   
5964   strong_xoffset = strong_x - entry->scroll_offset;
5965
5966   if (strong_xoffset < 0)
5967     {
5968       entry->scroll_offset += strong_xoffset;
5969       strong_xoffset = 0;
5970     }
5971   else if (strong_xoffset > text_area_width)
5972     {
5973       entry->scroll_offset += strong_xoffset - text_area_width;
5974       strong_xoffset = text_area_width;
5975     }
5976
5977   weak_xoffset = weak_x - entry->scroll_offset;
5978
5979   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
5980     {
5981       entry->scroll_offset += weak_xoffset;
5982     }
5983   else if (weak_xoffset > text_area_width &&
5984            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
5985     {
5986       entry->scroll_offset += weak_xoffset - text_area_width;
5987     }
5988
5989   g_object_notify (G_OBJECT (entry), "scroll-offset");
5990 }
5991
5992 static void
5993 gtk_entry_move_adjustments (GtkEntry *entry)
5994 {
5995   PangoContext *context;
5996   PangoFontMetrics *metrics;
5997   gint x, layout_x, border_x, border_y;
5998   gint char_width;
5999   GtkAdjustment *adjustment;
6000
6001   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6002   if (!adjustment)
6003     return;
6004
6005   /* Cursor position, layout offset, border width, and widget allocation */
6006   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6007   get_layout_position (entry, &layout_x, NULL);
6008   _gtk_entry_get_borders (entry, &border_x, &border_y);
6009   x += entry->widget.allocation.x + layout_x + border_x;
6010
6011   /* Approximate width of a char, so user can see what is ahead/behind */
6012   context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
6013   metrics = pango_context_get_metrics (context, 
6014                                        entry->widget.style->font_desc,
6015                                        pango_context_get_language (context));
6016   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6017
6018   /* Scroll it */
6019   gtk_adjustment_clamp_page (adjustment, 
6020                              x - (char_width + 1),   /* one char + one pixel before */
6021                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6022 }
6023
6024 static gint
6025 gtk_entry_move_visually (GtkEntry *entry,
6026                          gint      start,
6027                          gint      count)
6028 {
6029   gint index;
6030   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6031   const gchar *text;
6032
6033   text = pango_layout_get_text (layout);
6034   
6035   index = g_utf8_offset_to_pointer (text, start) - text;
6036
6037   while (count != 0)
6038     {
6039       int new_index, new_trailing;
6040       gboolean split_cursor;
6041       gboolean strong;
6042
6043       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6044                     "gtk-split-cursor", &split_cursor,
6045                     NULL);
6046
6047       if (split_cursor)
6048         strong = TRUE;
6049       else
6050         {
6051           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6052           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6053
6054           strong = keymap_direction == entry->resolved_dir;
6055         }
6056       
6057       if (count > 0)
6058         {
6059           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6060           count--;
6061         }
6062       else
6063         {
6064           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6065           count++;
6066         }
6067
6068       if (new_index < 0)
6069         index = 0;
6070       else if (new_index != G_MAXINT)
6071         index = new_index;
6072       
6073       while (new_trailing--)
6074         index = g_utf8_next_char (text + index) - text;
6075     }
6076   
6077   return g_utf8_pointer_to_offset (text, text + index);
6078 }
6079
6080 static gint
6081 gtk_entry_move_logically (GtkEntry *entry,
6082                           gint      start,
6083                           gint      count)
6084 {
6085   gint new_pos = start;
6086   guint length;
6087
6088   length = gtk_entry_buffer_get_length (get_buffer (entry));
6089
6090   /* Prevent any leak of information */
6091   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6092     {
6093       new_pos = CLAMP (start + count, 0, length);
6094     }
6095   else
6096     {
6097       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6098       PangoLogAttr *log_attrs;
6099       gint n_attrs;
6100
6101       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6102
6103       while (count > 0 && new_pos < length)
6104         {
6105           do
6106             new_pos++;
6107           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6108           
6109           count--;
6110         }
6111       while (count < 0 && new_pos > 0)
6112         {
6113           do
6114             new_pos--;
6115           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6116           
6117           count++;
6118         }
6119       
6120       g_free (log_attrs);
6121     }
6122
6123   return new_pos;
6124 }
6125
6126 static gint
6127 gtk_entry_move_forward_word (GtkEntry *entry,
6128                              gint      start,
6129                              gboolean  allow_whitespace)
6130 {
6131   gint new_pos = start;
6132   guint length;
6133
6134   length = gtk_entry_buffer_get_length (get_buffer (entry));
6135
6136   /* Prevent any leak of information */
6137   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6138     {
6139       new_pos = length;
6140     }
6141   else if (new_pos < length)
6142     {
6143       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6144       PangoLogAttr *log_attrs;
6145       gint n_attrs;
6146
6147       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6148       
6149       /* Find the next word boundary */
6150       new_pos++;
6151       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6152                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6153         new_pos++;
6154
6155       g_free (log_attrs);
6156     }
6157
6158   return new_pos;
6159 }
6160
6161
6162 static gint
6163 gtk_entry_move_backward_word (GtkEntry *entry,
6164                               gint      start,
6165                               gboolean  allow_whitespace)
6166 {
6167   gint new_pos = start;
6168
6169   /* Prevent any leak of information */
6170   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6171     {
6172       new_pos = 0;
6173     }
6174   else if (start > 0)
6175     {
6176       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6177       PangoLogAttr *log_attrs;
6178       gint n_attrs;
6179
6180       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6181
6182       new_pos = start - 1;
6183
6184       /* Find the previous word boundary */
6185       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6186                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6187         new_pos--;
6188
6189       g_free (log_attrs);
6190     }
6191
6192   return new_pos;
6193 }
6194
6195 static void
6196 gtk_entry_delete_whitespace (GtkEntry *entry)
6197 {
6198   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6199   PangoLogAttr *log_attrs;
6200   gint n_attrs;
6201   gint start, end;
6202
6203   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6204
6205   start = end = entry->current_pos;
6206   
6207   while (start > 0 && log_attrs[start-1].is_white)
6208     start--;
6209
6210   while (end < n_attrs && log_attrs[end].is_white)
6211     end++;
6212
6213   g_free (log_attrs);
6214
6215   if (start != end)
6216     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6217 }
6218
6219
6220 static void
6221 gtk_entry_select_word (GtkEntry *entry)
6222 {
6223   gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
6224   gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
6225
6226   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6227 }
6228
6229 static void
6230 gtk_entry_select_line (GtkEntry *entry)
6231 {
6232   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6233 }
6234
6235 static gint
6236 truncate_multiline (const gchar *text)
6237 {
6238   gint length;
6239
6240   for (length = 0;
6241        text[length] && text[length] != '\n' && text[length] != '\r';
6242        length++);
6243
6244   return length;
6245 }
6246
6247 static void
6248 paste_received (GtkClipboard *clipboard,
6249                 const gchar  *text,
6250                 gpointer      data)
6251 {
6252   GtkEntry *entry = GTK_ENTRY (data);
6253   GtkEditable *editable = GTK_EDITABLE (entry);
6254   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6255       
6256   if (entry->button == 2)
6257     {
6258       gint pos, start, end;
6259       pos = priv->insert_pos;
6260       gtk_editable_get_selection_bounds (editable, &start, &end);
6261       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6262         gtk_editable_select_region (editable, pos, pos);
6263     }
6264       
6265   if (text)
6266     {
6267       gint pos, start, end;
6268       gint length = -1;
6269       gboolean popup_completion;
6270       GtkEntryCompletion *completion;
6271
6272       completion = gtk_entry_get_completion (entry);
6273
6274       if (entry->truncate_multiline)
6275         length = truncate_multiline (text);
6276
6277       /* only complete if the selection is at the end */
6278       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6279                           MAX (entry->current_pos, entry->selection_bound));
6280
6281       if (completion)
6282         {
6283           if (gtk_widget_get_mapped (completion->priv->popup_window))
6284             _gtk_entry_completion_popdown (completion);
6285
6286           if (!popup_completion && completion->priv->changed_id > 0)
6287             g_signal_handler_block (entry, completion->priv->changed_id);
6288         }
6289
6290       begin_change (entry);
6291       g_object_freeze_notify (G_OBJECT (entry));
6292       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6293         gtk_editable_delete_text (editable, start, end);
6294
6295       pos = entry->current_pos;
6296       gtk_editable_insert_text (editable, text, length, &pos);
6297       gtk_editable_set_position (editable, pos);
6298       g_object_thaw_notify (G_OBJECT (entry));
6299       end_change (entry);
6300
6301       if (completion &&
6302           !popup_completion && completion->priv->changed_id > 0)
6303         g_signal_handler_unblock (entry, completion->priv->changed_id);
6304     }
6305
6306   g_object_unref (entry);
6307 }
6308
6309 static void
6310 gtk_entry_paste (GtkEntry *entry,
6311                  GdkAtom   selection)
6312 {
6313   g_object_ref (entry);
6314   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6315                               paste_received, entry);
6316 }
6317
6318 static void
6319 primary_get_cb (GtkClipboard     *clipboard,
6320                 GtkSelectionData *selection_data,
6321                 guint             info,
6322                 gpointer          data)
6323 {
6324   GtkEntry *entry = GTK_ENTRY (data);
6325   gint start, end;
6326   
6327   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6328     {
6329       gchar *str = gtk_entry_get_display_text (entry, start, end);
6330       gtk_selection_data_set_text (selection_data, str, -1);
6331       g_free (str);
6332     }
6333 }
6334
6335 static void
6336 primary_clear_cb (GtkClipboard *clipboard,
6337                   gpointer      data)
6338 {
6339   GtkEntry *entry = GTK_ENTRY (data);
6340
6341   gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
6342 }
6343
6344 static void
6345 gtk_entry_update_primary_selection (GtkEntry *entry)
6346 {
6347   GtkTargetList *list;
6348   GtkTargetEntry *targets;
6349   GtkClipboard *clipboard;
6350   gint start, end;
6351   gint n_targets;
6352
6353   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6354     return;
6355
6356   list = gtk_target_list_new (NULL, 0);
6357   gtk_target_list_add_text_targets (list, 0);
6358
6359   targets = gtk_target_table_new_from_list (list, &n_targets);
6360
6361   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6362   
6363   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6364     {
6365       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6366                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6367         primary_clear_cb (clipboard, entry);
6368     }
6369   else
6370     {
6371       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6372         gtk_clipboard_clear (clipboard);
6373     }
6374
6375   gtk_target_table_free (targets, n_targets);
6376   gtk_target_list_unref (list);
6377 }
6378
6379 static void
6380 gtk_entry_clear (GtkEntry             *entry,
6381                  GtkEntryIconPosition  icon_pos)
6382 {
6383   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6384   EntryIconInfo *icon_info = priv->icons[icon_pos];
6385
6386   if (!icon_info || icon_info->storage_type == GTK_IMAGE_EMPTY)
6387     return;
6388
6389   g_object_freeze_notify (G_OBJECT (entry));
6390
6391   /* Explicitly check, as the pointer may become invalidated
6392    * during destruction.
6393    */
6394   if (GDK_IS_WINDOW (icon_info->window))
6395     gdk_window_hide (icon_info->window);
6396
6397   if (icon_info->pixbuf)
6398     {
6399       g_object_unref (icon_info->pixbuf);
6400       icon_info->pixbuf = NULL;
6401     }
6402
6403   switch (icon_info->storage_type)
6404     {
6405     case GTK_IMAGE_PIXBUF:
6406       g_object_notify (G_OBJECT (entry),
6407                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6408       break;
6409
6410     case GTK_IMAGE_STOCK:
6411       g_free (icon_info->stock_id);
6412       icon_info->stock_id = NULL;
6413       g_object_notify (G_OBJECT (entry),
6414                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6415       break;
6416
6417     case GTK_IMAGE_ICON_NAME:
6418       g_free (icon_info->icon_name);
6419       icon_info->icon_name = NULL;
6420       g_object_notify (G_OBJECT (entry),
6421                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6422       break;
6423
6424     case GTK_IMAGE_GICON:
6425       if (icon_info->gicon)
6426         {
6427           g_object_unref (icon_info->gicon);
6428           icon_info->gicon = NULL;
6429         }
6430       g_object_notify (G_OBJECT (entry),
6431                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6432       break;
6433
6434     default:
6435       g_assert_not_reached ();
6436       break;
6437     }
6438
6439   icon_info->storage_type = GTK_IMAGE_EMPTY;
6440   g_object_notify (G_OBJECT (entry),
6441                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6442
6443   g_object_thaw_notify (G_OBJECT (entry));
6444 }
6445
6446 static void
6447 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6448                          GtkEntryIconPosition  icon_pos)
6449 {
6450   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6451   EntryIconInfo *icon_info = priv->icons[icon_pos];
6452   GtkIconInfo *info;
6453   GtkIconTheme *icon_theme;
6454   GtkSettings *settings;
6455   GtkStateType state;
6456   GtkWidget *widget;
6457   GdkScreen *screen;
6458   gint width, height;
6459
6460   if (!icon_info || icon_info->pixbuf)
6461     return;
6462
6463   widget = GTK_WIDGET (entry);
6464
6465   switch (icon_info->storage_type)
6466     {
6467     case GTK_IMAGE_EMPTY:
6468     case GTK_IMAGE_PIXBUF:
6469       break;
6470     case GTK_IMAGE_STOCK:
6471       state = gtk_widget_get_state (widget);
6472       gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6473       icon_info->pixbuf = gtk_widget_render_icon (widget,
6474                                                   icon_info->stock_id,
6475                                                   GTK_ICON_SIZE_MENU,
6476                                                   NULL);
6477       if (!icon_info->pixbuf)
6478         icon_info->pixbuf = gtk_widget_render_icon (widget,
6479                                                     GTK_STOCK_MISSING_IMAGE,
6480                                                     GTK_ICON_SIZE_MENU,
6481                                                     NULL);
6482       gtk_widget_set_state (widget, state);
6483       break;
6484
6485     case GTK_IMAGE_ICON_NAME:
6486       screen = gtk_widget_get_screen (widget);
6487       if (screen)
6488         {
6489           icon_theme = gtk_icon_theme_get_for_screen (screen);
6490           settings = gtk_settings_get_for_screen (screen);
6491           
6492           gtk_icon_size_lookup_for_settings (settings,
6493                                              GTK_ICON_SIZE_MENU,
6494                                              &width, &height);
6495
6496           icon_info->pixbuf = gtk_icon_theme_load_icon (icon_theme,
6497                                                         icon_info->icon_name,
6498                                                         MIN (width, height),
6499                                                         0, NULL);
6500
6501           if (icon_info->pixbuf == NULL)
6502             {
6503               state = gtk_widget_get_state (widget);
6504               gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6505               icon_info->pixbuf = gtk_widget_render_icon (widget,
6506                                                           GTK_STOCK_MISSING_IMAGE,
6507                                                           GTK_ICON_SIZE_MENU,
6508                                                           NULL);
6509               gtk_widget_set_state (widget, state);
6510             }
6511         }
6512       break;
6513
6514     case GTK_IMAGE_GICON:
6515       screen = gtk_widget_get_screen (widget);
6516       if (screen)
6517         {
6518           icon_theme = gtk_icon_theme_get_for_screen (screen);
6519           settings = gtk_settings_get_for_screen (screen);
6520
6521           gtk_icon_size_lookup_for_settings (settings,
6522                                              GTK_ICON_SIZE_MENU,
6523                                              &width, &height);
6524
6525           info = gtk_icon_theme_lookup_by_gicon (icon_theme,
6526                                                  icon_info->gicon,
6527                                                  MIN (width, height), 
6528                                                  GTK_ICON_LOOKUP_USE_BUILTIN);
6529           if (info)
6530             {
6531               icon_info->pixbuf = gtk_icon_info_load_icon (info, NULL);
6532               gtk_icon_info_free (info);
6533             }
6534
6535           if (icon_info->pixbuf == NULL)
6536             {
6537               state = gtk_widget_get_state (widget);
6538               gtk_widget_set_state (widget, GTK_STATE_NORMAL);
6539               icon_info->pixbuf = gtk_widget_render_icon (widget,
6540                                                           GTK_STOCK_MISSING_IMAGE,
6541                                                           GTK_ICON_SIZE_MENU,
6542                                                           NULL);
6543               gtk_widget_set_state (widget, state);
6544             }
6545         }
6546       break;
6547
6548     default:
6549       g_assert_not_reached ();
6550       break;
6551     }
6552     
6553   if (icon_info->pixbuf != NULL && icon_info->window != NULL)
6554     gdk_window_show_unraised (icon_info->window);
6555 }
6556
6557
6558 /* Public API
6559  */
6560
6561 /**
6562  * gtk_entry_new:
6563  *
6564  * Creates a new entry.
6565  *
6566  * Return value: a new #GtkEntry.
6567  */
6568 GtkWidget*
6569 gtk_entry_new (void)
6570 {
6571   return g_object_new (GTK_TYPE_ENTRY, NULL);
6572 }
6573
6574 /**
6575  * gtk_entry_new_with_buffer:
6576  * @buffer: The buffer to use for the new #GtkEntry.
6577  *
6578  * Creates a new entry with the specified text buffer.
6579  *
6580  * Return value: a new #GtkEntry
6581  *
6582  * Since: 2.18
6583  */
6584 GtkWidget*
6585 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6586 {
6587   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6588   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6589 }
6590
6591 /**
6592  * gtk_entry_new_with_max_length:
6593  * @max: the maximum length of the entry, or 0 for no maximum.
6594  *   (other than the maximum length of entries.) The value passed in will
6595  *   be clamped to the range 0-65536.
6596  *
6597  * Creates a new #GtkEntry widget with the given maximum length.
6598  * 
6599  * Return value: a new #GtkEntry
6600  *
6601  * Deprecated: 2.0: Use gtk_entry_set_max_length() instead.
6602  **/
6603 GtkWidget*
6604 gtk_entry_new_with_max_length (gint max)
6605 {
6606   GtkEntry *entry;
6607
6608   max = CLAMP (max, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
6609
6610   entry = g_object_new (GTK_TYPE_ENTRY, NULL);
6611   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
6612
6613   return GTK_WIDGET (entry);
6614 }
6615
6616
6617 static GtkEntryBuffer*
6618 get_buffer (GtkEntry *entry)
6619 {
6620   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
6621
6622   if (priv->buffer == NULL)
6623     {
6624       GtkEntryBuffer *buffer;
6625       buffer = gtk_entry_buffer_new (NULL, 0);
6626       gtk_entry_set_buffer (entry, buffer);
6627       g_object_unref (buffer);
6628     }
6629
6630   return priv->buffer;
6631 }
6632
6633 /**
6634  * gtk_entry_get_buffer:
6635  * @entry: a #GtkEntry
6636  *
6637  * Get the #GtkEntryBuffer object which holds the text for
6638  * this widget.
6639  *
6640  * Since: 2.18
6641  *
6642  * Returns: A #GtkEntryBuffer object.
6643  */
6644 GtkEntryBuffer*
6645 gtk_entry_get_buffer (GtkEntry *entry)
6646 {
6647   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6648
6649   return get_buffer (entry);
6650 }
6651
6652 /**
6653  * gtk_entry_set_buffer:
6654  * @entry: a #GtkEntry
6655  * @buffer: a #GtkEntryBuffer
6656  *
6657  * Set the #GtkEntryBuffer object which holds the text for
6658  * this widget.
6659  *
6660  * Since: 2.18
6661  */
6662 void
6663 gtk_entry_set_buffer (GtkEntry       *entry,
6664                       GtkEntryBuffer *buffer)
6665 {
6666   GtkEntryPrivate *priv;
6667   GObject *obj;
6668
6669   g_return_if_fail (GTK_IS_ENTRY (entry));
6670   priv = GTK_ENTRY_GET_PRIVATE (entry);
6671
6672   if (buffer)
6673     {
6674       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6675       g_object_ref (buffer);
6676     }
6677
6678   if (priv->buffer)
6679     {
6680       buffer_disconnect_signals (entry);
6681       g_object_unref (priv->buffer);
6682
6683       /* COMPAT: Deprecated. Not used. Setting these fields no longer necessary in GTK 3.x */
6684       entry->text = NULL;
6685       entry->text_length = 0;
6686       entry->text_max_length = 0;
6687     }
6688
6689   priv->buffer = buffer;
6690
6691   if (priv->buffer)
6692     {
6693        buffer_connect_signals (entry);
6694
6695        gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6696        gtk_entry_recompute (entry);
6697
6698       /* COMPAT: Deprecated. Not used. Setting these fields no longer necessary in GTK 3.x */
6699       entry->text = (char*)gtk_entry_buffer_get_text (priv->buffer);
6700       entry->text_length = gtk_entry_buffer_get_length (priv->buffer);
6701       entry->text_max_length = gtk_entry_buffer_get_max_length (priv->buffer);
6702     }
6703
6704   obj = G_OBJECT (entry);
6705   g_object_freeze_notify (obj);
6706   g_object_notify (obj, "buffer");
6707   g_object_notify (obj, "text");
6708   g_object_notify (obj, "text-length");
6709   g_object_notify (obj, "max-length");
6710   g_object_notify (obj, "visibility");
6711   g_object_notify (obj, "invisible-char");
6712   g_object_notify (obj, "invisible-char-set");
6713   g_object_thaw_notify (obj);
6714 }
6715
6716 /**
6717  * gtk_entry_get_text_window:
6718  * @entry: a #GtkEntry
6719  *
6720  * Returns the #GdkWindow which contains the text. This function is
6721  * useful when drawing something to the entry in an expose-event
6722  * callback because it enables the callback to distinguish between
6723  * the text window and entry's icon windows.
6724  *
6725  * See also gtk_entry_get_icon_window().
6726  *
6727  * Return value: the entry's text window.
6728  *
6729  * Since: 2.20
6730  **/
6731 GdkWindow *
6732 gtk_entry_get_text_window (GtkEntry *entry)
6733 {
6734   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6735
6736   return entry->text_area;
6737 }
6738
6739
6740 /**
6741  * gtk_entry_set_text:
6742  * @entry: a #GtkEntry
6743  * @text: the new text
6744  *
6745  * Sets the text in the widget to the given
6746  * value, replacing the current contents.
6747  *
6748  * See gtk_entry_buffer_set_text().
6749  */
6750 void
6751 gtk_entry_set_text (GtkEntry    *entry,
6752                     const gchar *text)
6753 {
6754   gint tmp_pos;
6755   GtkEntryCompletion *completion;
6756   GtkEntryPrivate *priv;
6757
6758   g_return_if_fail (GTK_IS_ENTRY (entry));
6759   g_return_if_fail (text != NULL);
6760   priv = GTK_ENTRY_GET_PRIVATE (entry);
6761
6762   /* Actually setting the text will affect the cursor and selection;
6763    * if the contents don't actually change, this will look odd to the user.
6764    */
6765   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6766     return;
6767
6768   completion = gtk_entry_get_completion (entry);
6769   if (completion && completion->priv->changed_id > 0)
6770     g_signal_handler_block (entry, completion->priv->changed_id);
6771
6772   begin_change (entry);
6773   g_object_freeze_notify (G_OBJECT (entry));
6774   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6775   tmp_pos = 0;
6776   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6777   g_object_thaw_notify (G_OBJECT (entry));
6778   end_change (entry);
6779
6780   if (completion && completion->priv->changed_id > 0)
6781     g_signal_handler_unblock (entry, completion->priv->changed_id);
6782 }
6783
6784 /**
6785  * gtk_entry_append_text:
6786  * @entry: a #GtkEntry
6787  * @text: the text to append
6788  *
6789  * Appends the given text to the contents of the widget.
6790  *
6791  * Deprecated: 2.0: Use gtk_editable_insert_text() instead.
6792  */
6793 void
6794 gtk_entry_append_text (GtkEntry *entry,
6795                        const gchar *text)
6796 {
6797   GtkEntryPrivate *priv;
6798   gint tmp_pos;
6799
6800   g_return_if_fail (GTK_IS_ENTRY (entry));
6801   g_return_if_fail (text != NULL);
6802   priv = GTK_ENTRY_GET_PRIVATE (entry);
6803
6804   tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
6805   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
6806 }
6807
6808 /**
6809  * gtk_entry_prepend_text:
6810  * @entry: a #GtkEntry
6811  * @text: the text to prepend
6812  *
6813  * Prepends the given text to the contents of the widget.
6814  *
6815  * Deprecated: 2.0: Use gtk_editable_insert_text() instead.
6816  */
6817 void
6818 gtk_entry_prepend_text (GtkEntry *entry,
6819                         const gchar *text)
6820 {
6821   gint tmp_pos;
6822
6823   g_return_if_fail (GTK_IS_ENTRY (entry));
6824   g_return_if_fail (text != NULL);
6825
6826   tmp_pos = 0;
6827   gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
6828 }
6829
6830 /**
6831  * gtk_entry_set_position:
6832  * @entry: a #GtkEntry
6833  * @position: the position of the cursor. The cursor is displayed
6834  *    before the character with the given (base 0) index in the widget. 
6835  *    The value must be less than or equal to the number of characters 
6836  *    in the widget. A value of -1 indicates that the position should
6837  *    be set after the last character in the entry. Note that this 
6838  *    position is in characters, not in bytes.
6839  *
6840  * Sets the cursor position in an entry to the given value. 
6841  *
6842  * Deprecated: 2.0: Use gtk_editable_set_position() instead.
6843  */
6844 void
6845 gtk_entry_set_position (GtkEntry *entry,
6846                         gint      position)
6847 {
6848   g_return_if_fail (GTK_IS_ENTRY (entry));
6849
6850   gtk_editable_set_position (GTK_EDITABLE (entry), position);
6851 }
6852
6853 /**
6854  * gtk_entry_set_visibility:
6855  * @entry: a #GtkEntry
6856  * @visible: %TRUE if the contents of the entry are displayed
6857  *           as plaintext
6858  *
6859  * Sets whether the contents of the entry are visible or not. 
6860  * When visibility is set to %FALSE, characters are displayed 
6861  * as the invisible char, and will also appear that way when 
6862  * the text in the entry widget is copied elsewhere.
6863  *
6864  * By default, GTK+ picks the best invisible character available
6865  * in the current font, but it can be changed with
6866  * gtk_entry_set_invisible_char().
6867  */
6868 void
6869 gtk_entry_set_visibility (GtkEntry *entry,
6870                           gboolean visible)
6871 {
6872   g_return_if_fail (GTK_IS_ENTRY (entry));
6873
6874   visible = visible != FALSE;
6875
6876   if (entry->visible != visible)
6877     {
6878       entry->visible = visible;
6879
6880       g_object_notify (G_OBJECT (entry), "visibility");
6881       gtk_entry_recompute (entry);
6882     }
6883 }
6884
6885 /**
6886  * gtk_entry_get_visibility:
6887  * @entry: a #GtkEntry
6888  *
6889  * Retrieves whether the text in @entry is visible. See
6890  * gtk_entry_set_visibility().
6891  *
6892  * Return value: %TRUE if the text is currently visible
6893  **/
6894 gboolean
6895 gtk_entry_get_visibility (GtkEntry *entry)
6896 {
6897   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6898
6899   return entry->visible;
6900 }
6901
6902 /**
6903  * gtk_entry_set_invisible_char:
6904  * @entry: a #GtkEntry
6905  * @ch: a Unicode character
6906  * 
6907  * Sets the character to use in place of the actual text when
6908  * gtk_entry_set_visibility() has been called to set text visibility
6909  * to %FALSE. i.e. this is the character used in "password mode" to
6910  * show the user how many characters have been typed. By default, GTK+
6911  * picks the best invisible char available in the current font. If you
6912  * set the invisible char to 0, then the user will get no feedback
6913  * at all; there will be no text on the screen as they type.
6914  **/
6915 void
6916 gtk_entry_set_invisible_char (GtkEntry *entry,
6917                               gunichar  ch)
6918 {
6919   GtkEntryPrivate *priv;
6920
6921   g_return_if_fail (GTK_IS_ENTRY (entry));
6922
6923   priv = GTK_ENTRY_GET_PRIVATE (entry);
6924
6925   if (!priv->invisible_char_set)
6926     {
6927       priv->invisible_char_set = TRUE;
6928       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6929     }
6930
6931   if (ch == entry->invisible_char)
6932     return;
6933
6934   entry->invisible_char = ch;
6935   g_object_notify (G_OBJECT (entry), "invisible-char");
6936   gtk_entry_recompute (entry);  
6937 }
6938
6939 /**
6940  * gtk_entry_get_invisible_char:
6941  * @entry: a #GtkEntry
6942  *
6943  * Retrieves the character displayed in place of the real characters
6944  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6945  *
6946  * Return value: the current invisible char, or 0, if the entry does not
6947  *               show invisible text at all. 
6948  **/
6949 gunichar
6950 gtk_entry_get_invisible_char (GtkEntry *entry)
6951 {
6952   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6953
6954   return entry->invisible_char;
6955 }
6956
6957 /**
6958  * gtk_entry_unset_invisible_char:
6959  * @entry: a #GtkEntry
6960  *
6961  * Unsets the invisible char previously set with
6962  * gtk_entry_set_invisible_char(). So that the
6963  * default invisible char is used again.
6964  *
6965  * Since: 2.16
6966  **/
6967 void
6968 gtk_entry_unset_invisible_char (GtkEntry *entry)
6969 {
6970   GtkEntryPrivate *priv;
6971   gunichar ch;
6972
6973   g_return_if_fail (GTK_IS_ENTRY (entry));
6974
6975   priv = GTK_ENTRY_GET_PRIVATE (entry);
6976
6977   if (!priv->invisible_char_set)
6978     return;
6979
6980   priv->invisible_char_set = FALSE;
6981   ch = find_invisible_char (GTK_WIDGET (entry));
6982
6983   if (entry->invisible_char != ch)
6984     {
6985       entry->invisible_char = ch;
6986       g_object_notify (G_OBJECT (entry), "invisible-char");
6987     }
6988
6989   g_object_notify (G_OBJECT (entry), "invisible-char-set");
6990   gtk_entry_recompute (entry);
6991 }
6992
6993 /**
6994  * gtk_entry_set_editable:
6995  * @entry: a #GtkEntry
6996  * @editable: %TRUE if the user is allowed to edit the text
6997  *   in the widget
6998  *
6999  * Determines if the user can edit the text in the editable
7000  * widget or not. 
7001  *
7002  * Deprecated: 2.0: Use gtk_editable_set_editable() instead.
7003  */
7004 void
7005 gtk_entry_set_editable (GtkEntry *entry,
7006                         gboolean  editable)
7007 {
7008   g_return_if_fail (GTK_IS_ENTRY (entry));
7009
7010   gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
7011 }
7012
7013 /**
7014  * gtk_entry_set_overwrite_mode:
7015  * @entry: a #GtkEntry
7016  * @overwrite: new value
7017  * 
7018  * Sets whether the text is overwritten when typing in the #GtkEntry.
7019  *
7020  * Since: 2.14
7021  **/
7022 void
7023 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7024                               gboolean  overwrite)
7025 {
7026   g_return_if_fail (GTK_IS_ENTRY (entry));
7027   
7028   if (entry->overwrite_mode == overwrite) 
7029     return;
7030   
7031   gtk_entry_toggle_overwrite (entry);
7032
7033   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7034 }
7035
7036 /**
7037  * gtk_entry_get_overwrite_mode:
7038  * @entry: a #GtkEntry
7039  * 
7040  * Gets the value set by gtk_entry_set_overwrite_mode().
7041  * 
7042  * Return value: whether the text is overwritten when typing.
7043  *
7044  * Since: 2.14
7045  **/
7046 gboolean
7047 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7048 {
7049   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7050
7051   return entry->overwrite_mode;
7052 }
7053
7054 /**
7055  * gtk_entry_get_text:
7056  * @entry: a #GtkEntry
7057  *
7058  * Retrieves the contents of the entry widget.
7059  * See also gtk_editable_get_chars().
7060  *
7061  * This is equivalent to:
7062  *
7063  * <informalexample><programlisting>
7064  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7065  * </programlisting></informalexample>
7066  *
7067  * Return value: a pointer to the contents of the widget as a
7068  *      string. This string points to internally allocated
7069  *      storage in the widget and must not be freed, modified or
7070  *      stored.
7071  **/
7072 G_CONST_RETURN gchar*
7073 gtk_entry_get_text (GtkEntry *entry)
7074 {
7075   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7076   return gtk_entry_buffer_get_text (get_buffer (entry));
7077 }
7078
7079 /**
7080  * gtk_entry_select_region:
7081  * @entry: a #GtkEntry
7082  * @start: the starting position
7083  * @end: the end position
7084  *
7085  * Selects a region of text. The characters that are selected are 
7086  * those characters at positions from @start_pos up to, but not 
7087  * including @end_pos. If @end_pos is negative, then the the characters 
7088  * selected will be those characters from @start_pos to the end of 
7089  * the text. 
7090  *
7091  * Deprecated: 2.0: Use gtk_editable_select_region() instead.
7092  */
7093 void       
7094 gtk_entry_select_region  (GtkEntry       *entry,
7095                           gint            start,
7096                           gint            end)
7097 {
7098   gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
7099 }
7100
7101 /**
7102  * gtk_entry_set_max_length:
7103  * @entry: a #GtkEntry
7104  * @max: the maximum length of the entry, or 0 for no maximum.
7105  *   (other than the maximum length of entries.) The value passed in will
7106  *   be clamped to the range 0-65536.
7107  * 
7108  * Sets the maximum allowed length of the contents of the widget. If
7109  * the current contents are longer than the given length, then they
7110  * will be truncated to fit.
7111  *
7112  * This is equivalent to:
7113  *
7114  * <informalexample><programlisting>
7115  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7116  * </programlisting></informalexample>
7117  **/
7118 void
7119 gtk_entry_set_max_length (GtkEntry     *entry,
7120                           gint          max)
7121 {
7122   g_return_if_fail (GTK_IS_ENTRY (entry));
7123   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7124 }
7125
7126 /**
7127  * gtk_entry_get_max_length:
7128  * @entry: a #GtkEntry
7129  *
7130  * Retrieves the maximum allowed length of the text in
7131  * @entry. See gtk_entry_set_max_length().
7132  *
7133  * This is equivalent to:
7134  *
7135  * <informalexample><programlisting>
7136  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7137  * </programlisting></informalexample>
7138  *
7139  * Return value: the maximum allowed number of characters
7140  *               in #GtkEntry, or 0 if there is no maximum.
7141  **/
7142 gint
7143 gtk_entry_get_max_length (GtkEntry *entry)
7144 {
7145   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7146   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7147 }
7148
7149 /**
7150  * gtk_entry_get_text_length:
7151  * @entry: a #GtkEntry
7152  *
7153  * Retrieves the current length of the text in
7154  * @entry. 
7155  *
7156  * This is equivalent to:
7157  *
7158  * <informalexample><programlisting>
7159  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7160  * </programlisting></informalexample>
7161  *
7162  * Return value: the current number of characters
7163  *               in #GtkEntry, or 0 if there are none.
7164  *
7165  * Since: 2.14
7166  **/
7167 guint16
7168 gtk_entry_get_text_length (GtkEntry *entry)
7169 {
7170   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7171   return gtk_entry_buffer_get_length (get_buffer (entry));
7172 }
7173
7174 /**
7175  * gtk_entry_set_activates_default:
7176  * @entry: a #GtkEntry
7177  * @setting: %TRUE to activate window's default widget on Enter keypress
7178  *
7179  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7180  * widget for the window containing the entry. This usually means that
7181  * the dialog box containing the entry will be closed, since the default
7182  * widget is usually one of the dialog buttons.
7183  *
7184  * (For experts: if @setting is %TRUE, the entry calls
7185  * gtk_window_activate_default() on the window containing the entry, in
7186  * the default handler for the #GtkWidget::activate signal.)
7187  **/
7188 void
7189 gtk_entry_set_activates_default (GtkEntry *entry,
7190                                  gboolean  setting)
7191 {
7192   g_return_if_fail (GTK_IS_ENTRY (entry));
7193   setting = setting != FALSE;
7194
7195   if (setting != entry->activates_default)
7196     {
7197       entry->activates_default = setting;
7198       g_object_notify (G_OBJECT (entry), "activates-default");
7199     }
7200 }
7201
7202 /**
7203  * gtk_entry_get_activates_default:
7204  * @entry: a #GtkEntry
7205  * 
7206  * Retrieves the value set by gtk_entry_set_activates_default().
7207  * 
7208  * Return value: %TRUE if the entry will activate the default widget
7209  **/
7210 gboolean
7211 gtk_entry_get_activates_default (GtkEntry *entry)
7212 {
7213   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7214
7215   return entry->activates_default;
7216 }
7217
7218 /**
7219  * gtk_entry_set_width_chars:
7220  * @entry: a #GtkEntry
7221  * @n_chars: width in chars
7222  *
7223  * Changes the size request of the entry to be about the right size
7224  * for @n_chars characters. Note that it changes the size
7225  * <emphasis>request</emphasis>, the size can still be affected by
7226  * how you pack the widget into containers. If @n_chars is -1, the
7227  * size reverts to the default entry size.
7228  **/
7229 void
7230 gtk_entry_set_width_chars (GtkEntry *entry,
7231                            gint      n_chars)
7232 {
7233   g_return_if_fail (GTK_IS_ENTRY (entry));
7234
7235   if (entry->width_chars != n_chars)
7236     {
7237       entry->width_chars = n_chars;
7238       g_object_notify (G_OBJECT (entry), "width-chars");
7239       gtk_widget_queue_resize (GTK_WIDGET (entry));
7240     }
7241 }
7242
7243 /**
7244  * gtk_entry_get_width_chars:
7245  * @entry: a #GtkEntry
7246  * 
7247  * Gets the value set by gtk_entry_set_width_chars().
7248  * 
7249  * Return value: number of chars to request space for, or negative if unset
7250  **/
7251 gint
7252 gtk_entry_get_width_chars (GtkEntry *entry)
7253 {
7254   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7255
7256   return entry->width_chars;
7257 }
7258
7259 /**
7260  * gtk_entry_set_has_frame:
7261  * @entry: a #GtkEntry
7262  * @setting: new value
7263  * 
7264  * Sets whether the entry has a beveled frame around it.
7265  **/
7266 void
7267 gtk_entry_set_has_frame (GtkEntry *entry,
7268                          gboolean  setting)
7269 {
7270   g_return_if_fail (GTK_IS_ENTRY (entry));
7271
7272   setting = (setting != FALSE);
7273
7274   if (entry->has_frame == setting)
7275     return;
7276
7277   gtk_widget_queue_resize (GTK_WIDGET (entry));
7278   entry->has_frame = setting;
7279   g_object_notify (G_OBJECT (entry), "has-frame");
7280 }
7281
7282 /**
7283  * gtk_entry_get_has_frame:
7284  * @entry: a #GtkEntry
7285  * 
7286  * Gets the value set by gtk_entry_set_has_frame().
7287  * 
7288  * Return value: whether the entry has a beveled frame
7289  **/
7290 gboolean
7291 gtk_entry_get_has_frame (GtkEntry *entry)
7292 {
7293   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7294
7295   return entry->has_frame;
7296 }
7297
7298 /**
7299  * gtk_entry_set_inner_border:
7300  * @entry: a #GtkEntry
7301  * @border: (allow-none): a #GtkBorder, or %NULL
7302  *
7303  * Sets %entry's inner-border property to %border, or clears it if %NULL
7304  * is passed. The inner-border is the area around the entry's text, but
7305  * inside its frame.
7306  *
7307  * If set, this property overrides the inner-border style property.
7308  * Overriding the style-provided border is useful when you want to do
7309  * in-place editing of some text in a canvas or list widget, where
7310  * pixel-exact positioning of the entry is important.
7311  *
7312  * Since: 2.10
7313  **/
7314 void
7315 gtk_entry_set_inner_border (GtkEntry        *entry,
7316                             const GtkBorder *border)
7317 {
7318   g_return_if_fail (GTK_IS_ENTRY (entry));
7319
7320   gtk_widget_queue_resize (GTK_WIDGET (entry));
7321
7322   if (border)
7323     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7324                              gtk_border_copy (border),
7325                              (GDestroyNotify) gtk_border_free);
7326   else
7327     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7328
7329   g_object_notify (G_OBJECT (entry), "inner-border");
7330 }
7331
7332 /**
7333  * gtk_entry_get_inner_border:
7334  * @entry: a #GtkEntry
7335  *
7336  * This function returns the entry's #GtkEntry:inner-border property. See
7337  * gtk_entry_set_inner_border() for more information.
7338  *
7339  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7340  *
7341  * Since: 2.10
7342  **/
7343 G_CONST_RETURN GtkBorder *
7344 gtk_entry_get_inner_border (GtkEntry *entry)
7345 {
7346   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7347
7348   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7349 }
7350
7351 /**
7352  * gtk_entry_get_layout:
7353  * @entry: a #GtkEntry
7354  * 
7355  * Gets the #PangoLayout used to display the entry.
7356  * The layout is useful to e.g. convert text positions to
7357  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7358  * The returned layout is owned by the entry and must not be 
7359  * modified or freed by the caller.
7360  *
7361  * Keep in mind that the layout text may contain a preedit string, so
7362  * gtk_entry_layout_index_to_text_index() and
7363  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7364  * indices in the layout to byte indices in the entry contents.
7365  *
7366  * Return value: (transfer none): the #PangoLayout for this entry
7367  **/
7368 PangoLayout*
7369 gtk_entry_get_layout (GtkEntry *entry)
7370 {
7371   PangoLayout *layout;
7372   
7373   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7374
7375   layout = gtk_entry_ensure_layout (entry, TRUE);
7376
7377   return layout;
7378 }
7379
7380
7381 /**
7382  * gtk_entry_layout_index_to_text_index:
7383  * @entry: a #GtkEntry
7384  * @layout_index: byte index into the entry layout text
7385  * 
7386  * Converts from a position in the entry contents (returned
7387  * by gtk_entry_get_text()) to a position in the
7388  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7389  * with text retrieved via pango_layout_get_text()).
7390  * 
7391  * Return value: byte index into the entry contents
7392  **/
7393 gint
7394 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7395                                       gint      layout_index)
7396 {
7397   PangoLayout *layout;
7398   const gchar *text;
7399   gint cursor_index;
7400   
7401   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7402
7403   layout = gtk_entry_ensure_layout (entry, TRUE);
7404   text = pango_layout_get_text (layout);
7405   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7406   
7407   if (layout_index >= cursor_index && entry->preedit_length)
7408     {
7409       if (layout_index >= cursor_index + entry->preedit_length)
7410         layout_index -= entry->preedit_length;
7411       else
7412         layout_index = cursor_index;
7413     }
7414
7415   return layout_index;
7416 }
7417
7418 /**
7419  * gtk_entry_text_index_to_layout_index:
7420  * @entry: a #GtkEntry
7421  * @text_index: byte index into the entry contents
7422  * 
7423  * Converts from a position in the entry's #PangoLayout (returned by
7424  * gtk_entry_get_layout()) to a position in the entry contents
7425  * (returned by gtk_entry_get_text()).
7426  * 
7427  * Return value: byte index into the entry layout text
7428  **/
7429 gint
7430 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7431                                       gint      text_index)
7432 {
7433   PangoLayout *layout;
7434   const gchar *text;
7435   gint cursor_index;
7436   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7437
7438   layout = gtk_entry_ensure_layout (entry, TRUE);
7439   text = pango_layout_get_text (layout);
7440   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7441   
7442   if (text_index > cursor_index)
7443     text_index += entry->preedit_length;
7444
7445   return text_index;
7446 }
7447
7448 /**
7449  * gtk_entry_get_layout_offsets:
7450  * @entry: a #GtkEntry
7451  * @x: (allow-none): location to store X offset of layout, or %NULL
7452  * @y: (allow-none): location to store Y offset of layout, or %NULL
7453  *
7454  *
7455  * Obtains the position of the #PangoLayout used to render text
7456  * in the entry, in widget coordinates. Useful if you want to line
7457  * up the text in an entry with some other text, e.g. when using the
7458  * entry to implement editable cells in a sheet widget.
7459  *
7460  * Also useful to convert mouse events into coordinates inside the
7461  * #PangoLayout, e.g. to take some action if some part of the entry text
7462  * is clicked.
7463  * 
7464  * Note that as the user scrolls around in the entry the offsets will
7465  * change; you'll need to connect to the "notify::scroll-offset"
7466  * signal to track this. Remember when using the #PangoLayout
7467  * functions you need to convert to and from pixels using
7468  * PANGO_PIXELS() or #PANGO_SCALE.
7469  *
7470  * Keep in mind that the layout text may contain a preedit string, so
7471  * gtk_entry_layout_index_to_text_index() and
7472  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7473  * indices in the layout to byte indices in the entry contents.
7474  **/
7475 void
7476 gtk_entry_get_layout_offsets (GtkEntry *entry,
7477                               gint     *x,
7478                               gint     *y)
7479 {
7480   gint text_area_x, text_area_y;
7481   
7482   g_return_if_fail (GTK_IS_ENTRY (entry));
7483
7484   /* this gets coords relative to text area */
7485   get_layout_position (entry, x, y);
7486
7487   /* convert to widget coords */
7488   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7489   
7490   if (x)
7491     *x += text_area_x;
7492
7493   if (y)
7494     *y += text_area_y;
7495 }
7496
7497
7498 /**
7499  * gtk_entry_set_alignment:
7500  * @entry: a #GtkEntry
7501  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7502  *          Reversed for RTL layouts
7503  * 
7504  * Sets the alignment for the contents of the entry. This controls
7505  * the horizontal positioning of the contents when the displayed
7506  * text is shorter than the width of the entry.
7507  *
7508  * Since: 2.4
7509  **/
7510 void
7511 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7512 {
7513   GtkEntryPrivate *priv;
7514   
7515   g_return_if_fail (GTK_IS_ENTRY (entry));
7516
7517   priv = GTK_ENTRY_GET_PRIVATE (entry);
7518
7519   if (xalign < 0.0)
7520     xalign = 0.0;
7521   else if (xalign > 1.0)
7522     xalign = 1.0;
7523
7524   if (xalign != priv->xalign)
7525     {
7526       priv->xalign = xalign;
7527
7528       gtk_entry_recompute (entry);
7529
7530       g_object_notify (G_OBJECT (entry), "xalign");
7531     }
7532 }
7533
7534 /**
7535  * gtk_entry_get_alignment:
7536  * @entry: a #GtkEntry
7537  * 
7538  * Gets the value set by gtk_entry_set_alignment().
7539  * 
7540  * Return value: the alignment
7541  *
7542  * Since: 2.4
7543  **/
7544 gfloat
7545 gtk_entry_get_alignment (GtkEntry *entry)
7546 {
7547   GtkEntryPrivate *priv;
7548   
7549   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7550
7551   priv = GTK_ENTRY_GET_PRIVATE (entry);
7552
7553   return priv->xalign;
7554 }
7555
7556 /**
7557  * gtk_entry_set_icon_from_pixbuf:
7558  * @entry: a #GtkEntry
7559  * @icon_pos: Icon position
7560  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7561  *
7562  * Sets the icon shown in the specified position using a pixbuf.
7563  *
7564  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7565  *
7566  * Since: 2.16
7567  */
7568 void
7569 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7570                                 GtkEntryIconPosition  icon_pos,
7571                                 GdkPixbuf            *pixbuf)
7572 {
7573   GtkEntryPrivate *priv;
7574   EntryIconInfo *icon_info;
7575
7576   g_return_if_fail (GTK_IS_ENTRY (entry));
7577   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7578
7579   priv = GTK_ENTRY_GET_PRIVATE (entry);
7580
7581   if ((icon_info = priv->icons[icon_pos]) == NULL)
7582     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7583
7584   g_object_freeze_notify (G_OBJECT (entry));
7585
7586   if (pixbuf)
7587     g_object_ref (pixbuf);
7588
7589   gtk_entry_clear (entry, icon_pos);
7590
7591   if (pixbuf)
7592     {
7593       icon_info->storage_type = GTK_IMAGE_PIXBUF;
7594       icon_info->pixbuf = pixbuf;
7595
7596       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7597         {
7598           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7599           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7600         }
7601       else
7602         {
7603           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7604           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7605         }
7606
7607       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7608           gdk_window_show_unraised (icon_info->window);
7609     }
7610
7611   gtk_entry_ensure_pixbuf (entry, icon_pos);
7612   
7613   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7614     gtk_widget_queue_resize (GTK_WIDGET (entry));
7615
7616   g_object_thaw_notify (G_OBJECT (entry));
7617 }
7618
7619 /**
7620  * gtk_entry_set_icon_from_stock:
7621  * @entry: A #GtkEntry
7622  * @icon_pos: Icon position
7623  * @stock_id: (allow-none): The name of the stock item, or %NULL
7624  *
7625  * Sets the icon shown in the entry at the specified position from
7626  * a stock image.
7627  *
7628  * If @stock_id is %NULL, no icon will be shown in the specified position.
7629  *
7630  * Since: 2.16
7631  */
7632 void
7633 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7634                                GtkEntryIconPosition  icon_pos,
7635                                const gchar          *stock_id)
7636 {
7637   GtkEntryPrivate *priv;
7638   EntryIconInfo *icon_info;
7639   gchar *new_id;
7640
7641   g_return_if_fail (GTK_IS_ENTRY (entry));
7642   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7643
7644   priv = GTK_ENTRY_GET_PRIVATE (entry);
7645
7646   if ((icon_info = priv->icons[icon_pos]) == NULL)
7647     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7648
7649   g_object_freeze_notify (G_OBJECT (entry));
7650
7651   gtk_widget_ensure_style (GTK_WIDGET (entry));
7652
7653   /* need to dup before clearing */
7654   new_id = g_strdup (stock_id);
7655
7656   gtk_entry_clear (entry, icon_pos);
7657
7658   if (new_id != NULL)
7659     {
7660       icon_info->storage_type = GTK_IMAGE_STOCK;
7661       icon_info->stock_id = new_id;
7662
7663       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7664         {
7665           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7666           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7667         }
7668       else
7669         {
7670           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7671           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7672         }
7673
7674       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7675           gdk_window_show_unraised (icon_info->window);
7676     }
7677
7678   gtk_entry_ensure_pixbuf (entry, icon_pos);
7679
7680   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7681     gtk_widget_queue_resize (GTK_WIDGET (entry));
7682
7683   g_object_thaw_notify (G_OBJECT (entry));
7684 }
7685
7686 /**
7687  * gtk_entry_set_icon_from_icon_name:
7688  * @entry: A #GtkEntry
7689  * @icon_pos: The position at which to set the icon
7690  * @icon_name: (allow-none): An icon name, or %NULL
7691  *
7692  * Sets the icon shown in the entry at the specified position
7693  * from the current icon theme.
7694  *
7695  * If the icon name isn't known, a "broken image" icon will be displayed
7696  * instead.
7697  *
7698  * If @icon_name is %NULL, no icon will be shown in the specified position.
7699  *
7700  * Since: 2.16
7701  */
7702 void
7703 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7704                                    GtkEntryIconPosition  icon_pos,
7705                                    const gchar          *icon_name)
7706 {
7707   GtkEntryPrivate *priv;
7708   EntryIconInfo *icon_info;
7709   gchar *new_name;
7710
7711   g_return_if_fail (GTK_IS_ENTRY (entry));
7712   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7713
7714   priv = GTK_ENTRY_GET_PRIVATE (entry);
7715
7716   if ((icon_info = priv->icons[icon_pos]) == NULL)
7717     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7718
7719   g_object_freeze_notify (G_OBJECT (entry));
7720
7721   gtk_widget_ensure_style (GTK_WIDGET (entry));
7722
7723   /* need to dup before clearing */
7724   new_name = g_strdup (icon_name);
7725
7726   gtk_entry_clear (entry, icon_pos);
7727
7728   if (new_name != NULL)
7729     {
7730       icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7731       icon_info->icon_name = new_name;
7732
7733       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7734         {
7735           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7736           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7737         }
7738       else
7739         {
7740           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7741           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7742         }
7743
7744       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7745           gdk_window_show_unraised (icon_info->window);
7746     }
7747
7748   gtk_entry_ensure_pixbuf (entry, icon_pos);
7749
7750   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7751     gtk_widget_queue_resize (GTK_WIDGET (entry));
7752
7753   g_object_thaw_notify (G_OBJECT (entry));
7754 }
7755
7756 /**
7757  * gtk_entry_set_icon_from_gicon:
7758  * @entry: A #GtkEntry
7759  * @icon_pos: The position at which to set the icon
7760  * @icon: (allow-none): The icon to set, or %NULL
7761  *
7762  * Sets the icon shown in the entry at the specified position
7763  * from the current icon theme.
7764  * If the icon isn't known, a "broken image" icon will be displayed
7765  * instead.
7766  *
7767  * If @icon is %NULL, no icon will be shown in the specified position.
7768  *
7769  * Since: 2.16
7770  */
7771 void
7772 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7773                                GtkEntryIconPosition  icon_pos,
7774                                GIcon                *icon)
7775 {
7776   GtkEntryPrivate *priv;
7777   EntryIconInfo *icon_info;
7778
7779   g_return_if_fail (GTK_IS_ENTRY (entry));
7780   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7781
7782   priv = GTK_ENTRY_GET_PRIVATE (entry);
7783
7784   if ((icon_info = priv->icons[icon_pos]) == NULL)
7785     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7786
7787   g_object_freeze_notify (G_OBJECT (entry));
7788
7789   /* need to ref before clearing */
7790   if (icon)
7791     g_object_ref (icon);
7792
7793   gtk_entry_clear (entry, icon_pos);
7794
7795   if (icon)
7796     {
7797       icon_info->storage_type = GTK_IMAGE_GICON;
7798       icon_info->gicon = icon;
7799
7800       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7801         {
7802           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7803           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7804         }
7805       else
7806         {
7807           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7808           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7809         }
7810
7811       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7812           gdk_window_show_unraised (icon_info->window);
7813     }
7814
7815   gtk_entry_ensure_pixbuf (entry, icon_pos);
7816
7817   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7818     gtk_widget_queue_resize (GTK_WIDGET (entry));
7819
7820   g_object_thaw_notify (G_OBJECT (entry));
7821 }
7822
7823 /**
7824  * gtk_entry_set_icon_activatable:
7825  * @entry: A #GtkEntry
7826  * @icon_pos: Icon position
7827  * @activatable: %TRUE if the icon should be activatable
7828  *
7829  * Sets whether the icon is activatable.
7830  *
7831  * Since: 2.16
7832  */
7833 void
7834 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7835                                 GtkEntryIconPosition  icon_pos,
7836                                 gboolean              activatable)
7837 {
7838   GtkEntryPrivate *priv;
7839   EntryIconInfo *icon_info;
7840
7841   g_return_if_fail (GTK_IS_ENTRY (entry));
7842   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7843
7844   priv = GTK_ENTRY_GET_PRIVATE (entry);
7845
7846   if ((icon_info = priv->icons[icon_pos]) == NULL)
7847     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7848
7849   activatable = activatable != FALSE;
7850
7851   if (icon_info->nonactivatable != !activatable)
7852     {
7853       icon_info->nonactivatable = !activatable;
7854
7855       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7856         update_cursors (GTK_WIDGET (entry));
7857
7858       g_object_notify (G_OBJECT (entry),
7859                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7860     }
7861 }
7862
7863 /**
7864  * gtk_entry_get_icon_activatable:
7865  * @entry: a #GtkEntry
7866  * @icon_pos: Icon position
7867  *
7868  * Returns whether the icon is activatable.
7869  *
7870  * Returns: %TRUE if the icon is activatable.
7871  *
7872  * Since: 2.16
7873  */
7874 gboolean
7875 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7876                                 GtkEntryIconPosition  icon_pos)
7877 {
7878   GtkEntryPrivate *priv;
7879   EntryIconInfo *icon_info;
7880
7881   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7882   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7883
7884   priv = GTK_ENTRY_GET_PRIVATE (entry);
7885   icon_info = priv->icons[icon_pos];
7886
7887   return (icon_info != NULL && !icon_info->nonactivatable);
7888 }
7889
7890 /**
7891  * gtk_entry_get_icon_pixbuf:
7892  * @entry: A #GtkEntry
7893  * @icon_pos: Icon position
7894  *
7895  * Retrieves the image used for the icon.
7896  *
7897  * Unlike the other methods of setting and getting icon data, this
7898  * method will work regardless of whether the icon was set using a
7899  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7900  *
7901  * Returns: A #GdkPixbuf, or %NULL if no icon is set for this position.
7902  *
7903  * Since: 2.16
7904  */
7905 GdkPixbuf *
7906 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7907                            GtkEntryIconPosition  icon_pos)
7908 {
7909   GtkEntryPrivate *priv;
7910   EntryIconInfo *icon_info;
7911
7912   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7913   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7914
7915   priv = GTK_ENTRY_GET_PRIVATE (entry);
7916   icon_info = priv->icons[icon_pos];
7917
7918   if (!icon_info)
7919     return NULL;
7920
7921   gtk_entry_ensure_pixbuf (entry, icon_pos);
7922
7923   return icon_info->pixbuf;
7924 }
7925
7926 /**
7927  * gtk_entry_get_icon_gicon:
7928  * @entry: A #GtkEntry
7929  * @icon_pos: Icon position
7930  *
7931  * Retrieves the #GIcon used for the icon, or %NULL if there is
7932  * no icon or if the icon was set by some other method (e.g., by
7933  * stock, pixbuf, or icon name).
7934  *
7935  * Returns: A #GIcon, or %NULL if no icon is set or if the icon
7936  *          is not a #GIcon
7937  *
7938  * Since: 2.16
7939  */
7940 GIcon *
7941 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7942                           GtkEntryIconPosition  icon_pos)
7943 {
7944   GtkEntryPrivate *priv;
7945   EntryIconInfo *icon_info;
7946
7947   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7948   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7949
7950   priv = GTK_ENTRY_GET_PRIVATE (entry);
7951   icon_info = priv->icons[icon_pos];
7952
7953   if (!icon_info)
7954     return NULL;
7955
7956   return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
7957 }
7958
7959 /**
7960  * gtk_entry_get_icon_stock:
7961  * @entry: A #GtkEntry
7962  * @icon_pos: Icon position
7963  *
7964  * Retrieves the stock id used for the icon, or %NULL if there is
7965  * no icon or if the icon was set by some other method (e.g., by
7966  * pixbuf, icon name or gicon).
7967  *
7968  * Returns: A stock id, or %NULL if no icon is set or if the icon
7969  *          wasn't set from a stock id
7970  *
7971  * Since: 2.16
7972  */
7973 const gchar *
7974 gtk_entry_get_icon_stock (GtkEntry             *entry,
7975                           GtkEntryIconPosition  icon_pos)
7976 {
7977   GtkEntryPrivate *priv;
7978   EntryIconInfo *icon_info;
7979
7980   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7981   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7982
7983   priv = GTK_ENTRY_GET_PRIVATE (entry);
7984   icon_info = priv->icons[icon_pos];
7985
7986   if (!icon_info)
7987     return NULL;
7988
7989   return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
7990 }
7991
7992 /**
7993  * gtk_entry_get_icon_name:
7994  * @entry: A #GtkEntry
7995  * @icon_pos: Icon position
7996  *
7997  * Retrieves the icon name used for the icon, or %NULL if there is
7998  * no icon or if the icon was set by some other method (e.g., by
7999  * pixbuf, stock or gicon).
8000  *
8001  * Returns: An icon name, or %NULL if no icon is set or if the icon
8002  *          wasn't set from an icon name
8003  *
8004  * Since: 2.16
8005  */
8006 const gchar *
8007 gtk_entry_get_icon_name (GtkEntry             *entry,
8008                          GtkEntryIconPosition  icon_pos)
8009 {
8010   GtkEntryPrivate *priv;
8011   EntryIconInfo *icon_info;
8012
8013   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8014   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8015
8016   priv = GTK_ENTRY_GET_PRIVATE (entry);
8017   icon_info = priv->icons[icon_pos];
8018
8019   if (!icon_info)
8020     return NULL;
8021
8022   return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
8023 }
8024
8025 /**
8026  * gtk_entry_set_icon_sensitive:
8027  * @entry: A #GtkEntry
8028  * @icon_pos: Icon position
8029  * @sensitive: Specifies whether the icon should appear
8030  *             sensitive or insensitive
8031  *
8032  * Sets the sensitivity for the specified icon.
8033  *
8034  * Since: 2.16
8035  */
8036 void
8037 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8038                               GtkEntryIconPosition  icon_pos,
8039                               gboolean              sensitive)
8040 {
8041   GtkEntryPrivate *priv;
8042   EntryIconInfo *icon_info;
8043
8044   g_return_if_fail (GTK_IS_ENTRY (entry));
8045   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8046
8047   priv = GTK_ENTRY_GET_PRIVATE (entry);
8048
8049   if ((icon_info = priv->icons[icon_pos]) == NULL)
8050     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8051
8052   if (icon_info->insensitive != !sensitive)
8053     {
8054       icon_info->insensitive = !sensitive;
8055
8056       icon_info->pressed = FALSE;
8057       icon_info->prelight = FALSE;
8058
8059       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8060         update_cursors (GTK_WIDGET (entry));
8061
8062       gtk_widget_queue_draw (GTK_WIDGET (entry));
8063
8064       g_object_notify (G_OBJECT (entry),
8065                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8066     }
8067 }
8068
8069 /**
8070  * gtk_entry_get_icon_sensitive:
8071  * @entry: a #GtkEntry
8072  * @icon_pos: Icon position
8073  *
8074  * Returns whether the icon appears sensitive or insensitive.
8075  *
8076  * Returns: %TRUE if the icon is sensitive.
8077  *
8078  * Since: 2.16
8079  */
8080 gboolean
8081 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8082                               GtkEntryIconPosition  icon_pos)
8083 {
8084   GtkEntryPrivate *priv;
8085   EntryIconInfo *icon_info;
8086
8087   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8088   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8089
8090   priv = GTK_ENTRY_GET_PRIVATE (entry);
8091   icon_info = priv->icons[icon_pos];
8092
8093   return (!icon_info || !icon_info->insensitive);
8094
8095 }
8096
8097 /**
8098  * gtk_entry_get_icon_storage_type:
8099  * @entry: a #GtkEntry
8100  * @icon_pos: Icon position
8101  *
8102  * Gets the type of representation being used by the icon
8103  * to store image data. If the icon has no image data,
8104  * the return value will be %GTK_IMAGE_EMPTY.
8105  *
8106  * Return value: image representation being used
8107  *
8108  * Since: 2.16
8109  **/
8110 GtkImageType
8111 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8112                                  GtkEntryIconPosition  icon_pos)
8113 {
8114   GtkEntryPrivate *priv;
8115   EntryIconInfo *icon_info;
8116
8117   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8118   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8119
8120   priv = GTK_ENTRY_GET_PRIVATE (entry);
8121   icon_info = priv->icons[icon_pos];
8122
8123   if (!icon_info)
8124     return GTK_IMAGE_EMPTY;
8125
8126   return icon_info->storage_type;
8127 }
8128
8129 /**
8130  * gtk_entry_get_icon_at_pos:
8131  * @entry: a #GtkEntry
8132  * @x: the x coordinate of the position to find
8133  * @y: the y coordinate of the position to find
8134  *
8135  * Finds the icon at the given position and return its index.
8136  * If @x, @y doesn't lie inside an icon, -1 is returned.
8137  * This function is intended for use in a #GtkWidget::query-tooltip
8138  * signal handler.
8139  *
8140  * Returns: the index of the icon at the given position, or -1
8141  *
8142  * Since: 2.16
8143  */
8144 gint
8145 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8146                            gint      x,
8147                            gint      y)
8148 {
8149   GtkAllocation primary;
8150   GtkAllocation secondary;
8151
8152   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8153
8154   get_icon_allocations (entry, &primary, &secondary);
8155
8156   if (primary.x <= x && x < primary.x + primary.width &&
8157       primary.y <= y && y < primary.y + primary.height)
8158     return GTK_ENTRY_ICON_PRIMARY;
8159
8160   if (secondary.x <= x && x < secondary.x + secondary.width &&
8161       secondary.y <= y && y < secondary.y + secondary.height)
8162     return GTK_ENTRY_ICON_SECONDARY;
8163
8164   return -1;
8165 }
8166
8167 /**
8168  * gtk_entry_set_icon_drag_source:
8169  * @entry: a #GtkIconEntry
8170  * @icon_pos: icon position
8171  * @target_list: the targets (data formats) in which the data can be provided
8172  * @actions: a bitmask of the allowed drag actions
8173  *
8174  * Sets up the icon at the given position so that GTK+ will start a drag
8175  * operation when the user clicks and drags the icon.
8176  *
8177  * To handle the drag operation, you need to connect to the usual
8178  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8179  * signal, and use gtk_entry_get_current_icon_drag_source() in
8180  * your signal handler to find out if the drag was started from
8181  * an icon.
8182  *
8183  * By default, GTK+ uses the icon as the drag icon. You can use the 
8184  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8185  * have to use g_signal_connect_after() to ensure that your signal handler
8186  * gets executed after the default handler.
8187  *
8188  * Since: 2.16
8189  */
8190 void
8191 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8192                                 GtkEntryIconPosition  icon_pos,
8193                                 GtkTargetList        *target_list,
8194                                 GdkDragAction         actions)
8195 {
8196   GtkEntryPrivate *priv;
8197   EntryIconInfo *icon_info;
8198
8199   g_return_if_fail (GTK_IS_ENTRY (entry));
8200   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8201
8202   priv = GTK_ENTRY_GET_PRIVATE (entry);
8203
8204   if ((icon_info = priv->icons[icon_pos]) == NULL)
8205     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8206
8207   if (icon_info->target_list)
8208     gtk_target_list_unref (icon_info->target_list);
8209   icon_info->target_list = target_list;
8210   if (icon_info->target_list)
8211     gtk_target_list_ref (icon_info->target_list);
8212
8213   icon_info->actions = actions;
8214 }
8215
8216 /**
8217  * gtk_entry_get_current_icon_drag_source:
8218  * @entry: a #GtkIconEntry
8219  *
8220  * Returns the index of the icon which is the source of the current
8221  * DND operation, or -1.
8222  *
8223  * This function is meant to be used in a #GtkWidget::drag-data-get
8224  * callback.
8225  *
8226  * Returns: index of the icon which is the source of the current
8227  *          DND operation, or -1.
8228  *
8229  * Since: 2.16
8230  */
8231 gint
8232 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8233 {
8234   GtkEntryPrivate *priv;
8235   EntryIconInfo *icon_info = NULL;
8236   gint i;
8237
8238   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8239
8240   priv = GTK_ENTRY_GET_PRIVATE (entry);
8241
8242   for (i = 0; i < MAX_ICONS; i++)
8243     {
8244       if ((icon_info = priv->icons[i]))
8245         {
8246           if (icon_info->in_drag)
8247             return i;
8248         }
8249     }
8250
8251   return -1;
8252 }
8253
8254 /**
8255  * gtk_entry_get_icon_window:
8256  * @entry: A #GtkEntry
8257  * @icon_pos: Icon position
8258  *
8259  * Returns the #GdkWindow which contains the entry's icon at
8260  * @icon_pos. This function is useful when drawing something to the
8261  * entry in an expose-event callback because it enables the callback
8262  * to distinguish between the text window and entry's icon windows.
8263  *
8264  * See also gtk_entry_get_text_window().
8265  *
8266  * Return value: the entry's icon window at @icon_pos.
8267  *
8268  * Since: 2.20
8269  */
8270 GdkWindow  *
8271 gtk_entry_get_icon_window (GtkEntry             *entry,
8272                            GtkEntryIconPosition  icon_pos)
8273 {
8274   GtkEntryPrivate *priv;
8275   EntryIconInfo *icon_info;
8276
8277   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8278
8279   priv = GTK_ENTRY_GET_PRIVATE (entry);
8280
8281   icon_info = priv->icons[icon_pos];
8282
8283   if (icon_info)
8284     return icon_info->window;
8285
8286   return NULL;
8287 }
8288
8289 static void
8290 ensure_has_tooltip (GtkEntry *entry)
8291 {
8292   GtkEntryPrivate *priv;
8293   EntryIconInfo *icon_info;
8294   int i;
8295   gboolean has_tooltip = FALSE;
8296
8297   priv = GTK_ENTRY_GET_PRIVATE (entry);
8298
8299   for (i = 0; i < MAX_ICONS; i++)
8300     {
8301       if ((icon_info = priv->icons[i]) != NULL)
8302         {
8303           if (icon_info->tooltip != NULL)
8304             {
8305               has_tooltip = TRUE;
8306               break;
8307             }
8308         }
8309     }
8310
8311   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8312 }
8313
8314 /**
8315  * gtk_entry_get_icon_tooltip_text:
8316  * @entry: a #GtkEntry
8317  * @icon_pos: the icon position
8318  *
8319  * Gets the contents of the tooltip on the icon at the specified 
8320  * position in @entry.
8321  * 
8322  * Returns: the tooltip text, or %NULL. Free the returned string
8323  *     with g_free() when done.
8324  * 
8325  * Since: 2.16
8326  */
8327 gchar *
8328 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8329                                  GtkEntryIconPosition  icon_pos)
8330 {
8331   GtkEntryPrivate *priv;
8332   EntryIconInfo *icon_info;
8333   gchar *text = NULL;
8334
8335   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8336   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8337
8338   priv = GTK_ENTRY_GET_PRIVATE (entry);
8339   icon_info = priv->icons[icon_pos];
8340
8341   if (!icon_info)
8342     return NULL;
8343  
8344   if (icon_info->tooltip && 
8345       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8346     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8347
8348   return text;
8349 }
8350
8351 /**
8352  * gtk_entry_set_icon_tooltip_text:
8353  * @entry: a #GtkEntry
8354  * @icon_pos: the icon position
8355  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8356  *
8357  * Sets @tooltip as the contents of the tooltip for the icon
8358  * at the specified position.
8359  *
8360  * Use %NULL for @tooltip to remove an existing tooltip.
8361  *
8362  * See also gtk_widget_set_tooltip_text() and 
8363  * gtk_entry_set_icon_tooltip_markup().
8364  *
8365  * Since: 2.16
8366  */
8367 void
8368 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8369                                  GtkEntryIconPosition  icon_pos,
8370                                  const gchar          *tooltip)
8371 {
8372   GtkEntryPrivate *priv;
8373   EntryIconInfo *icon_info;
8374
8375   g_return_if_fail (GTK_IS_ENTRY (entry));
8376   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8377
8378   priv = GTK_ENTRY_GET_PRIVATE (entry);
8379
8380   if (!(icon_info = priv->icons[icon_pos]))
8381     icon_info = priv->icons[icon_pos];
8382
8383   if (icon_info->tooltip)
8384     g_free (icon_info->tooltip);
8385
8386   /* Treat an empty string as a NULL string,
8387    * because an empty string would be useless for a tooltip:
8388    */
8389   if (tooltip && tooltip[0] == '\0')
8390     tooltip = NULL;
8391
8392   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8393
8394   ensure_has_tooltip (entry);
8395 }
8396
8397 /**
8398  * gtk_entry_get_icon_tooltip_markup:
8399  * @entry: a #GtkEntry
8400  * @icon_pos: the icon position
8401  *
8402  * Gets the contents of the tooltip on the icon at the specified 
8403  * position in @entry.
8404  * 
8405  * Returns: the tooltip text, or %NULL. Free the returned string
8406  *     with g_free() when done.
8407  * 
8408  * Since: 2.16
8409  */
8410 gchar *
8411 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8412                                    GtkEntryIconPosition  icon_pos)
8413 {
8414   GtkEntryPrivate *priv;
8415   EntryIconInfo *icon_info;
8416
8417   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8418   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8419
8420   priv = GTK_ENTRY_GET_PRIVATE (entry);
8421   icon_info = priv->icons[icon_pos];
8422
8423   if (!icon_info)
8424     return NULL;
8425  
8426   return g_strdup (icon_info->tooltip);
8427 }
8428
8429 /**
8430  * gtk_entry_set_icon_tooltip_markup:
8431  * @entry: a #GtkEntry
8432  * @icon_pos: the icon position
8433  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8434  *
8435  * Sets @tooltip as the contents of the tooltip for the icon at
8436  * the specified position. @tooltip is assumed to be marked up with
8437  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8438  *
8439  * Use %NULL for @tooltip to remove an existing tooltip.
8440  *
8441  * See also gtk_widget_set_tooltip_markup() and 
8442  * gtk_enty_set_icon_tooltip_text().
8443  *
8444  * Since: 2.16
8445  */
8446 void
8447 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8448                                    GtkEntryIconPosition  icon_pos,
8449                                    const gchar          *tooltip)
8450 {
8451   GtkEntryPrivate *priv;
8452   EntryIconInfo *icon_info;
8453
8454   g_return_if_fail (GTK_IS_ENTRY (entry));
8455   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8456
8457   priv = GTK_ENTRY_GET_PRIVATE (entry);
8458
8459   if (!(icon_info = priv->icons[icon_pos]))
8460     icon_info = priv->icons[icon_pos];
8461
8462   if (icon_info->tooltip)
8463     g_free (icon_info->tooltip);
8464
8465   /* Treat an empty string as a NULL string,
8466    * because an empty string would be useless for a tooltip:
8467    */
8468   if (tooltip && tooltip[0] == '\0')
8469     tooltip = NULL;
8470
8471   icon_info->tooltip = g_strdup (tooltip);
8472
8473   ensure_has_tooltip (entry);
8474 }
8475
8476 static gboolean
8477 gtk_entry_query_tooltip (GtkWidget  *widget,
8478                          gint        x,
8479                          gint        y,
8480                          gboolean    keyboard_tip,
8481                          GtkTooltip *tooltip)
8482 {
8483   GtkEntry *entry;
8484   GtkEntryPrivate *priv;
8485   EntryIconInfo *icon_info;
8486   gint icon_pos;
8487
8488   entry = GTK_ENTRY (widget);
8489   priv = GTK_ENTRY_GET_PRIVATE (entry);
8490
8491   if (!keyboard_tip)
8492     {
8493       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8494       if (icon_pos != -1)
8495         {
8496           if ((icon_info = priv->icons[icon_pos]) != NULL)
8497             {
8498               if (icon_info->tooltip)
8499                 {
8500                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8501                   return TRUE;
8502                 }
8503
8504               return FALSE;
8505             }
8506         }
8507     }
8508
8509   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8510                                                                    x, y,
8511                                                                    keyboard_tip,
8512                                                                    tooltip);
8513 }
8514
8515
8516 /* Quick hack of a popup menu
8517  */
8518 static void
8519 activate_cb (GtkWidget *menuitem,
8520              GtkEntry  *entry)
8521 {
8522   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8523   g_signal_emit_by_name (entry, signal);
8524 }
8525
8526
8527 static gboolean
8528 gtk_entry_mnemonic_activate (GtkWidget *widget,
8529                              gboolean   group_cycling)
8530 {
8531   gtk_widget_grab_focus (widget);
8532   return TRUE;
8533 }
8534
8535 static void
8536 append_action_signal (GtkEntry     *entry,
8537                       GtkWidget    *menu,
8538                       const gchar  *stock_id,
8539                       const gchar  *signal,
8540                       gboolean      sensitive)
8541 {
8542   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8543
8544   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8545   g_signal_connect (menuitem, "activate",
8546                     G_CALLBACK (activate_cb), entry);
8547
8548   gtk_widget_set_sensitive (menuitem, sensitive);
8549   
8550   gtk_widget_show (menuitem);
8551   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8552 }
8553         
8554 static void
8555 popup_menu_detach (GtkWidget *attach_widget,
8556                    GtkMenu   *menu)
8557 {
8558   GTK_ENTRY (attach_widget)->popup_menu = NULL;
8559 }
8560
8561 static void
8562 popup_position_func (GtkMenu   *menu,
8563                      gint      *x,
8564                      gint      *y,
8565                      gboolean  *push_in,
8566                      gpointer   user_data)
8567 {
8568   GtkEntry *entry = GTK_ENTRY (user_data);
8569   GtkWidget *widget = GTK_WIDGET (entry);
8570   GdkScreen *screen;
8571   GtkRequisition menu_req;
8572   GdkRectangle monitor;
8573   GtkBorder inner_border;
8574   gint monitor_num, strong_x, height;
8575  
8576   g_return_if_fail (gtk_widget_get_realized (widget));
8577
8578   gdk_window_get_origin (entry->text_area, x, y);
8579
8580   screen = gtk_widget_get_screen (widget);
8581   monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
8582   if (monitor_num < 0)
8583     monitor_num = 0;
8584   gtk_menu_set_monitor (menu, monitor_num);
8585
8586   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8587   gtk_widget_size_request (entry->popup_menu, &menu_req);
8588   gdk_drawable_get_size (entry->text_area, NULL, &height);
8589   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8590   _gtk_entry_effective_inner_border (entry, &inner_border);
8591
8592   *x += inner_border.left + strong_x - entry->scroll_offset;
8593   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8594     *x -= menu_req.width;
8595
8596   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8597     *y += height;
8598   else if ((*y - menu_req.height) >= monitor.y)
8599     *y -= menu_req.height;
8600   else if (monitor.y + monitor.height - (*y + height) > *y)
8601     *y += height;
8602   else
8603     *y -= menu_req.height;
8604
8605   *push_in = FALSE;
8606 }
8607
8608 static void
8609 unichar_chosen_func (const char *text,
8610                      gpointer    data)
8611 {
8612   GtkEntry *entry = GTK_ENTRY (data);
8613
8614   if (entry->editable)
8615     gtk_entry_enter_text (entry, text);
8616 }
8617
8618 typedef struct
8619 {
8620   GtkEntry *entry;
8621   gint button;
8622   guint time;
8623 } PopupInfo;
8624
8625 static void
8626 popup_targets_received (GtkClipboard     *clipboard,
8627                         GtkSelectionData *data,
8628                         gpointer          user_data)
8629 {
8630   PopupInfo *info = user_data;
8631   GtkEntry *entry = info->entry;
8632   
8633   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8634     {
8635       DisplayMode mode;
8636       gboolean clipboard_contains_text;
8637       GtkWidget *menuitem;
8638       GtkWidget *submenu;
8639       gboolean show_input_method_menu;
8640       gboolean show_unicode_menu;
8641       
8642       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8643       if (entry->popup_menu)
8644         gtk_widget_destroy (entry->popup_menu);
8645       
8646       entry->popup_menu = gtk_menu_new ();
8647       
8648       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
8649                                  GTK_WIDGET (entry),
8650                                  popup_menu_detach);
8651       
8652       mode = gtk_entry_get_display_mode (entry);
8653       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8654                             entry->editable && mode == DISPLAY_NORMAL &&
8655                             entry->current_pos != entry->selection_bound);
8656
8657       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8658                             mode == DISPLAY_NORMAL &&
8659                             entry->current_pos != entry->selection_bound);
8660
8661       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8662                             entry->editable && clipboard_contains_text);
8663
8664       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8665       gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
8666       g_signal_connect_swapped (menuitem, "activate",
8667                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8668       gtk_widget_show (menuitem);
8669       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8670
8671       menuitem = gtk_separator_menu_item_new ();
8672       gtk_widget_show (menuitem);
8673       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8674       
8675       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8676       g_signal_connect_swapped (menuitem, "activate",
8677                                 G_CALLBACK (gtk_entry_select_all), entry);
8678       gtk_widget_show (menuitem);
8679       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8680       
8681       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8682                     "gtk-show-input-method-menu", &show_input_method_menu,
8683                     "gtk-show-unicode-menu", &show_unicode_menu,
8684                     NULL);
8685
8686       if (show_input_method_menu || show_unicode_menu)
8687         {
8688           menuitem = gtk_separator_menu_item_new ();
8689           gtk_widget_show (menuitem);
8690           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8691         }
8692       
8693       if (show_input_method_menu)
8694         {
8695           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8696           gtk_widget_set_sensitive (menuitem, entry->editable);      
8697           gtk_widget_show (menuitem);
8698           submenu = gtk_menu_new ();
8699           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8700           
8701           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8702       
8703           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
8704                                                 GTK_MENU_SHELL (submenu));
8705         }
8706       
8707       if (show_unicode_menu)
8708         {
8709           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8710           gtk_widget_set_sensitive (menuitem, entry->editable);      
8711           gtk_widget_show (menuitem);
8712           
8713           submenu = gtk_menu_new ();
8714           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8715           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
8716           
8717           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8718                                                         unichar_chosen_func,
8719                                                         entry);
8720         }
8721       
8722       g_signal_emit (entry,
8723                      signals[POPULATE_POPUP],
8724                      0,
8725                      entry->popup_menu);
8726   
8727
8728       if (info->button)
8729         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8730                         NULL, NULL,
8731                         info->button, info->time);
8732       else
8733         {
8734           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8735                           popup_position_func, entry,
8736                           info->button, info->time);
8737           gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
8738         }
8739     }
8740
8741   g_object_unref (entry);
8742   g_slice_free (PopupInfo, info);
8743 }
8744                         
8745 static void
8746 gtk_entry_do_popup (GtkEntry       *entry,
8747                     GdkEventButton *event)
8748 {
8749   PopupInfo *info = g_slice_new (PopupInfo);
8750
8751   /* In order to know what entries we should make sensitive, we
8752    * ask for the current targets of the clipboard, and when
8753    * we get them, then we actually pop up the menu.
8754    */
8755   info->entry = g_object_ref (entry);
8756   
8757   if (event)
8758     {
8759       info->button = event->button;
8760       info->time = event->time;
8761     }
8762   else
8763     {
8764       info->button = 0;
8765       info->time = gtk_get_current_event_time ();
8766     }
8767
8768   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8769                                   gdk_atom_intern_static_string ("TARGETS"),
8770                                   popup_targets_received,
8771                                   info);
8772 }
8773
8774 static gboolean
8775 gtk_entry_popup_menu (GtkWidget *widget)
8776 {
8777   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8778   return TRUE;
8779 }
8780
8781 static void
8782 gtk_entry_drag_begin (GtkWidget      *widget,
8783                       GdkDragContext *context)
8784 {
8785   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8786   gint i;
8787
8788   for (i = 0; i < MAX_ICONS; i++)
8789     {
8790       EntryIconInfo *icon_info = priv->icons[i];
8791       
8792       if (icon_info != NULL)
8793         {
8794           if (icon_info->in_drag) 
8795             {
8796               switch (icon_info->storage_type)
8797                 {
8798                 case GTK_IMAGE_STOCK:
8799                   gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
8800                   break;
8801
8802                 case GTK_IMAGE_ICON_NAME:
8803                   gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
8804                   break;
8805
8806                   /* FIXME: No GIcon support for dnd icons */
8807                 case GTK_IMAGE_GICON:
8808                 case GTK_IMAGE_PIXBUF:
8809                   gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
8810                   break;
8811                 default:
8812                   g_assert_not_reached ();
8813                 }
8814             }
8815         }
8816     }
8817 }
8818
8819 static void
8820 gtk_entry_drag_end (GtkWidget      *widget,
8821                     GdkDragContext *context)
8822 {
8823   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8824   gint i;
8825
8826   for (i = 0; i < MAX_ICONS; i++)
8827     {
8828       EntryIconInfo *icon_info = priv->icons[i];
8829
8830       if (icon_info != NULL)
8831         icon_info->in_drag = 0;
8832     }
8833 }
8834
8835 static void
8836 gtk_entry_drag_leave (GtkWidget        *widget,
8837                       GdkDragContext   *context,
8838                       guint             time)
8839 {
8840   GtkEntry *entry = GTK_ENTRY (widget);
8841
8842   entry->dnd_position = -1;
8843   gtk_widget_queue_draw (widget);
8844 }
8845
8846 static gboolean
8847 gtk_entry_drag_drop  (GtkWidget        *widget,
8848                       GdkDragContext   *context,
8849                       gint              x,
8850                       gint              y,
8851                       guint             time)
8852 {
8853   GtkEntry *entry = GTK_ENTRY (widget);
8854   GdkAtom target = GDK_NONE;
8855   
8856   if (entry->editable)
8857     target = gtk_drag_dest_find_target (widget, context, NULL);
8858
8859   if (target != GDK_NONE)
8860     gtk_drag_get_data (widget, context, target, time);
8861   else
8862     gtk_drag_finish (context, FALSE, FALSE, time);
8863   
8864   return TRUE;
8865 }
8866
8867 static gboolean
8868 gtk_entry_drag_motion (GtkWidget        *widget,
8869                        GdkDragContext   *context,
8870                        gint              x,
8871                        gint              y,
8872                        guint             time)
8873 {
8874   GtkEntry *entry = GTK_ENTRY (widget);
8875   GtkWidget *source_widget;
8876   GdkDragAction suggested_action;
8877   gint new_position, old_position;
8878   gint sel1, sel2;
8879   
8880   x -= widget->style->xthickness;
8881   y -= widget->style->ythickness;
8882   
8883   old_position = entry->dnd_position;
8884   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8885
8886   if (entry->editable &&
8887       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8888     {
8889       source_widget = gtk_drag_get_source_widget (context);
8890       suggested_action = context->suggested_action;
8891
8892       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8893           new_position < sel1 || new_position > sel2)
8894         {
8895           if (source_widget == widget)
8896             {
8897               /* Default to MOVE, unless the user has
8898                * pressed ctrl or alt to affect available actions
8899                */
8900               if ((context->actions & GDK_ACTION_MOVE) != 0)
8901                 suggested_action = GDK_ACTION_MOVE;
8902             }
8903               
8904           entry->dnd_position = new_position;
8905         }
8906       else
8907         {
8908           if (source_widget == widget)
8909             suggested_action = 0;       /* Can't drop in selection where drag started */
8910           
8911           entry->dnd_position = -1;
8912         }
8913     }
8914   else
8915     {
8916       /* Entry not editable, or no text */
8917       suggested_action = 0;
8918       entry->dnd_position = -1;
8919     }
8920   
8921   gdk_drag_status (context, suggested_action, time);
8922   
8923   if (entry->dnd_position != old_position)
8924     gtk_widget_queue_draw (widget);
8925
8926   return TRUE;
8927 }
8928
8929 static void
8930 gtk_entry_drag_data_received (GtkWidget        *widget,
8931                               GdkDragContext   *context,
8932                               gint              x,
8933                               gint              y,
8934                               GtkSelectionData *selection_data,
8935                               guint             info,
8936                               guint             time)
8937 {
8938   GtkEntry *entry = GTK_ENTRY (widget);
8939   GtkEditable *editable = GTK_EDITABLE (widget);
8940   gchar *str;
8941
8942   str = (gchar *) gtk_selection_data_get_text (selection_data);
8943
8944   x -= widget->style->xthickness;
8945   y -= widget->style->ythickness;
8946
8947   if (str && entry->editable)
8948     {
8949       gint new_position;
8950       gint sel1, sel2;
8951       gint length = -1;
8952
8953       if (entry->truncate_multiline)
8954         length = truncate_multiline (str);
8955
8956       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8957
8958       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8959           new_position < sel1 || new_position > sel2)
8960         {
8961           gtk_editable_insert_text (editable, str, length, &new_position);
8962         }
8963       else
8964         {
8965           /* Replacing selection */
8966           begin_change (entry);
8967           g_object_freeze_notify (G_OBJECT (entry));
8968           gtk_editable_delete_text (editable, sel1, sel2);
8969           gtk_editable_insert_text (editable, str, length, &sel1);
8970           g_object_thaw_notify (G_OBJECT (entry));
8971           end_change (entry);
8972         }
8973       
8974       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
8975     }
8976   else
8977     {
8978       /* Drag and drop didn't happen! */
8979       gtk_drag_finish (context, FALSE, FALSE, time);
8980     }
8981
8982   g_free (str);
8983 }
8984
8985 static void
8986 gtk_entry_drag_data_get (GtkWidget        *widget,
8987                          GdkDragContext   *context,
8988                          GtkSelectionData *selection_data,
8989                          guint             info,
8990                          guint             time)
8991 {
8992   gint sel_start, sel_end;
8993   gint i;
8994
8995   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8996   GtkEditable *editable = GTK_EDITABLE (widget);
8997
8998   /* If there is an icon drag going on, exit early. */
8999   for (i = 0; i < MAX_ICONS; i++)
9000     {
9001       EntryIconInfo *icon_info = priv->icons[i];
9002
9003       if (icon_info != NULL)
9004         {
9005           if (icon_info->in_drag)
9006             return;
9007         }
9008     }
9009
9010   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9011     {
9012       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9013
9014       gtk_selection_data_set_text (selection_data, str, -1);
9015       
9016       g_free (str);
9017     }
9018
9019 }
9020
9021 static void
9022 gtk_entry_drag_data_delete (GtkWidget      *widget,
9023                             GdkDragContext *context)
9024 {
9025   gint sel_start, sel_end;
9026   gint i;
9027
9028   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
9029   GtkEditable *editable = GTK_EDITABLE (widget);
9030
9031   /* If there is an icon drag going on, exit early. */
9032   for (i = 0; i < MAX_ICONS; i++)
9033     {
9034       EntryIconInfo *icon_info = priv->icons[i];
9035
9036       if (icon_info != NULL)
9037         {
9038           if (icon_info->in_drag)
9039             return;
9040         }
9041     }
9042   
9043   if (GTK_ENTRY (widget)->editable &&
9044       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9045     gtk_editable_delete_text (editable, sel_start, sel_end);
9046 }
9047
9048 /* We display the cursor when
9049  *
9050  *  - the selection is empty, AND
9051  *  - the widget has focus
9052  */
9053
9054 #define CURSOR_ON_MULTIPLIER 2
9055 #define CURSOR_OFF_MULTIPLIER 1
9056 #define CURSOR_PEND_MULTIPLIER 3
9057 #define CURSOR_DIVIDER 3
9058
9059 static gboolean
9060 cursor_blinks (GtkEntry *entry)
9061 {
9062   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9063       entry->editable &&
9064       entry->selection_bound == entry->current_pos)
9065     {
9066       GtkSettings *settings;
9067       gboolean blink;
9068
9069       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9070       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9071
9072       return blink;
9073     }
9074   else
9075     return FALSE;
9076 }
9077
9078 static gint
9079 get_cursor_time (GtkEntry *entry)
9080 {
9081   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9082   gint time;
9083
9084   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9085
9086   return time;
9087 }
9088
9089 static gint
9090 get_cursor_blink_timeout (GtkEntry *entry)
9091 {
9092   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9093   gint timeout;
9094
9095   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9096
9097   return timeout;
9098 }
9099
9100 static void
9101 show_cursor (GtkEntry *entry)
9102 {
9103   GtkWidget *widget;
9104
9105   if (!entry->cursor_visible)
9106     {
9107       entry->cursor_visible = TRUE;
9108
9109       widget = GTK_WIDGET (entry);
9110       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
9111         gtk_widget_queue_draw (widget);
9112     }
9113 }
9114
9115 static void
9116 hide_cursor (GtkEntry *entry)
9117 {
9118   GtkWidget *widget;
9119
9120   if (entry->cursor_visible)
9121     {
9122       entry->cursor_visible = FALSE;
9123
9124       widget = GTK_WIDGET (entry);
9125       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
9126         gtk_widget_queue_draw (widget);
9127     }
9128 }
9129
9130 /*
9131  * Blink!
9132  */
9133 static gint
9134 blink_cb (gpointer data)
9135 {
9136   GtkEntry *entry;
9137   GtkEntryPrivate *priv; 
9138   gint blink_timeout;
9139
9140   entry = GTK_ENTRY (data);
9141   priv = GTK_ENTRY_GET_PRIVATE (entry);
9142  
9143   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9144     {
9145       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9146                  "connect a handler to this signal, it must return\n"
9147                  "FALSE so the entry gets the event as well");
9148
9149       gtk_entry_check_cursor_blink (entry);
9150
9151       return FALSE;
9152     }
9153   
9154   g_assert (entry->selection_bound == entry->current_pos);
9155   
9156   blink_timeout = get_cursor_blink_timeout (entry);
9157   if (priv->blink_time > 1000 * blink_timeout && 
9158       blink_timeout < G_MAXINT/1000) 
9159     {
9160       /* we've blinked enough without the user doing anything, stop blinking */
9161       show_cursor (entry);
9162       entry->blink_timeout = 0;
9163     } 
9164   else if (entry->cursor_visible)
9165     {
9166       hide_cursor (entry);
9167       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9168                                             blink_cb,
9169                                             entry);
9170     }
9171   else
9172     {
9173       show_cursor (entry);
9174       priv->blink_time += get_cursor_time (entry);
9175       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9176                                             blink_cb,
9177                                             entry);
9178     }
9179
9180   /* Remove ourselves */
9181   return FALSE;
9182 }
9183
9184 static void
9185 gtk_entry_check_cursor_blink (GtkEntry *entry)
9186 {
9187   GtkEntryPrivate *priv; 
9188   
9189   priv = GTK_ENTRY_GET_PRIVATE (entry);
9190
9191   if (cursor_blinks (entry))
9192     {
9193       if (!entry->blink_timeout)
9194         {
9195           show_cursor (entry);
9196           entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9197                                                 blink_cb,
9198                                                 entry);
9199         }
9200     }
9201   else
9202     {
9203       if (entry->blink_timeout)  
9204         { 
9205           g_source_remove (entry->blink_timeout);
9206           entry->blink_timeout = 0;
9207         }
9208       
9209       entry->cursor_visible = TRUE;
9210     }
9211   
9212 }
9213
9214 static void
9215 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9216 {
9217   if (cursor_blinks (entry))
9218     {
9219       if (entry->blink_timeout != 0)
9220         g_source_remove (entry->blink_timeout);
9221       
9222       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9223                                             blink_cb,
9224                                             entry);
9225       show_cursor (entry);
9226     }
9227 }
9228
9229 static void
9230 gtk_entry_reset_blink_time (GtkEntry *entry)
9231 {
9232   GtkEntryPrivate *priv; 
9233   
9234   priv = GTK_ENTRY_GET_PRIVATE (entry);
9235   
9236   priv->blink_time = 0;
9237 }
9238
9239
9240 /* completion */
9241 static gint
9242 gtk_entry_completion_timeout (gpointer data)
9243 {
9244   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9245
9246   completion->priv->completion_timeout = 0;
9247
9248   if (completion->priv->filter_model &&
9249       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9250       >= completion->priv->minimum_key_length)
9251     {
9252       gint matches;
9253       gint actions;
9254       GtkTreeSelection *s;
9255       gboolean popup_single;
9256
9257       gtk_entry_completion_complete (completion);
9258       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9259
9260       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9261
9262       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9263
9264       gtk_tree_selection_unselect_all (s);
9265
9266       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9267
9268       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9269       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9270         { 
9271           if (gtk_widget_get_visible (completion->priv->popup_window))
9272             _gtk_entry_completion_resize_popup (completion);
9273           else
9274             _gtk_entry_completion_popup (completion);
9275         }
9276       else 
9277         _gtk_entry_completion_popdown (completion);
9278     }
9279   else if (gtk_widget_get_visible (completion->priv->popup_window))
9280     _gtk_entry_completion_popdown (completion);
9281
9282   return FALSE;
9283 }
9284
9285 static inline gboolean
9286 keyval_is_cursor_move (guint keyval)
9287 {
9288   if (keyval == GDK_Up || keyval == GDK_KP_Up)
9289     return TRUE;
9290
9291   if (keyval == GDK_Down || keyval == GDK_KP_Down)
9292     return TRUE;
9293
9294   if (keyval == GDK_Page_Up)
9295     return TRUE;
9296
9297   if (keyval == GDK_Page_Down)
9298     return TRUE;
9299
9300   return FALSE;
9301 }
9302
9303 static gboolean
9304 gtk_entry_completion_key_press (GtkWidget   *widget,
9305                                 GdkEventKey *event,
9306                                 gpointer     user_data)
9307 {
9308   gint matches, actions = 0;
9309   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9310
9311   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9312     return FALSE;
9313
9314   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9315
9316   if (completion->priv->actions)
9317     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9318
9319   if (keyval_is_cursor_move (event->keyval))
9320     {
9321       GtkTreePath *path = NULL;
9322       
9323       if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
9324         {
9325           if (completion->priv->current_selected < 0)
9326             completion->priv->current_selected = matches + actions - 1;
9327           else
9328             completion->priv->current_selected--;
9329         }
9330       else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
9331         {
9332           if (completion->priv->current_selected < matches + actions - 1)
9333             completion->priv->current_selected++;
9334           else
9335             completion->priv->current_selected = -1;
9336         }
9337       else if (event->keyval == GDK_Page_Up)
9338         {
9339           if (completion->priv->current_selected < 0)
9340             completion->priv->current_selected = matches + actions - 1;
9341           else if (completion->priv->current_selected == 0)
9342             completion->priv->current_selected = -1;
9343           else if (completion->priv->current_selected < matches) 
9344             {
9345               completion->priv->current_selected -= 14;
9346               if (completion->priv->current_selected < 0)
9347                 completion->priv->current_selected = 0;
9348             }
9349           else 
9350             {
9351               completion->priv->current_selected -= 14;
9352               if (completion->priv->current_selected < matches - 1)
9353                 completion->priv->current_selected = matches - 1;
9354             }
9355         }
9356       else if (event->keyval == GDK_Page_Down)
9357         {
9358           if (completion->priv->current_selected < 0)
9359             completion->priv->current_selected = 0;
9360           else if (completion->priv->current_selected < matches - 1)
9361             {
9362               completion->priv->current_selected += 14;
9363               if (completion->priv->current_selected > matches - 1)
9364                 completion->priv->current_selected = matches - 1;
9365             }
9366           else if (completion->priv->current_selected == matches + actions - 1)
9367             {
9368               completion->priv->current_selected = -1;
9369             }
9370           else
9371             {
9372               completion->priv->current_selected += 14;
9373               if (completion->priv->current_selected > matches + actions - 1)
9374                 completion->priv->current_selected = matches + actions - 1;
9375             }
9376         }
9377
9378       if (completion->priv->current_selected < 0)
9379         {
9380           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9381           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9382
9383           if (completion->priv->inline_selection &&
9384               completion->priv->completion_prefix)
9385             {
9386               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9387                                   completion->priv->completion_prefix);
9388               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9389             }
9390         }
9391       else if (completion->priv->current_selected < matches)
9392         {
9393           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9394
9395           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9396           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9397                                     path, NULL, FALSE);
9398
9399           if (completion->priv->inline_selection)
9400             {
9401
9402               GtkTreeIter iter;
9403               GtkTreeModel *model = NULL;
9404               GtkTreeSelection *sel;
9405               gboolean entry_set;
9406
9407               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9408               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9409                 return FALSE;
9410               
9411               if (completion->priv->completion_prefix == NULL)
9412                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9413
9414               g_signal_emit_by_name (completion, "cursor-on-match", model,
9415                                      &iter, &entry_set);
9416             }
9417         }
9418       else if (completion->priv->current_selected - matches >= 0)
9419         {
9420           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9421
9422           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9423           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9424                                     path, NULL, FALSE);
9425
9426           if (completion->priv->inline_selection &&
9427               completion->priv->completion_prefix)
9428             {
9429               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9430                                   completion->priv->completion_prefix);
9431               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9432             }
9433         }
9434
9435       gtk_tree_path_free (path);
9436
9437       return TRUE;
9438     }
9439   else if (event->keyval == GDK_Escape ||
9440            event->keyval == GDK_Left ||
9441            event->keyval == GDK_KP_Left ||
9442            event->keyval == GDK_Right ||
9443            event->keyval == GDK_KP_Right) 
9444     {
9445       gboolean retval = TRUE;
9446
9447       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9448       _gtk_entry_completion_popdown (completion);
9449
9450       if (completion->priv->current_selected < 0)
9451         {
9452           retval = FALSE;
9453           goto keypress_completion_out;
9454         }
9455       else if (completion->priv->inline_selection)
9456         {
9457           /* Escape rejects the tentative completion */
9458           if (event->keyval == GDK_Escape)
9459             {
9460               if (completion->priv->completion_prefix)
9461                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9462                                     completion->priv->completion_prefix);
9463               else 
9464                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9465             }
9466
9467           /* Move the cursor to the end for Right/Esc, to the
9468              beginning for Left */
9469           if (event->keyval == GDK_Right ||
9470               event->keyval == GDK_KP_Right ||
9471               event->keyval == GDK_Escape)
9472             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9473           else
9474             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9475         }
9476
9477 keypress_completion_out:
9478       if (completion->priv->inline_selection)
9479         {
9480           g_free (completion->priv->completion_prefix);
9481           completion->priv->completion_prefix = NULL;
9482         }
9483
9484       return retval;
9485     }
9486   else if (event->keyval == GDK_Tab || 
9487            event->keyval == GDK_KP_Tab ||
9488            event->keyval == GDK_ISO_Left_Tab) 
9489     {
9490       GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ? 
9491         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
9492       
9493       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9494       _gtk_entry_completion_popdown (completion);
9495
9496       g_free (completion->priv->completion_prefix);
9497       completion->priv->completion_prefix = NULL;
9498
9499       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
9500
9501       return TRUE;
9502     }
9503   else if (event->keyval == GDK_ISO_Enter ||
9504            event->keyval == GDK_KP_Enter ||
9505            event->keyval == GDK_Return)
9506     {
9507       GtkTreeIter iter;
9508       GtkTreeModel *model = NULL;
9509       GtkTreeSelection *sel;
9510       gboolean retval = TRUE;
9511
9512       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9513       _gtk_entry_completion_popdown (completion);
9514
9515       if (completion->priv->current_selected < matches)
9516         {
9517           gboolean entry_set;
9518
9519           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9520           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9521             {
9522               g_signal_handler_block (widget, completion->priv->changed_id);
9523               g_signal_emit_by_name (completion, "match-selected",
9524                                      model, &iter, &entry_set);
9525               g_signal_handler_unblock (widget, completion->priv->changed_id);
9526
9527               if (!entry_set)
9528                 {
9529                   gchar *str = NULL;
9530
9531                   gtk_tree_model_get (model, &iter,
9532                                       completion->priv->text_column, &str,
9533                                       -1);
9534
9535                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9536
9537                   /* move the cursor to the end */
9538                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9539
9540                   g_free (str);
9541                 }
9542             }
9543           else
9544             retval = FALSE;
9545         }
9546       else if (completion->priv->current_selected - matches >= 0)
9547         {
9548           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9549           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9550             {
9551               GtkTreePath *path;
9552
9553               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9554               g_signal_emit_by_name (completion, "action-activated",
9555                                      gtk_tree_path_get_indices (path)[0]);
9556               gtk_tree_path_free (path);
9557             }
9558           else
9559             retval = FALSE;
9560         }
9561
9562       g_free (completion->priv->completion_prefix);
9563       completion->priv->completion_prefix = NULL;
9564
9565       return retval;
9566     }
9567
9568   return FALSE;
9569 }
9570
9571 static void
9572 gtk_entry_completion_changed (GtkWidget *entry,
9573                               gpointer   user_data)
9574 {
9575   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9576
9577   /* (re)install completion timeout */
9578   if (completion->priv->completion_timeout)
9579     g_source_remove (completion->priv->completion_timeout);
9580
9581   if (!gtk_entry_get_text (GTK_ENTRY (entry)))
9582     return;
9583
9584   /* no need to normalize for this test */
9585   if (completion->priv->minimum_key_length > 0 &&
9586       strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
9587     {
9588       if (gtk_widget_get_visible (completion->priv->popup_window))
9589         _gtk_entry_completion_popdown (completion);
9590       return;
9591     }
9592
9593   completion->priv->completion_timeout =
9594     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9595                    gtk_entry_completion_timeout,
9596                    completion);
9597 }
9598
9599 static gboolean
9600 check_completion_callback (GtkEntryCompletion *completion)
9601 {
9602   completion->priv->check_completion_idle = NULL;
9603   
9604   gtk_entry_completion_complete (completion);
9605   gtk_entry_completion_insert_prefix (completion);
9606
9607   return FALSE;
9608 }
9609
9610 static void
9611 clear_completion_callback (GtkEntry   *entry,
9612                            GParamSpec *pspec)
9613 {
9614   if (pspec->name == I_("cursor-position") ||
9615       pspec->name == I_("selection-bound"))
9616     {
9617       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9618       
9619       completion->priv->has_completion = FALSE;
9620     }
9621 }
9622
9623 static gboolean
9624 accept_completion_callback (GtkEntry *entry)
9625 {
9626   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9627
9628   if (completion->priv->has_completion)
9629     gtk_editable_set_position (GTK_EDITABLE (entry),
9630                                gtk_entry_buffer_get_length (get_buffer (entry)));
9631
9632   return FALSE;
9633 }
9634
9635 static void
9636 completion_insert_text_callback (GtkEntry           *entry,
9637                                  const gchar        *text,
9638                                  gint                length,
9639                                  gint                position,
9640                                  GtkEntryCompletion *completion)
9641 {
9642   /* idle to update the selection based on the file list */
9643   if (completion->priv->check_completion_idle == NULL)
9644     {
9645       completion->priv->check_completion_idle = g_idle_source_new ();
9646       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9647       g_source_set_closure (completion->priv->check_completion_idle,
9648                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9649                                                    G_OBJECT (completion)));
9650       g_source_attach (completion->priv->check_completion_idle, NULL);
9651     }
9652 }
9653
9654 static void
9655 completion_changed (GtkEntryCompletion *completion,
9656                     GParamSpec         *pspec,
9657                     gpointer            data)
9658 {
9659   GtkEntry *entry = GTK_ENTRY (data);
9660
9661   if (pspec->name == I_("popup-completion") ||
9662       pspec->name == I_("inline-completion"))
9663     {
9664       disconnect_completion_signals (entry, completion);
9665       connect_completion_signals (entry, completion);
9666     }
9667 }
9668
9669 static void
9670 disconnect_completion_signals (GtkEntry           *entry,
9671                                GtkEntryCompletion *completion)
9672 {
9673   g_signal_handlers_disconnect_by_func (completion, 
9674                                        G_CALLBACK (completion_changed), entry);
9675   if (completion->priv->changed_id > 0 &&
9676       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9677     {
9678       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9679       completion->priv->changed_id = 0;
9680     }
9681   g_signal_handlers_disconnect_by_func (entry, 
9682                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9683   if (completion->priv->insert_text_id > 0 &&
9684       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9685     {
9686       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9687       completion->priv->insert_text_id = 0;
9688     }
9689   g_signal_handlers_disconnect_by_func (entry, 
9690                                         G_CALLBACK (completion_insert_text_callback), completion);
9691   g_signal_handlers_disconnect_by_func (entry, 
9692                                         G_CALLBACK (clear_completion_callback), completion);
9693   g_signal_handlers_disconnect_by_func (entry, 
9694                                         G_CALLBACK (accept_completion_callback), completion);
9695 }
9696
9697 static void
9698 connect_completion_signals (GtkEntry           *entry,
9699                             GtkEntryCompletion *completion)
9700 {
9701   if (completion->priv->popup_completion)
9702     {
9703       completion->priv->changed_id =
9704         g_signal_connect (entry, "changed",
9705                           G_CALLBACK (gtk_entry_completion_changed), completion);
9706       g_signal_connect (entry, "key-press-event",
9707                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9708     }
9709  
9710   if (completion->priv->inline_completion)
9711     {
9712       completion->priv->insert_text_id =
9713         g_signal_connect (entry, "insert-text",
9714                           G_CALLBACK (completion_insert_text_callback), completion);
9715       g_signal_connect (entry, "notify",
9716                         G_CALLBACK (clear_completion_callback), completion);
9717       g_signal_connect (entry, "activate",
9718                         G_CALLBACK (accept_completion_callback), completion);
9719       g_signal_connect (entry, "focus-out-event",
9720                         G_CALLBACK (accept_completion_callback), completion);
9721     }
9722
9723   g_signal_connect (completion, "notify",
9724                     G_CALLBACK (completion_changed), entry);
9725 }
9726
9727 /**
9728  * gtk_entry_set_completion:
9729  * @entry: A #GtkEntry
9730  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9731  *
9732  * Sets @completion to be the auxiliary completion object to use with @entry.
9733  * All further configuration of the completion mechanism is done on
9734  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9735  * @completion is set to %NULL.
9736  *
9737  * Since: 2.4
9738  */
9739 void
9740 gtk_entry_set_completion (GtkEntry           *entry,
9741                           GtkEntryCompletion *completion)
9742 {
9743   GtkEntryCompletion *old;
9744
9745   g_return_if_fail (GTK_IS_ENTRY (entry));
9746   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9747
9748   old = gtk_entry_get_completion (entry);
9749
9750   if (old == completion)
9751     return;
9752   
9753   if (old)
9754     {
9755       if (old->priv->completion_timeout)
9756         {
9757           g_source_remove (old->priv->completion_timeout);
9758           old->priv->completion_timeout = 0;
9759         }
9760
9761       if (gtk_widget_get_mapped (old->priv->popup_window))
9762         _gtk_entry_completion_popdown (old);
9763
9764       disconnect_completion_signals (entry, old);
9765       old->priv->entry = NULL;
9766
9767       g_object_unref (old);
9768     }
9769
9770   if (!completion)
9771     {
9772       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9773       return;
9774     }
9775
9776   /* hook into the entry */
9777   g_object_ref (completion);
9778
9779   connect_completion_signals (entry, completion);    
9780   completion->priv->entry = GTK_WIDGET (entry);
9781   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9782 }
9783
9784 /**
9785  * gtk_entry_get_completion:
9786  * @entry: A #GtkEntry
9787  *
9788  * Returns the auxiliary completion object currently in use by @entry.
9789  *
9790  * Return value: The auxiliary completion object currently in use by @entry.
9791  *
9792  * Since: 2.4
9793  */
9794 GtkEntryCompletion *
9795 gtk_entry_get_completion (GtkEntry *entry)
9796 {
9797   GtkEntryCompletion *completion;
9798
9799   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9800
9801   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9802                                      GTK_ENTRY_COMPLETION_KEY));
9803
9804   return completion;
9805 }
9806
9807 /**
9808  * gtk_entry_set_cursor_hadjustment:
9809  * @entry: a #GtkEntry
9810  * @adjustment: an adjustment which should be adjusted when the cursor 
9811  *              is moved, or %NULL
9812  *
9813  * Hooks up an adjustment to the cursor position in an entry, so that when 
9814  * the cursor is moved, the adjustment is scrolled to show that position. 
9815  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9816  * the adjustment.
9817  *
9818  * The adjustment has to be in pixel units and in the same coordinate system 
9819  * as the entry. 
9820  * 
9821  * Since: 2.12
9822  */
9823 void
9824 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9825                                   GtkAdjustment *adjustment)
9826 {
9827   g_return_if_fail (GTK_IS_ENTRY (entry));
9828   if (adjustment)
9829     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9830
9831   if (adjustment)
9832     g_object_ref (adjustment);
9833
9834   g_object_set_qdata_full (G_OBJECT (entry), 
9835                            quark_cursor_hadjustment,
9836                            adjustment, 
9837                            g_object_unref);
9838 }
9839
9840 /**
9841  * gtk_entry_get_cursor_hadjustment:
9842  * @entry: a #GtkEntry
9843  *
9844  * Retrieves the horizontal cursor adjustment for the entry. 
9845  * See gtk_entry_set_cursor_hadjustment().
9846  *
9847  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9848  *   if none has been set.
9849  *
9850  * Since: 2.12
9851  */
9852 GtkAdjustment*
9853 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9854 {
9855   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9856
9857   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9858 }
9859
9860 /**
9861  * gtk_entry_set_progress_fraction:
9862  * @entry: a #GtkEntry
9863  * @fraction: fraction of the task that's been completed
9864  *
9865  * Causes the entry's progress indicator to "fill in" the given
9866  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9867  * inclusive.
9868  *
9869  * Since: 2.16
9870  */
9871 void
9872 gtk_entry_set_progress_fraction (GtkEntry *entry,
9873                                  gdouble   fraction)
9874 {
9875   GtkWidget       *widget;
9876   GtkEntryPrivate *private;
9877   gdouble          old_fraction;
9878   gint x, y, width, height;
9879   gint old_x, old_y, old_width, old_height;
9880
9881   g_return_if_fail (GTK_IS_ENTRY (entry));
9882
9883   widget = GTK_WIDGET (entry);
9884   private = GTK_ENTRY_GET_PRIVATE (entry);
9885
9886   if (private->progress_pulse_mode)
9887     old_fraction = -1;
9888   else
9889     old_fraction = private->progress_fraction;
9890
9891   if (gtk_widget_is_drawable (widget))
9892     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9893
9894   fraction = CLAMP (fraction, 0.0, 1.0);
9895
9896   private->progress_fraction = fraction;
9897   private->progress_pulse_mode = FALSE;
9898   private->progress_pulse_current = 0.0;
9899
9900   if (gtk_widget_is_drawable (widget))
9901     {
9902       get_progress_area (widget, &x, &y, &width, &height);
9903
9904       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9905         gtk_widget_queue_draw (widget);
9906     }
9907
9908   if (fraction != old_fraction)
9909     g_object_notify (G_OBJECT (entry), "progress-fraction");
9910 }
9911
9912 /**
9913  * gtk_entry_get_progress_fraction:
9914  * @entry: a #GtkEntry
9915  *
9916  * Returns the current fraction of the task that's been completed.
9917  * See gtk_entry_set_progress_fraction().
9918  *
9919  * Return value: a fraction from 0.0 to 1.0
9920  *
9921  * Since: 2.16
9922  */
9923 gdouble
9924 gtk_entry_get_progress_fraction (GtkEntry *entry)
9925 {
9926   GtkEntryPrivate *private;
9927
9928   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9929
9930   private = GTK_ENTRY_GET_PRIVATE (entry);
9931
9932   return private->progress_fraction;
9933 }
9934
9935 /**
9936  * gtk_entry_set_progress_pulse_step:
9937  * @entry: a #GtkEntry
9938  * @fraction: fraction between 0.0 and 1.0
9939  *
9940  * Sets the fraction of total entry width to move the progress
9941  * bouncing block for each call to gtk_entry_progress_pulse().
9942  *
9943  * Since: 2.16
9944  */
9945 void
9946 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9947                                    gdouble   fraction)
9948 {
9949   GtkEntryPrivate *private;
9950
9951   g_return_if_fail (GTK_IS_ENTRY (entry));
9952
9953   private = GTK_ENTRY_GET_PRIVATE (entry);
9954
9955   fraction = CLAMP (fraction, 0.0, 1.0);
9956
9957   if (fraction != private->progress_pulse_fraction)
9958     {
9959       private->progress_pulse_fraction = fraction;
9960
9961       gtk_widget_queue_draw (GTK_WIDGET (entry));
9962
9963       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
9964     }
9965 }
9966
9967 /**
9968  * gtk_entry_get_progress_pulse_step:
9969  * @entry: a #GtkEntry
9970  *
9971  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
9972  *
9973  * Return value: a fraction from 0.0 to 1.0
9974  *
9975  * Since: 2.16
9976  */
9977 gdouble
9978 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
9979 {
9980   GtkEntryPrivate *private;
9981
9982   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9983
9984   private = GTK_ENTRY_GET_PRIVATE (entry);
9985
9986   return private->progress_pulse_fraction;
9987 }
9988
9989 /**
9990  * gtk_entry_progress_pulse:
9991  * @entry: a #GtkEntry
9992  *
9993  * Indicates that some progress is made, but you don't know how much.
9994  * Causes the entry's progress indicator to enter "activity mode,"
9995  * where a block bounces back and forth. Each call to
9996  * gtk_entry_progress_pulse() causes the block to move by a little bit
9997  * (the amount of movement per pulse is determined by
9998  * gtk_entry_set_progress_pulse_step()).
9999  *
10000  * Since: 2.16
10001  */
10002 void
10003 gtk_entry_progress_pulse (GtkEntry *entry)
10004 {
10005   GtkEntryPrivate *private;
10006
10007   g_return_if_fail (GTK_IS_ENTRY (entry));
10008
10009   private = GTK_ENTRY_GET_PRIVATE (entry);
10010
10011   if (private->progress_pulse_mode)
10012     {
10013       if (private->progress_pulse_way_back)
10014         {
10015           private->progress_pulse_current -= private->progress_pulse_fraction;
10016
10017           if (private->progress_pulse_current < 0.0)
10018             {
10019               private->progress_pulse_current = 0.0;
10020               private->progress_pulse_way_back = FALSE;
10021             }
10022         }
10023       else
10024         {
10025           private->progress_pulse_current += private->progress_pulse_fraction;
10026
10027           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10028             {
10029               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10030               private->progress_pulse_way_back = TRUE;
10031             }
10032         }
10033     }
10034   else
10035     {
10036       private->progress_fraction = 0.0;
10037       private->progress_pulse_mode = TRUE;
10038       private->progress_pulse_way_back = FALSE;
10039       private->progress_pulse_current = 0.0;
10040     }
10041
10042   gtk_widget_queue_draw (GTK_WIDGET (entry));
10043 }
10044
10045 /* Caps Lock warning for password entries */
10046
10047 static void
10048 show_capslock_feedback (GtkEntry    *entry,
10049                         const gchar *text)
10050 {
10051   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
10052
10053   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10054     {
10055       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10056       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10057       priv->caps_lock_warning_shown = TRUE;
10058     }
10059
10060   if (priv->caps_lock_warning_shown)
10061     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10062   else
10063     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10064 }
10065
10066 static void
10067 remove_capslock_feedback (GtkEntry *entry)
10068 {
10069   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
10070
10071   if (priv->caps_lock_warning_shown)
10072     {
10073       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10074       priv->caps_lock_warning_shown = FALSE;
10075     } 
10076 }
10077
10078 static void
10079 keymap_state_changed (GdkKeymap *keymap, 
10080                       GtkEntry  *entry)
10081 {
10082   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
10083   char *text = NULL;
10084
10085   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10086     { 
10087       if (gdk_keymap_get_caps_lock_state (keymap))
10088         text = _("Caps Lock is on");
10089     }
10090
10091   if (text)
10092     show_capslock_feedback (entry, text);
10093   else
10094     remove_capslock_feedback (entry);
10095 }
10096
10097 #define __GTK_ENTRY_C__
10098 #include "gtkaliasdef.c"