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