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