]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
gtk: add gtk_entry_get_text_area() and gtk_entry_get_icon_area()
[~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  * Returns 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  * See also gtk_entry_get_icon_area().
6667  *
6668  * Since: 3.0
6669  **/
6670 void
6671 gtk_entry_get_text_area (GtkEntry     *entry,
6672                          GdkRectangle *text_area)
6673 {
6674   g_return_if_fail (GTK_IS_ENTRY (entry));
6675   g_return_if_fail (text_area != NULL);
6676
6677   if (entry->text_area)
6678     {
6679       GtkAllocation allocation;
6680       gint x, y;
6681
6682       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6683       gdk_window_get_position (entry->text_area, &x, &y);
6684
6685       text_area->x = x - allocation.x;
6686       text_area->y = y - allocation.y;
6687       text_area->width = gdk_window_get_width (entry->text_area);
6688       text_area->height = gdk_window_get_height (entry->text_area);
6689     }
6690   else
6691     {
6692       text_area->x = 0;
6693       text_area->y = 0;
6694       text_area->width = 0;
6695       text_area->height = 0;
6696     }
6697 }
6698
6699 /**
6700  * gtk_entry_set_text:
6701  * @entry: a #GtkEntry
6702  * @text: the new text
6703  *
6704  * Sets the text in the widget to the given
6705  * value, replacing the current contents.
6706  *
6707  * See gtk_entry_buffer_set_text().
6708  */
6709 void
6710 gtk_entry_set_text (GtkEntry    *entry,
6711                     const gchar *text)
6712 {
6713   gint tmp_pos;
6714   GtkEntryCompletion *completion;
6715   GtkEntryPrivate *priv;
6716
6717   g_return_if_fail (GTK_IS_ENTRY (entry));
6718   g_return_if_fail (text != NULL);
6719   priv = GTK_ENTRY_GET_PRIVATE (entry);
6720
6721   /* Actually setting the text will affect the cursor and selection;
6722    * if the contents don't actually change, this will look odd to the user.
6723    */
6724   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6725     return;
6726
6727   completion = gtk_entry_get_completion (entry);
6728   if (completion && completion->priv->changed_id > 0)
6729     g_signal_handler_block (entry, completion->priv->changed_id);
6730
6731   begin_change (entry);
6732   g_object_freeze_notify (G_OBJECT (entry));
6733   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6734   tmp_pos = 0;
6735   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6736   g_object_thaw_notify (G_OBJECT (entry));
6737   end_change (entry);
6738
6739   if (completion && completion->priv->changed_id > 0)
6740     g_signal_handler_unblock (entry, completion->priv->changed_id);
6741 }
6742
6743 /**
6744  * gtk_entry_set_visibility:
6745  * @entry: a #GtkEntry
6746  * @visible: %TRUE if the contents of the entry are displayed
6747  *           as plaintext
6748  *
6749  * Sets whether the contents of the entry are visible or not. 
6750  * When visibility is set to %FALSE, characters are displayed 
6751  * as the invisible char, and will also appear that way when 
6752  * the text in the entry widget is copied elsewhere.
6753  *
6754  * By default, GTK+ picks the best invisible character available
6755  * in the current font, but it can be changed with
6756  * gtk_entry_set_invisible_char().
6757  */
6758 void
6759 gtk_entry_set_visibility (GtkEntry *entry,
6760                           gboolean visible)
6761 {
6762   g_return_if_fail (GTK_IS_ENTRY (entry));
6763
6764   visible = visible != FALSE;
6765
6766   if (entry->visible != visible)
6767     {
6768       entry->visible = visible;
6769
6770       g_object_notify (G_OBJECT (entry), "visibility");
6771       gtk_entry_recompute (entry);
6772     }
6773 }
6774
6775 /**
6776  * gtk_entry_get_visibility:
6777  * @entry: a #GtkEntry
6778  *
6779  * Retrieves whether the text in @entry is visible. See
6780  * gtk_entry_set_visibility().
6781  *
6782  * Return value: %TRUE if the text is currently visible
6783  **/
6784 gboolean
6785 gtk_entry_get_visibility (GtkEntry *entry)
6786 {
6787   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6788
6789   return entry->visible;
6790 }
6791
6792 /**
6793  * gtk_entry_set_invisible_char:
6794  * @entry: a #GtkEntry
6795  * @ch: a Unicode character
6796  * 
6797  * Sets the character to use in place of the actual text when
6798  * gtk_entry_set_visibility() has been called to set text visibility
6799  * to %FALSE. i.e. this is the character used in "password mode" to
6800  * show the user how many characters have been typed. By default, GTK+
6801  * picks the best invisible char available in the current font. If you
6802  * set the invisible char to 0, then the user will get no feedback
6803  * at all; there will be no text on the screen as they type.
6804  **/
6805 void
6806 gtk_entry_set_invisible_char (GtkEntry *entry,
6807                               gunichar  ch)
6808 {
6809   GtkEntryPrivate *priv;
6810
6811   g_return_if_fail (GTK_IS_ENTRY (entry));
6812
6813   priv = GTK_ENTRY_GET_PRIVATE (entry);
6814
6815   if (!priv->invisible_char_set)
6816     {
6817       priv->invisible_char_set = TRUE;
6818       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6819     }
6820
6821   if (ch == entry->invisible_char)
6822     return;
6823
6824   entry->invisible_char = ch;
6825   g_object_notify (G_OBJECT (entry), "invisible-char");
6826   gtk_entry_recompute (entry);  
6827 }
6828
6829 /**
6830  * gtk_entry_get_invisible_char:
6831  * @entry: a #GtkEntry
6832  *
6833  * Retrieves the character displayed in place of the real characters
6834  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6835  *
6836  * Return value: the current invisible char, or 0, if the entry does not
6837  *               show invisible text at all. 
6838  **/
6839 gunichar
6840 gtk_entry_get_invisible_char (GtkEntry *entry)
6841 {
6842   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6843
6844   return entry->invisible_char;
6845 }
6846
6847 /**
6848  * gtk_entry_unset_invisible_char:
6849  * @entry: a #GtkEntry
6850  *
6851  * Unsets the invisible char previously set with
6852  * gtk_entry_set_invisible_char(). So that the
6853  * default invisible char is used again.
6854  *
6855  * Since: 2.16
6856  **/
6857 void
6858 gtk_entry_unset_invisible_char (GtkEntry *entry)
6859 {
6860   GtkEntryPrivate *priv;
6861   gunichar ch;
6862
6863   g_return_if_fail (GTK_IS_ENTRY (entry));
6864
6865   priv = GTK_ENTRY_GET_PRIVATE (entry);
6866
6867   if (!priv->invisible_char_set)
6868     return;
6869
6870   priv->invisible_char_set = FALSE;
6871   ch = find_invisible_char (GTK_WIDGET (entry));
6872
6873   if (entry->invisible_char != ch)
6874     {
6875       entry->invisible_char = ch;
6876       g_object_notify (G_OBJECT (entry), "invisible-char");
6877     }
6878
6879   g_object_notify (G_OBJECT (entry), "invisible-char-set");
6880   gtk_entry_recompute (entry);
6881 }
6882
6883 /**
6884  * gtk_entry_set_overwrite_mode:
6885  * @entry: a #GtkEntry
6886  * @overwrite: new value
6887  * 
6888  * Sets whether the text is overwritten when typing in the #GtkEntry.
6889  *
6890  * Since: 2.14
6891  **/
6892 void
6893 gtk_entry_set_overwrite_mode (GtkEntry *entry,
6894                               gboolean  overwrite)
6895 {
6896   g_return_if_fail (GTK_IS_ENTRY (entry));
6897   
6898   if (entry->overwrite_mode == overwrite) 
6899     return;
6900   
6901   gtk_entry_toggle_overwrite (entry);
6902
6903   g_object_notify (G_OBJECT (entry), "overwrite-mode");
6904 }
6905
6906 /**
6907  * gtk_entry_get_overwrite_mode:
6908  * @entry: a #GtkEntry
6909  * 
6910  * Gets the value set by gtk_entry_set_overwrite_mode().
6911  * 
6912  * Return value: whether the text is overwritten when typing.
6913  *
6914  * Since: 2.14
6915  **/
6916 gboolean
6917 gtk_entry_get_overwrite_mode (GtkEntry *entry)
6918 {
6919   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6920
6921   return entry->overwrite_mode;
6922 }
6923
6924 /**
6925  * gtk_entry_get_text:
6926  * @entry: a #GtkEntry
6927  *
6928  * Retrieves the contents of the entry widget.
6929  * See also gtk_editable_get_chars().
6930  *
6931  * This is equivalent to:
6932  *
6933  * <informalexample><programlisting>
6934  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
6935  * </programlisting></informalexample>
6936  *
6937  * Return value: a pointer to the contents of the widget as a
6938  *      string. This string points to internally allocated
6939  *      storage in the widget and must not be freed, modified or
6940  *      stored.
6941  **/
6942 G_CONST_RETURN gchar*
6943 gtk_entry_get_text (GtkEntry *entry)
6944 {
6945   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6946   return gtk_entry_buffer_get_text (get_buffer (entry));
6947 }
6948
6949 /**
6950  * gtk_entry_set_max_length:
6951  * @entry: a #GtkEntry
6952  * @max: the maximum length of the entry, or 0 for no maximum.
6953  *   (other than the maximum length of entries.) The value passed in will
6954  *   be clamped to the range 0-65536.
6955  * 
6956  * Sets the maximum allowed length of the contents of the widget. If
6957  * the current contents are longer than the given length, then they
6958  * will be truncated to fit.
6959  *
6960  * This is equivalent to:
6961  *
6962  * <informalexample><programlisting>
6963  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
6964  * </programlisting></informalexample>
6965  **/
6966 void
6967 gtk_entry_set_max_length (GtkEntry     *entry,
6968                           gint          max)
6969 {
6970   g_return_if_fail (GTK_IS_ENTRY (entry));
6971   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
6972 }
6973
6974 /**
6975  * gtk_entry_get_max_length:
6976  * @entry: a #GtkEntry
6977  *
6978  * Retrieves the maximum allowed length of the text in
6979  * @entry. See gtk_entry_set_max_length().
6980  *
6981  * This is equivalent to:
6982  *
6983  * <informalexample><programlisting>
6984  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
6985  * </programlisting></informalexample>
6986  *
6987  * Return value: the maximum allowed number of characters
6988  *               in #GtkEntry, or 0 if there is no maximum.
6989  **/
6990 gint
6991 gtk_entry_get_max_length (GtkEntry *entry)
6992 {
6993   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6994   return gtk_entry_buffer_get_max_length (get_buffer (entry));
6995 }
6996
6997 /**
6998  * gtk_entry_get_text_length:
6999  * @entry: a #GtkEntry
7000  *
7001  * Retrieves the current length of the text in
7002  * @entry. 
7003  *
7004  * This is equivalent to:
7005  *
7006  * <informalexample><programlisting>
7007  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7008  * </programlisting></informalexample>
7009  *
7010  * Return value: the current number of characters
7011  *               in #GtkEntry, or 0 if there are none.
7012  *
7013  * Since: 2.14
7014  **/
7015 guint16
7016 gtk_entry_get_text_length (GtkEntry *entry)
7017 {
7018   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7019   return gtk_entry_buffer_get_length (get_buffer (entry));
7020 }
7021
7022 /**
7023  * gtk_entry_set_activates_default:
7024  * @entry: a #GtkEntry
7025  * @setting: %TRUE to activate window's default widget on Enter keypress
7026  *
7027  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7028  * widget for the window containing the entry. This usually means that
7029  * the dialog box containing the entry will be closed, since the default
7030  * widget is usually one of the dialog buttons.
7031  *
7032  * (For experts: if @setting is %TRUE, the entry calls
7033  * gtk_window_activate_default() on the window containing the entry, in
7034  * the default handler for the #GtkWidget::activate signal.)
7035  **/
7036 void
7037 gtk_entry_set_activates_default (GtkEntry *entry,
7038                                  gboolean  setting)
7039 {
7040   g_return_if_fail (GTK_IS_ENTRY (entry));
7041   setting = setting != FALSE;
7042
7043   if (setting != entry->activates_default)
7044     {
7045       entry->activates_default = setting;
7046       g_object_notify (G_OBJECT (entry), "activates-default");
7047     }
7048 }
7049
7050 /**
7051  * gtk_entry_get_activates_default:
7052  * @entry: a #GtkEntry
7053  * 
7054  * Retrieves the value set by gtk_entry_set_activates_default().
7055  * 
7056  * Return value: %TRUE if the entry will activate the default widget
7057  **/
7058 gboolean
7059 gtk_entry_get_activates_default (GtkEntry *entry)
7060 {
7061   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7062
7063   return entry->activates_default;
7064 }
7065
7066 /**
7067  * gtk_entry_set_width_chars:
7068  * @entry: a #GtkEntry
7069  * @n_chars: width in chars
7070  *
7071  * Changes the size request of the entry to be about the right size
7072  * for @n_chars characters. Note that it changes the size
7073  * <emphasis>request</emphasis>, the size can still be affected by
7074  * how you pack the widget into containers. If @n_chars is -1, the
7075  * size reverts to the default entry size.
7076  **/
7077 void
7078 gtk_entry_set_width_chars (GtkEntry *entry,
7079                            gint      n_chars)
7080 {
7081   g_return_if_fail (GTK_IS_ENTRY (entry));
7082
7083   if (entry->width_chars != n_chars)
7084     {
7085       entry->width_chars = n_chars;
7086       g_object_notify (G_OBJECT (entry), "width-chars");
7087       gtk_widget_queue_resize (GTK_WIDGET (entry));
7088     }
7089 }
7090
7091 /**
7092  * gtk_entry_get_width_chars:
7093  * @entry: a #GtkEntry
7094  * 
7095  * Gets the value set by gtk_entry_set_width_chars().
7096  * 
7097  * Return value: number of chars to request space for, or negative if unset
7098  **/
7099 gint
7100 gtk_entry_get_width_chars (GtkEntry *entry)
7101 {
7102   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7103
7104   return entry->width_chars;
7105 }
7106
7107 /**
7108  * gtk_entry_set_has_frame:
7109  * @entry: a #GtkEntry
7110  * @setting: new value
7111  * 
7112  * Sets whether the entry has a beveled frame around it.
7113  **/
7114 void
7115 gtk_entry_set_has_frame (GtkEntry *entry,
7116                          gboolean  setting)
7117 {
7118   g_return_if_fail (GTK_IS_ENTRY (entry));
7119
7120   setting = (setting != FALSE);
7121
7122   if (entry->has_frame == setting)
7123     return;
7124
7125   gtk_widget_queue_resize (GTK_WIDGET (entry));
7126   entry->has_frame = setting;
7127   g_object_notify (G_OBJECT (entry), "has-frame");
7128 }
7129
7130 /**
7131  * gtk_entry_get_has_frame:
7132  * @entry: a #GtkEntry
7133  * 
7134  * Gets the value set by gtk_entry_set_has_frame().
7135  * 
7136  * Return value: whether the entry has a beveled frame
7137  **/
7138 gboolean
7139 gtk_entry_get_has_frame (GtkEntry *entry)
7140 {
7141   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7142
7143   return entry->has_frame;
7144 }
7145
7146 /**
7147  * gtk_entry_set_inner_border:
7148  * @entry: a #GtkEntry
7149  * @border: (allow-none): a #GtkBorder, or %NULL
7150  *
7151  * Sets %entry's inner-border property to %border, or clears it if %NULL
7152  * is passed. The inner-border is the area around the entry's text, but
7153  * inside its frame.
7154  *
7155  * If set, this property overrides the inner-border style property.
7156  * Overriding the style-provided border is useful when you want to do
7157  * in-place editing of some text in a canvas or list widget, where
7158  * pixel-exact positioning of the entry is important.
7159  *
7160  * Since: 2.10
7161  **/
7162 void
7163 gtk_entry_set_inner_border (GtkEntry        *entry,
7164                             const GtkBorder *border)
7165 {
7166   g_return_if_fail (GTK_IS_ENTRY (entry));
7167
7168   gtk_widget_queue_resize (GTK_WIDGET (entry));
7169
7170   if (border)
7171     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7172                              gtk_border_copy (border),
7173                              (GDestroyNotify) gtk_border_free);
7174   else
7175     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7176
7177   g_object_notify (G_OBJECT (entry), "inner-border");
7178 }
7179
7180 /**
7181  * gtk_entry_get_inner_border:
7182  * @entry: a #GtkEntry
7183  *
7184  * This function returns the entry's #GtkEntry:inner-border property. See
7185  * gtk_entry_set_inner_border() for more information.
7186  *
7187  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7188  *
7189  * Since: 2.10
7190  **/
7191 G_CONST_RETURN GtkBorder *
7192 gtk_entry_get_inner_border (GtkEntry *entry)
7193 {
7194   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7195
7196   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7197 }
7198
7199 /**
7200  * gtk_entry_get_layout:
7201  * @entry: a #GtkEntry
7202  * 
7203  * Gets the #PangoLayout used to display the entry.
7204  * The layout is useful to e.g. convert text positions to
7205  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7206  * The returned layout is owned by the entry and must not be 
7207  * modified or freed by the caller.
7208  *
7209  * Keep in mind that the layout text may contain a preedit string, so
7210  * gtk_entry_layout_index_to_text_index() and
7211  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7212  * indices in the layout to byte indices in the entry contents.
7213  *
7214  * Return value: (transfer none): the #PangoLayout for this entry
7215  **/
7216 PangoLayout*
7217 gtk_entry_get_layout (GtkEntry *entry)
7218 {
7219   PangoLayout *layout;
7220   
7221   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7222
7223   layout = gtk_entry_ensure_layout (entry, TRUE);
7224
7225   return layout;
7226 }
7227
7228
7229 /**
7230  * gtk_entry_layout_index_to_text_index:
7231  * @entry: a #GtkEntry
7232  * @layout_index: byte index into the entry layout text
7233  * 
7234  * Converts from a position in the entry contents (returned
7235  * by gtk_entry_get_text()) to a position in the
7236  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7237  * with text retrieved via pango_layout_get_text()).
7238  * 
7239  * Return value: byte index into the entry contents
7240  **/
7241 gint
7242 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7243                                       gint      layout_index)
7244 {
7245   PangoLayout *layout;
7246   const gchar *text;
7247   gint cursor_index;
7248   
7249   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7250
7251   layout = gtk_entry_ensure_layout (entry, TRUE);
7252   text = pango_layout_get_text (layout);
7253   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7254   
7255   if (layout_index >= cursor_index && entry->preedit_length)
7256     {
7257       if (layout_index >= cursor_index + entry->preedit_length)
7258         layout_index -= entry->preedit_length;
7259       else
7260         layout_index = cursor_index;
7261     }
7262
7263   return layout_index;
7264 }
7265
7266 /**
7267  * gtk_entry_text_index_to_layout_index:
7268  * @entry: a #GtkEntry
7269  * @text_index: byte index into the entry contents
7270  * 
7271  * Converts from a position in the entry's #PangoLayout (returned by
7272  * gtk_entry_get_layout()) to a position in the entry contents
7273  * (returned by gtk_entry_get_text()).
7274  * 
7275  * Return value: byte index into the entry layout text
7276  **/
7277 gint
7278 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7279                                       gint      text_index)
7280 {
7281   PangoLayout *layout;
7282   const gchar *text;
7283   gint cursor_index;
7284   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7285
7286   layout = gtk_entry_ensure_layout (entry, TRUE);
7287   text = pango_layout_get_text (layout);
7288   cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
7289   
7290   if (text_index > cursor_index)
7291     text_index += entry->preedit_length;
7292
7293   return text_index;
7294 }
7295
7296 /**
7297  * gtk_entry_get_layout_offsets:
7298  * @entry: a #GtkEntry
7299  * @x: (allow-none): location to store X offset of layout, or %NULL
7300  * @y: (allow-none): location to store Y offset of layout, or %NULL
7301  *
7302  *
7303  * Obtains the position of the #PangoLayout used to render text
7304  * in the entry, in widget coordinates. Useful if you want to line
7305  * up the text in an entry with some other text, e.g. when using the
7306  * entry to implement editable cells in a sheet widget.
7307  *
7308  * Also useful to convert mouse events into coordinates inside the
7309  * #PangoLayout, e.g. to take some action if some part of the entry text
7310  * is clicked.
7311  * 
7312  * Note that as the user scrolls around in the entry the offsets will
7313  * change; you'll need to connect to the "notify::scroll-offset"
7314  * signal to track this. Remember when using the #PangoLayout
7315  * functions you need to convert to and from pixels using
7316  * PANGO_PIXELS() or #PANGO_SCALE.
7317  *
7318  * Keep in mind that the layout text may contain a preedit string, so
7319  * gtk_entry_layout_index_to_text_index() and
7320  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7321  * indices in the layout to byte indices in the entry contents.
7322  **/
7323 void
7324 gtk_entry_get_layout_offsets (GtkEntry *entry,
7325                               gint     *x,
7326                               gint     *y)
7327 {
7328   gint text_area_x, text_area_y;
7329   
7330   g_return_if_fail (GTK_IS_ENTRY (entry));
7331
7332   /* this gets coords relative to text area */
7333   get_layout_position (entry, x, y);
7334
7335   /* convert to widget coords */
7336   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7337   
7338   if (x)
7339     *x += text_area_x;
7340
7341   if (y)
7342     *y += text_area_y;
7343 }
7344
7345
7346 /**
7347  * gtk_entry_set_alignment:
7348  * @entry: a #GtkEntry
7349  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7350  *          Reversed for RTL layouts
7351  * 
7352  * Sets the alignment for the contents of the entry. This controls
7353  * the horizontal positioning of the contents when the displayed
7354  * text is shorter than the width of the entry.
7355  *
7356  * Since: 2.4
7357  **/
7358 void
7359 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7360 {
7361   GtkEntryPrivate *priv;
7362   
7363   g_return_if_fail (GTK_IS_ENTRY (entry));
7364
7365   priv = GTK_ENTRY_GET_PRIVATE (entry);
7366
7367   if (xalign < 0.0)
7368     xalign = 0.0;
7369   else if (xalign > 1.0)
7370     xalign = 1.0;
7371
7372   if (xalign != priv->xalign)
7373     {
7374       priv->xalign = xalign;
7375
7376       gtk_entry_recompute (entry);
7377
7378       g_object_notify (G_OBJECT (entry), "xalign");
7379     }
7380 }
7381
7382 /**
7383  * gtk_entry_get_alignment:
7384  * @entry: a #GtkEntry
7385  * 
7386  * Gets the value set by gtk_entry_set_alignment().
7387  * 
7388  * Return value: the alignment
7389  *
7390  * Since: 2.4
7391  **/
7392 gfloat
7393 gtk_entry_get_alignment (GtkEntry *entry)
7394 {
7395   GtkEntryPrivate *priv;
7396   
7397   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7398
7399   priv = GTK_ENTRY_GET_PRIVATE (entry);
7400
7401   return priv->xalign;
7402 }
7403
7404 /**
7405  * gtk_entry_set_icon_from_pixbuf:
7406  * @entry: a #GtkEntry
7407  * @icon_pos: Icon position
7408  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7409  *
7410  * Sets the icon shown in the specified position using a pixbuf.
7411  *
7412  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7413  *
7414  * Since: 2.16
7415  */
7416 void
7417 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7418                                 GtkEntryIconPosition  icon_pos,
7419                                 GdkPixbuf            *pixbuf)
7420 {
7421   GtkEntryPrivate *priv;
7422   EntryIconInfo *icon_info;
7423
7424   g_return_if_fail (GTK_IS_ENTRY (entry));
7425   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7426
7427   priv = GTK_ENTRY_GET_PRIVATE (entry);
7428
7429   if ((icon_info = priv->icons[icon_pos]) == NULL)
7430     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7431
7432   g_object_freeze_notify (G_OBJECT (entry));
7433
7434   if (pixbuf)
7435     g_object_ref (pixbuf);
7436
7437   gtk_entry_clear (entry, icon_pos);
7438
7439   if (pixbuf)
7440     {
7441       icon_info->storage_type = GTK_IMAGE_PIXBUF;
7442       icon_info->pixbuf = pixbuf;
7443
7444       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7445         {
7446           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7447           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7448         }
7449       else
7450         {
7451           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7452           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7453         }
7454
7455       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7456           gdk_window_show_unraised (icon_info->window);
7457     }
7458
7459   gtk_entry_ensure_pixbuf (entry, icon_pos);
7460   
7461   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7462     gtk_widget_queue_resize (GTK_WIDGET (entry));
7463
7464   g_object_thaw_notify (G_OBJECT (entry));
7465 }
7466
7467 /**
7468  * gtk_entry_set_icon_from_stock:
7469  * @entry: A #GtkEntry
7470  * @icon_pos: Icon position
7471  * @stock_id: (allow-none): The name of the stock item, or %NULL
7472  *
7473  * Sets the icon shown in the entry at the specified position from
7474  * a stock image.
7475  *
7476  * If @stock_id is %NULL, no icon will be shown in the specified position.
7477  *
7478  * Since: 2.16
7479  */
7480 void
7481 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7482                                GtkEntryIconPosition  icon_pos,
7483                                const gchar          *stock_id)
7484 {
7485   GtkEntryPrivate *priv;
7486   EntryIconInfo *icon_info;
7487   gchar *new_id;
7488
7489   g_return_if_fail (GTK_IS_ENTRY (entry));
7490   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7491
7492   priv = GTK_ENTRY_GET_PRIVATE (entry);
7493
7494   if ((icon_info = priv->icons[icon_pos]) == NULL)
7495     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7496
7497   g_object_freeze_notify (G_OBJECT (entry));
7498
7499   gtk_widget_ensure_style (GTK_WIDGET (entry));
7500
7501   /* need to dup before clearing */
7502   new_id = g_strdup (stock_id);
7503
7504   gtk_entry_clear (entry, icon_pos);
7505
7506   if (new_id != NULL)
7507     {
7508       icon_info->storage_type = GTK_IMAGE_STOCK;
7509       icon_info->stock_id = new_id;
7510
7511       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7512         {
7513           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7514           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7515         }
7516       else
7517         {
7518           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7519           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7520         }
7521
7522       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7523           gdk_window_show_unraised (icon_info->window);
7524     }
7525
7526   gtk_entry_ensure_pixbuf (entry, icon_pos);
7527
7528   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7529     gtk_widget_queue_resize (GTK_WIDGET (entry));
7530
7531   g_object_thaw_notify (G_OBJECT (entry));
7532 }
7533
7534 /**
7535  * gtk_entry_set_icon_from_icon_name:
7536  * @entry: A #GtkEntry
7537  * @icon_pos: The position at which to set the icon
7538  * @icon_name: (allow-none): An icon name, or %NULL
7539  *
7540  * Sets the icon shown in the entry at the specified position
7541  * from the current icon theme.
7542  *
7543  * If the icon name isn't known, a "broken image" icon will be displayed
7544  * instead.
7545  *
7546  * If @icon_name is %NULL, no icon will be shown in the specified position.
7547  *
7548  * Since: 2.16
7549  */
7550 void
7551 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7552                                    GtkEntryIconPosition  icon_pos,
7553                                    const gchar          *icon_name)
7554 {
7555   GtkEntryPrivate *priv;
7556   EntryIconInfo *icon_info;
7557   gchar *new_name;
7558
7559   g_return_if_fail (GTK_IS_ENTRY (entry));
7560   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7561
7562   priv = GTK_ENTRY_GET_PRIVATE (entry);
7563
7564   if ((icon_info = priv->icons[icon_pos]) == NULL)
7565     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7566
7567   g_object_freeze_notify (G_OBJECT (entry));
7568
7569   gtk_widget_ensure_style (GTK_WIDGET (entry));
7570
7571   /* need to dup before clearing */
7572   new_name = g_strdup (icon_name);
7573
7574   gtk_entry_clear (entry, icon_pos);
7575
7576   if (new_name != NULL)
7577     {
7578       icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7579       icon_info->icon_name = new_name;
7580
7581       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7582         {
7583           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7584           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7585         }
7586       else
7587         {
7588           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7589           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7590         }
7591
7592       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7593           gdk_window_show_unraised (icon_info->window);
7594     }
7595
7596   gtk_entry_ensure_pixbuf (entry, icon_pos);
7597
7598   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7599     gtk_widget_queue_resize (GTK_WIDGET (entry));
7600
7601   g_object_thaw_notify (G_OBJECT (entry));
7602 }
7603
7604 /**
7605  * gtk_entry_set_icon_from_gicon:
7606  * @entry: A #GtkEntry
7607  * @icon_pos: The position at which to set the icon
7608  * @icon: (allow-none): The icon to set, or %NULL
7609  *
7610  * Sets the icon shown in the entry at the specified position
7611  * from the current icon theme.
7612  * If the icon isn't known, a "broken image" icon will be displayed
7613  * instead.
7614  *
7615  * If @icon is %NULL, no icon will be shown in the specified position.
7616  *
7617  * Since: 2.16
7618  */
7619 void
7620 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7621                                GtkEntryIconPosition  icon_pos,
7622                                GIcon                *icon)
7623 {
7624   GtkEntryPrivate *priv;
7625   EntryIconInfo *icon_info;
7626
7627   g_return_if_fail (GTK_IS_ENTRY (entry));
7628   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7629
7630   priv = GTK_ENTRY_GET_PRIVATE (entry);
7631
7632   if ((icon_info = priv->icons[icon_pos]) == NULL)
7633     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7634
7635   g_object_freeze_notify (G_OBJECT (entry));
7636
7637   /* need to ref before clearing */
7638   if (icon)
7639     g_object_ref (icon);
7640
7641   gtk_entry_clear (entry, icon_pos);
7642
7643   if (icon)
7644     {
7645       icon_info->storage_type = GTK_IMAGE_GICON;
7646       icon_info->gicon = icon;
7647
7648       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7649         {
7650           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7651           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7652         }
7653       else
7654         {
7655           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7656           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7657         }
7658
7659       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7660           gdk_window_show_unraised (icon_info->window);
7661     }
7662
7663   gtk_entry_ensure_pixbuf (entry, icon_pos);
7664
7665   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7666     gtk_widget_queue_resize (GTK_WIDGET (entry));
7667
7668   g_object_thaw_notify (G_OBJECT (entry));
7669 }
7670
7671 /**
7672  * gtk_entry_set_icon_activatable:
7673  * @entry: A #GtkEntry
7674  * @icon_pos: Icon position
7675  * @activatable: %TRUE if the icon should be activatable
7676  *
7677  * Sets whether the icon is activatable.
7678  *
7679  * Since: 2.16
7680  */
7681 void
7682 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7683                                 GtkEntryIconPosition  icon_pos,
7684                                 gboolean              activatable)
7685 {
7686   GtkEntryPrivate *priv;
7687   EntryIconInfo *icon_info;
7688
7689   g_return_if_fail (GTK_IS_ENTRY (entry));
7690   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7691
7692   priv = GTK_ENTRY_GET_PRIVATE (entry);
7693
7694   if ((icon_info = priv->icons[icon_pos]) == NULL)
7695     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7696
7697   activatable = activatable != FALSE;
7698
7699   if (icon_info->nonactivatable != !activatable)
7700     {
7701       icon_info->nonactivatable = !activatable;
7702
7703       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7704         update_cursors (GTK_WIDGET (entry));
7705
7706       g_object_notify (G_OBJECT (entry),
7707                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7708     }
7709 }
7710
7711 /**
7712  * gtk_entry_get_icon_activatable:
7713  * @entry: a #GtkEntry
7714  * @icon_pos: Icon position
7715  *
7716  * Returns whether the icon is activatable.
7717  *
7718  * Returns: %TRUE if the icon is activatable.
7719  *
7720  * Since: 2.16
7721  */
7722 gboolean
7723 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7724                                 GtkEntryIconPosition  icon_pos)
7725 {
7726   GtkEntryPrivate *priv;
7727   EntryIconInfo *icon_info;
7728
7729   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7730   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7731
7732   priv = GTK_ENTRY_GET_PRIVATE (entry);
7733   icon_info = priv->icons[icon_pos];
7734
7735   return (icon_info != NULL && !icon_info->nonactivatable);
7736 }
7737
7738 /**
7739  * gtk_entry_get_icon_pixbuf:
7740  * @entry: A #GtkEntry
7741  * @icon_pos: Icon position
7742  *
7743  * Retrieves the image used for the icon.
7744  *
7745  * Unlike the other methods of setting and getting icon data, this
7746  * method will work regardless of whether the icon was set using a
7747  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7748  *
7749  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7750  *     set for this position.
7751  *
7752  * Since: 2.16
7753  */
7754 GdkPixbuf *
7755 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7756                            GtkEntryIconPosition  icon_pos)
7757 {
7758   GtkEntryPrivate *priv;
7759   EntryIconInfo *icon_info;
7760
7761   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7762   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7763
7764   priv = GTK_ENTRY_GET_PRIVATE (entry);
7765   icon_info = priv->icons[icon_pos];
7766
7767   if (!icon_info)
7768     return NULL;
7769
7770   gtk_entry_ensure_pixbuf (entry, icon_pos);
7771
7772   return icon_info->pixbuf;
7773 }
7774
7775 /**
7776  * gtk_entry_get_icon_gicon:
7777  * @entry: A #GtkEntry
7778  * @icon_pos: Icon position
7779  *
7780  * Retrieves the #GIcon used for the icon, or %NULL if there is
7781  * no icon or if the icon was set by some other method (e.g., by
7782  * stock, pixbuf, or icon name).
7783  *
7784  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
7785  *     or if the icon is not a #GIcon
7786  *
7787  * Since: 2.16
7788  */
7789 GIcon *
7790 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7791                           GtkEntryIconPosition  icon_pos)
7792 {
7793   GtkEntryPrivate *priv;
7794   EntryIconInfo *icon_info;
7795
7796   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7797   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7798
7799   priv = GTK_ENTRY_GET_PRIVATE (entry);
7800   icon_info = priv->icons[icon_pos];
7801
7802   if (!icon_info)
7803     return NULL;
7804
7805   return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
7806 }
7807
7808 /**
7809  * gtk_entry_get_icon_stock:
7810  * @entry: A #GtkEntry
7811  * @icon_pos: Icon position
7812  *
7813  * Retrieves the stock id used for the icon, or %NULL if there is
7814  * no icon or if the icon was set by some other method (e.g., by
7815  * pixbuf, icon name or gicon).
7816  *
7817  * Returns: A stock id, or %NULL if no icon is set or if the icon
7818  *          wasn't set from a stock id
7819  *
7820  * Since: 2.16
7821  */
7822 const gchar *
7823 gtk_entry_get_icon_stock (GtkEntry             *entry,
7824                           GtkEntryIconPosition  icon_pos)
7825 {
7826   GtkEntryPrivate *priv;
7827   EntryIconInfo *icon_info;
7828
7829   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7830   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7831
7832   priv = GTK_ENTRY_GET_PRIVATE (entry);
7833   icon_info = priv->icons[icon_pos];
7834
7835   if (!icon_info)
7836     return NULL;
7837
7838   return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
7839 }
7840
7841 /**
7842  * gtk_entry_get_icon_name:
7843  * @entry: A #GtkEntry
7844  * @icon_pos: Icon position
7845  *
7846  * Retrieves the icon name used for the icon, or %NULL if there is
7847  * no icon or if the icon was set by some other method (e.g., by
7848  * pixbuf, stock or gicon).
7849  *
7850  * Returns: An icon name, or %NULL if no icon is set or if the icon
7851  *          wasn't set from an icon name
7852  *
7853  * Since: 2.16
7854  */
7855 const gchar *
7856 gtk_entry_get_icon_name (GtkEntry             *entry,
7857                          GtkEntryIconPosition  icon_pos)
7858 {
7859   GtkEntryPrivate *priv;
7860   EntryIconInfo *icon_info;
7861
7862   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7863   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7864
7865   priv = GTK_ENTRY_GET_PRIVATE (entry);
7866   icon_info = priv->icons[icon_pos];
7867
7868   if (!icon_info)
7869     return NULL;
7870
7871   return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
7872 }
7873
7874 /**
7875  * gtk_entry_set_icon_sensitive:
7876  * @entry: A #GtkEntry
7877  * @icon_pos: Icon position
7878  * @sensitive: Specifies whether the icon should appear
7879  *             sensitive or insensitive
7880  *
7881  * Sets the sensitivity for the specified icon.
7882  *
7883  * Since: 2.16
7884  */
7885 void
7886 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
7887                               GtkEntryIconPosition  icon_pos,
7888                               gboolean              sensitive)
7889 {
7890   GtkEntryPrivate *priv;
7891   EntryIconInfo *icon_info;
7892
7893   g_return_if_fail (GTK_IS_ENTRY (entry));
7894   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7895
7896   priv = GTK_ENTRY_GET_PRIVATE (entry);
7897
7898   if ((icon_info = priv->icons[icon_pos]) == NULL)
7899     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7900
7901   if (icon_info->insensitive != !sensitive)
7902     {
7903       icon_info->insensitive = !sensitive;
7904
7905       icon_info->pressed = FALSE;
7906       icon_info->prelight = FALSE;
7907
7908       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7909         update_cursors (GTK_WIDGET (entry));
7910
7911       gtk_widget_queue_draw (GTK_WIDGET (entry));
7912
7913       g_object_notify (G_OBJECT (entry),
7914                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
7915     }
7916 }
7917
7918 /**
7919  * gtk_entry_get_icon_sensitive:
7920  * @entry: a #GtkEntry
7921  * @icon_pos: Icon position
7922  *
7923  * Returns whether the icon appears sensitive or insensitive.
7924  *
7925  * Returns: %TRUE if the icon is sensitive.
7926  *
7927  * Since: 2.16
7928  */
7929 gboolean
7930 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
7931                               GtkEntryIconPosition  icon_pos)
7932 {
7933   GtkEntryPrivate *priv;
7934   EntryIconInfo *icon_info;
7935
7936   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
7937   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
7938
7939   priv = GTK_ENTRY_GET_PRIVATE (entry);
7940   icon_info = priv->icons[icon_pos];
7941
7942   return (!icon_info || !icon_info->insensitive);
7943
7944 }
7945
7946 /**
7947  * gtk_entry_get_icon_storage_type:
7948  * @entry: a #GtkEntry
7949  * @icon_pos: Icon position
7950  *
7951  * Gets the type of representation being used by the icon
7952  * to store image data. If the icon has no image data,
7953  * the return value will be %GTK_IMAGE_EMPTY.
7954  *
7955  * Return value: image representation being used
7956  *
7957  * Since: 2.16
7958  **/
7959 GtkImageType
7960 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
7961                                  GtkEntryIconPosition  icon_pos)
7962 {
7963   GtkEntryPrivate *priv;
7964   EntryIconInfo *icon_info;
7965
7966   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
7967   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
7968
7969   priv = GTK_ENTRY_GET_PRIVATE (entry);
7970   icon_info = priv->icons[icon_pos];
7971
7972   if (!icon_info)
7973     return GTK_IMAGE_EMPTY;
7974
7975   return icon_info->storage_type;
7976 }
7977
7978 /**
7979  * gtk_entry_get_icon_at_pos:
7980  * @entry: a #GtkEntry
7981  * @x: the x coordinate of the position to find
7982  * @y: the y coordinate of the position to find
7983  *
7984  * Finds the icon at the given position and return its index.
7985  * If @x, @y doesn't lie inside an icon, -1 is returned.
7986  * This function is intended for use in a #GtkWidget::query-tooltip
7987  * signal handler.
7988  *
7989  * Returns: the index of the icon at the given position, or -1
7990  *
7991  * Since: 2.16
7992  */
7993 gint
7994 gtk_entry_get_icon_at_pos (GtkEntry *entry,
7995                            gint      x,
7996                            gint      y)
7997 {
7998   GtkAllocation primary;
7999   GtkAllocation secondary;
8000   gint frame_x, frame_y;
8001
8002   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8003
8004   get_frame_size (entry, &frame_x, &frame_y, NULL, NULL);
8005   x -= frame_x;
8006   y -= frame_y;
8007
8008   get_icon_allocations (entry, &primary, &secondary);
8009
8010   if (primary.x <= x && x < primary.x + primary.width &&
8011       primary.y <= y && y < primary.y + primary.height)
8012     return GTK_ENTRY_ICON_PRIMARY;
8013
8014   if (secondary.x <= x && x < secondary.x + secondary.width &&
8015       secondary.y <= y && y < secondary.y + secondary.height)
8016     return GTK_ENTRY_ICON_SECONDARY;
8017
8018   return -1;
8019 }
8020
8021 /**
8022  * gtk_entry_set_icon_drag_source:
8023  * @entry: a #GtkIconEntry
8024  * @icon_pos: icon position
8025  * @target_list: the targets (data formats) in which the data can be provided
8026  * @actions: a bitmask of the allowed drag actions
8027  *
8028  * Sets up the icon at the given position so that GTK+ will start a drag
8029  * operation when the user clicks and drags the icon.
8030  *
8031  * To handle the drag operation, you need to connect to the usual
8032  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8033  * signal, and use gtk_entry_get_current_icon_drag_source() in
8034  * your signal handler to find out if the drag was started from
8035  * an icon.
8036  *
8037  * By default, GTK+ uses the icon as the drag icon. You can use the 
8038  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8039  * have to use g_signal_connect_after() to ensure that your signal handler
8040  * gets executed after the default handler.
8041  *
8042  * Since: 2.16
8043  */
8044 void
8045 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8046                                 GtkEntryIconPosition  icon_pos,
8047                                 GtkTargetList        *target_list,
8048                                 GdkDragAction         actions)
8049 {
8050   GtkEntryPrivate *priv;
8051   EntryIconInfo *icon_info;
8052
8053   g_return_if_fail (GTK_IS_ENTRY (entry));
8054   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8055
8056   priv = GTK_ENTRY_GET_PRIVATE (entry);
8057
8058   if ((icon_info = priv->icons[icon_pos]) == NULL)
8059     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8060
8061   if (icon_info->target_list)
8062     gtk_target_list_unref (icon_info->target_list);
8063   icon_info->target_list = target_list;
8064   if (icon_info->target_list)
8065     gtk_target_list_ref (icon_info->target_list);
8066
8067   icon_info->actions = actions;
8068 }
8069
8070 /**
8071  * gtk_entry_get_current_icon_drag_source:
8072  * @entry: a #GtkIconEntry
8073  *
8074  * Returns the index of the icon which is the source of the current
8075  * DND operation, or -1.
8076  *
8077  * This function is meant to be used in a #GtkWidget::drag-data-get
8078  * callback.
8079  *
8080  * Returns: index of the icon which is the source of the current
8081  *          DND operation, or -1.
8082  *
8083  * Since: 2.16
8084  */
8085 gint
8086 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8087 {
8088   GtkEntryPrivate *priv;
8089   EntryIconInfo *icon_info = NULL;
8090   gint i;
8091
8092   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8093
8094   priv = GTK_ENTRY_GET_PRIVATE (entry);
8095
8096   for (i = 0; i < MAX_ICONS; i++)
8097     {
8098       if ((icon_info = priv->icons[i]))
8099         {
8100           if (icon_info->in_drag)
8101             return i;
8102         }
8103     }
8104
8105   return -1;
8106 }
8107
8108 /**
8109  * gtk_entry_get_icon_area:
8110  * @entry: A #GtkEntry
8111  * @icon_pos: Icon position
8112  * @icon_area: Return location for the icon's area
8113  *
8114  * Returns the area where entry's icon at @icon_pos is drawn.
8115  * This function is useful when drawing something to the
8116  * entry in a draw callback.
8117  *
8118  * See also gtk_entry_get_text_area()
8119  *
8120  * Since: 3.0
8121  */
8122 void
8123 gtk_entry_get_icon_area (GtkEntry             *entry,
8124                          GtkEntryIconPosition  icon_pos,
8125                          GdkRectangle         *icon_area)
8126 {
8127   GtkEntryPrivate *priv;
8128   EntryIconInfo *icon_info;
8129
8130   g_return_if_fail (GTK_IS_ENTRY (entry));
8131   g_return_if_fail (icon_area != NULL);
8132
8133   priv = GTK_ENTRY_GET_PRIVATE (entry);
8134
8135   icon_info = priv->icons[icon_pos];
8136
8137   if (icon_info)
8138     {
8139       GtkAllocation primary;
8140       GtkAllocation secondary;
8141
8142       get_icon_allocations (entry, &primary, &secondary);
8143
8144       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8145         *icon_area = primary;
8146       else
8147         *icon_area = secondary;
8148     }
8149   else
8150     {
8151       icon_area->x = 0;
8152       icon_area->y = 0;
8153       icon_area->width = 0;
8154       icon_area->height = 0;
8155     }
8156 }
8157
8158 static void
8159 ensure_has_tooltip (GtkEntry *entry)
8160 {
8161   GtkEntryPrivate *priv;
8162   EntryIconInfo *icon_info;
8163   int i;
8164   gboolean has_tooltip = FALSE;
8165
8166   priv = GTK_ENTRY_GET_PRIVATE (entry);
8167
8168   for (i = 0; i < MAX_ICONS; i++)
8169     {
8170       if ((icon_info = priv->icons[i]) != NULL)
8171         {
8172           if (icon_info->tooltip != NULL)
8173             {
8174               has_tooltip = TRUE;
8175               break;
8176             }
8177         }
8178     }
8179
8180   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8181 }
8182
8183 /**
8184  * gtk_entry_get_icon_tooltip_text:
8185  * @entry: a #GtkEntry
8186  * @icon_pos: the icon position
8187  *
8188  * Gets the contents of the tooltip on the icon at the specified 
8189  * position in @entry.
8190  * 
8191  * Returns: the tooltip text, or %NULL. Free the returned string
8192  *     with g_free() when done.
8193  * 
8194  * Since: 2.16
8195  */
8196 gchar *
8197 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8198                                  GtkEntryIconPosition  icon_pos)
8199 {
8200   GtkEntryPrivate *priv;
8201   EntryIconInfo *icon_info;
8202   gchar *text = NULL;
8203
8204   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8205   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8206
8207   priv = GTK_ENTRY_GET_PRIVATE (entry);
8208   icon_info = priv->icons[icon_pos];
8209
8210   if (!icon_info)
8211     return NULL;
8212  
8213   if (icon_info->tooltip && 
8214       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8215     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8216
8217   return text;
8218 }
8219
8220 /**
8221  * gtk_entry_set_icon_tooltip_text:
8222  * @entry: a #GtkEntry
8223  * @icon_pos: the icon position
8224  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8225  *
8226  * Sets @tooltip as the contents of the tooltip for the icon
8227  * at the specified position.
8228  *
8229  * Use %NULL for @tooltip to remove an existing tooltip.
8230  *
8231  * See also gtk_widget_set_tooltip_text() and 
8232  * gtk_entry_set_icon_tooltip_markup().
8233  *
8234  * Since: 2.16
8235  */
8236 void
8237 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8238                                  GtkEntryIconPosition  icon_pos,
8239                                  const gchar          *tooltip)
8240 {
8241   GtkEntryPrivate *priv;
8242   EntryIconInfo *icon_info;
8243
8244   g_return_if_fail (GTK_IS_ENTRY (entry));
8245   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8246
8247   priv = GTK_ENTRY_GET_PRIVATE (entry);
8248
8249   if (!(icon_info = priv->icons[icon_pos]))
8250     icon_info = priv->icons[icon_pos];
8251
8252   if (icon_info->tooltip)
8253     g_free (icon_info->tooltip);
8254
8255   /* Treat an empty string as a NULL string,
8256    * because an empty string would be useless for a tooltip:
8257    */
8258   if (tooltip && tooltip[0] == '\0')
8259     tooltip = NULL;
8260
8261   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8262
8263   ensure_has_tooltip (entry);
8264 }
8265
8266 /**
8267  * gtk_entry_get_icon_tooltip_markup:
8268  * @entry: a #GtkEntry
8269  * @icon_pos: the icon position
8270  *
8271  * Gets the contents of the tooltip on the icon at the specified 
8272  * position in @entry.
8273  * 
8274  * Returns: the tooltip text, or %NULL. Free the returned string
8275  *     with g_free() when done.
8276  * 
8277  * Since: 2.16
8278  */
8279 gchar *
8280 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8281                                    GtkEntryIconPosition  icon_pos)
8282 {
8283   GtkEntryPrivate *priv;
8284   EntryIconInfo *icon_info;
8285
8286   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8287   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8288
8289   priv = GTK_ENTRY_GET_PRIVATE (entry);
8290   icon_info = priv->icons[icon_pos];
8291
8292   if (!icon_info)
8293     return NULL;
8294  
8295   return g_strdup (icon_info->tooltip);
8296 }
8297
8298 /**
8299  * gtk_entry_set_icon_tooltip_markup:
8300  * @entry: a #GtkEntry
8301  * @icon_pos: the icon position
8302  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8303  *
8304  * Sets @tooltip as the contents of the tooltip for the icon at
8305  * the specified position. @tooltip is assumed to be marked up with
8306  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8307  *
8308  * Use %NULL for @tooltip to remove an existing tooltip.
8309  *
8310  * See also gtk_widget_set_tooltip_markup() and 
8311  * gtk_enty_set_icon_tooltip_text().
8312  *
8313  * Since: 2.16
8314  */
8315 void
8316 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8317                                    GtkEntryIconPosition  icon_pos,
8318                                    const gchar          *tooltip)
8319 {
8320   GtkEntryPrivate *priv;
8321   EntryIconInfo *icon_info;
8322
8323   g_return_if_fail (GTK_IS_ENTRY (entry));
8324   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8325
8326   priv = GTK_ENTRY_GET_PRIVATE (entry);
8327
8328   if (!(icon_info = priv->icons[icon_pos]))
8329     icon_info = priv->icons[icon_pos];
8330
8331   if (icon_info->tooltip)
8332     g_free (icon_info->tooltip);
8333
8334   /* Treat an empty string as a NULL string,
8335    * because an empty string would be useless for a tooltip:
8336    */
8337   if (tooltip && tooltip[0] == '\0')
8338     tooltip = NULL;
8339
8340   icon_info->tooltip = g_strdup (tooltip);
8341
8342   ensure_has_tooltip (entry);
8343 }
8344
8345 static gboolean
8346 gtk_entry_query_tooltip (GtkWidget  *widget,
8347                          gint        x,
8348                          gint        y,
8349                          gboolean    keyboard_tip,
8350                          GtkTooltip *tooltip)
8351 {
8352   GtkEntry *entry;
8353   GtkEntryPrivate *priv;
8354   EntryIconInfo *icon_info;
8355   gint icon_pos;
8356
8357   entry = GTK_ENTRY (widget);
8358   priv = GTK_ENTRY_GET_PRIVATE (entry);
8359
8360   if (!keyboard_tip)
8361     {
8362       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8363       if (icon_pos != -1)
8364         {
8365           if ((icon_info = priv->icons[icon_pos]) != NULL)
8366             {
8367               if (icon_info->tooltip)
8368                 {
8369                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8370                   return TRUE;
8371                 }
8372
8373               return FALSE;
8374             }
8375         }
8376     }
8377
8378   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8379                                                                    x, y,
8380                                                                    keyboard_tip,
8381                                                                    tooltip);
8382 }
8383
8384
8385 /* Quick hack of a popup menu
8386  */
8387 static void
8388 activate_cb (GtkWidget *menuitem,
8389              GtkEntry  *entry)
8390 {
8391   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8392   g_signal_emit_by_name (entry, signal);
8393 }
8394
8395
8396 static gboolean
8397 gtk_entry_mnemonic_activate (GtkWidget *widget,
8398                              gboolean   group_cycling)
8399 {
8400   gtk_widget_grab_focus (widget);
8401   return TRUE;
8402 }
8403
8404 static void
8405 append_action_signal (GtkEntry     *entry,
8406                       GtkWidget    *menu,
8407                       const gchar  *stock_id,
8408                       const gchar  *signal,
8409                       gboolean      sensitive)
8410 {
8411   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8412
8413   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8414   g_signal_connect (menuitem, "activate",
8415                     G_CALLBACK (activate_cb), entry);
8416
8417   gtk_widget_set_sensitive (menuitem, sensitive);
8418   
8419   gtk_widget_show (menuitem);
8420   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8421 }
8422         
8423 static void
8424 popup_menu_detach (GtkWidget *attach_widget,
8425                    GtkMenu   *menu)
8426 {
8427   GTK_ENTRY (attach_widget)->popup_menu = NULL;
8428 }
8429
8430 static void
8431 popup_position_func (GtkMenu   *menu,
8432                      gint      *x,
8433                      gint      *y,
8434                      gboolean  *push_in,
8435                      gpointer   user_data)
8436 {
8437   GtkEntry *entry = GTK_ENTRY (user_data);
8438   GtkWidget *widget = GTK_WIDGET (entry);
8439   GdkScreen *screen;
8440   GtkRequisition menu_req;
8441   GdkRectangle monitor;
8442   GtkBorder inner_border;
8443   gint monitor_num, strong_x, height;
8444  
8445   g_return_if_fail (gtk_widget_get_realized (widget));
8446
8447   gdk_window_get_origin (entry->text_area, x, y);
8448
8449   screen = gtk_widget_get_screen (widget);
8450   monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
8451   if (monitor_num < 0)
8452     monitor_num = 0;
8453   gtk_menu_set_monitor (menu, monitor_num);
8454
8455   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8456   gtk_widget_get_preferred_size (entry->popup_menu,
8457                                  &menu_req, NULL);
8458   height = gdk_window_get_height (entry->text_area);
8459   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8460   _gtk_entry_effective_inner_border (entry, &inner_border);
8461
8462   *x += inner_border.left + strong_x - entry->scroll_offset;
8463   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8464     *x -= menu_req.width;
8465
8466   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8467     *y += height;
8468   else if ((*y - menu_req.height) >= monitor.y)
8469     *y -= menu_req.height;
8470   else if (monitor.y + monitor.height - (*y + height) > *y)
8471     *y += height;
8472   else
8473     *y -= menu_req.height;
8474
8475   *push_in = FALSE;
8476 }
8477
8478 static void
8479 unichar_chosen_func (const char *text,
8480                      gpointer    data)
8481 {
8482   GtkEntry *entry = GTK_ENTRY (data);
8483
8484   if (entry->editable)
8485     gtk_entry_enter_text (entry, text);
8486 }
8487
8488 typedef struct
8489 {
8490   GtkEntry *entry;
8491   gint button;
8492   guint time;
8493 } PopupInfo;
8494
8495 static void
8496 popup_targets_received (GtkClipboard     *clipboard,
8497                         GtkSelectionData *data,
8498                         gpointer          user_data)
8499 {
8500   PopupInfo *info = user_data;
8501   GtkEntry *entry = info->entry;
8502   
8503   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8504     {
8505       DisplayMode mode;
8506       gboolean clipboard_contains_text;
8507       GtkWidget *menuitem;
8508       GtkWidget *submenu;
8509       gboolean show_input_method_menu;
8510       gboolean show_unicode_menu;
8511       
8512       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8513       if (entry->popup_menu)
8514         gtk_widget_destroy (entry->popup_menu);
8515       
8516       entry->popup_menu = gtk_menu_new ();
8517       
8518       gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
8519                                  GTK_WIDGET (entry),
8520                                  popup_menu_detach);
8521       
8522       mode = gtk_entry_get_display_mode (entry);
8523       append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8524                             entry->editable && mode == DISPLAY_NORMAL &&
8525                             entry->current_pos != entry->selection_bound);
8526
8527       append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8528                             mode == DISPLAY_NORMAL &&
8529                             entry->current_pos != entry->selection_bound);
8530
8531       append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8532                             entry->editable && clipboard_contains_text);
8533
8534       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8535       gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
8536       g_signal_connect_swapped (menuitem, "activate",
8537                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8538       gtk_widget_show (menuitem);
8539       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8540
8541       menuitem = gtk_separator_menu_item_new ();
8542       gtk_widget_show (menuitem);
8543       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8544       
8545       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8546       g_signal_connect_swapped (menuitem, "activate",
8547                                 G_CALLBACK (gtk_entry_select_all), entry);
8548       gtk_widget_show (menuitem);
8549       gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8550       
8551       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8552                     "gtk-show-input-method-menu", &show_input_method_menu,
8553                     "gtk-show-unicode-menu", &show_unicode_menu,
8554                     NULL);
8555
8556       if (show_input_method_menu || show_unicode_menu)
8557         {
8558           menuitem = gtk_separator_menu_item_new ();
8559           gtk_widget_show (menuitem);
8560           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8561         }
8562       
8563       if (show_input_method_menu)
8564         {
8565           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8566           gtk_widget_set_sensitive (menuitem, entry->editable);      
8567           gtk_widget_show (menuitem);
8568           submenu = gtk_menu_new ();
8569           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8570           
8571           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
8572       
8573           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
8574                                                 GTK_MENU_SHELL (submenu));
8575         }
8576       
8577       if (show_unicode_menu)
8578         {
8579           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8580           gtk_widget_set_sensitive (menuitem, entry->editable);      
8581           gtk_widget_show (menuitem);
8582           
8583           submenu = gtk_menu_new ();
8584           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8585           gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);      
8586           
8587           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8588                                                         unichar_chosen_func,
8589                                                         entry);
8590         }
8591       
8592       g_signal_emit (entry,
8593                      signals[POPULATE_POPUP],
8594                      0,
8595                      entry->popup_menu);
8596   
8597
8598       if (info->button)
8599         gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8600                         NULL, NULL,
8601                         info->button, info->time);
8602       else
8603         {
8604           gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
8605                           popup_position_func, entry,
8606                           info->button, info->time);
8607           gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
8608         }
8609     }
8610
8611   g_object_unref (entry);
8612   g_slice_free (PopupInfo, info);
8613 }
8614                         
8615 static void
8616 gtk_entry_do_popup (GtkEntry       *entry,
8617                     GdkEventButton *event)
8618 {
8619   PopupInfo *info = g_slice_new (PopupInfo);
8620
8621   /* In order to know what entries we should make sensitive, we
8622    * ask for the current targets of the clipboard, and when
8623    * we get them, then we actually pop up the menu.
8624    */
8625   info->entry = g_object_ref (entry);
8626   
8627   if (event)
8628     {
8629       info->button = event->button;
8630       info->time = event->time;
8631     }
8632   else
8633     {
8634       info->button = 0;
8635       info->time = gtk_get_current_event_time ();
8636     }
8637
8638   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8639                                   gdk_atom_intern_static_string ("TARGETS"),
8640                                   popup_targets_received,
8641                                   info);
8642 }
8643
8644 static gboolean
8645 gtk_entry_popup_menu (GtkWidget *widget)
8646 {
8647   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8648   return TRUE;
8649 }
8650
8651 static void
8652 gtk_entry_drag_begin (GtkWidget      *widget,
8653                       GdkDragContext *context)
8654 {
8655   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8656   gint i;
8657
8658   for (i = 0; i < MAX_ICONS; i++)
8659     {
8660       EntryIconInfo *icon_info = priv->icons[i];
8661       
8662       if (icon_info != NULL)
8663         {
8664           if (icon_info->in_drag) 
8665             {
8666               switch (icon_info->storage_type)
8667                 {
8668                 case GTK_IMAGE_STOCK:
8669                   gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
8670                   break;
8671
8672                 case GTK_IMAGE_ICON_NAME:
8673                   gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
8674                   break;
8675
8676                   /* FIXME: No GIcon support for dnd icons */
8677                 case GTK_IMAGE_GICON:
8678                 case GTK_IMAGE_PIXBUF:
8679                   gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
8680                   break;
8681                 default:
8682                   g_assert_not_reached ();
8683                 }
8684             }
8685         }
8686     }
8687 }
8688
8689 static void
8690 gtk_entry_drag_end (GtkWidget      *widget,
8691                     GdkDragContext *context)
8692 {
8693   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8694   gint i;
8695
8696   for (i = 0; i < MAX_ICONS; i++)
8697     {
8698       EntryIconInfo *icon_info = priv->icons[i];
8699
8700       if (icon_info != NULL)
8701         icon_info->in_drag = 0;
8702     }
8703 }
8704
8705 static void
8706 gtk_entry_drag_leave (GtkWidget        *widget,
8707                       GdkDragContext   *context,
8708                       guint             time)
8709 {
8710   GtkEntry *entry = GTK_ENTRY (widget);
8711
8712   entry->dnd_position = -1;
8713   gtk_widget_queue_draw (widget);
8714 }
8715
8716 static gboolean
8717 gtk_entry_drag_drop  (GtkWidget        *widget,
8718                       GdkDragContext   *context,
8719                       gint              x,
8720                       gint              y,
8721                       guint             time)
8722 {
8723   GtkEntry *entry = GTK_ENTRY (widget);
8724   GdkAtom target = GDK_NONE;
8725   
8726   if (entry->editable)
8727     target = gtk_drag_dest_find_target (widget, context, NULL);
8728
8729   if (target != GDK_NONE)
8730     gtk_drag_get_data (widget, context, target, time);
8731   else
8732     gtk_drag_finish (context, FALSE, FALSE, time);
8733   
8734   return TRUE;
8735 }
8736
8737 static gboolean
8738 gtk_entry_drag_motion (GtkWidget        *widget,
8739                        GdkDragContext   *context,
8740                        gint              x,
8741                        gint              y,
8742                        guint             time)
8743 {
8744   GtkEntry *entry = GTK_ENTRY (widget);
8745   GtkStyle *style;
8746   GtkWidget *source_widget;
8747   GdkDragAction suggested_action;
8748   gint new_position, old_position;
8749   gint sel1, sel2;
8750
8751   style = gtk_widget_get_style (widget);
8752   x -= style->xthickness;
8753   y -= style->ythickness;
8754
8755   old_position = entry->dnd_position;
8756   new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8757
8758   if (entry->editable &&
8759       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8760     {
8761       source_widget = gtk_drag_get_source_widget (context);
8762       suggested_action = context->suggested_action;
8763
8764       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8765           new_position < sel1 || new_position > sel2)
8766         {
8767           if (source_widget == widget)
8768             {
8769               /* Default to MOVE, unless the user has
8770                * pressed ctrl or alt to affect available actions
8771                */
8772               if ((context->actions & GDK_ACTION_MOVE) != 0)
8773                 suggested_action = GDK_ACTION_MOVE;
8774             }
8775               
8776           entry->dnd_position = new_position;
8777         }
8778       else
8779         {
8780           if (source_widget == widget)
8781             suggested_action = 0;       /* Can't drop in selection where drag started */
8782           
8783           entry->dnd_position = -1;
8784         }
8785     }
8786   else
8787     {
8788       /* Entry not editable, or no text */
8789       suggested_action = 0;
8790       entry->dnd_position = -1;
8791     }
8792   
8793   gdk_drag_status (context, suggested_action, time);
8794   
8795   if (entry->dnd_position != old_position)
8796     gtk_widget_queue_draw (widget);
8797
8798   return TRUE;
8799 }
8800
8801 static void
8802 gtk_entry_drag_data_received (GtkWidget        *widget,
8803                               GdkDragContext   *context,
8804                               gint              x,
8805                               gint              y,
8806                               GtkSelectionData *selection_data,
8807                               guint             info,
8808                               guint             time)
8809 {
8810   GtkEntry *entry = GTK_ENTRY (widget);
8811   GtkEditable *editable = GTK_EDITABLE (widget);
8812   GtkStyle *style;
8813   gchar *str;
8814
8815   str = (gchar *) gtk_selection_data_get_text (selection_data);
8816
8817   style = gtk_widget_get_style (widget);
8818   x -= style->xthickness;
8819   y -= style->ythickness;
8820
8821   if (str && entry->editable)
8822     {
8823       gint new_position;
8824       gint sel1, sel2;
8825       gint length = -1;
8826
8827       if (entry->truncate_multiline)
8828         length = truncate_multiline (str);
8829
8830       new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8831
8832       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8833           new_position < sel1 || new_position > sel2)
8834         {
8835           gtk_editable_insert_text (editable, str, length, &new_position);
8836         }
8837       else
8838         {
8839           /* Replacing selection */
8840           begin_change (entry);
8841           g_object_freeze_notify (G_OBJECT (entry));
8842           gtk_editable_delete_text (editable, sel1, sel2);
8843           gtk_editable_insert_text (editable, str, length, &sel1);
8844           g_object_thaw_notify (G_OBJECT (entry));
8845           end_change (entry);
8846         }
8847       
8848       gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
8849     }
8850   else
8851     {
8852       /* Drag and drop didn't happen! */
8853       gtk_drag_finish (context, FALSE, FALSE, time);
8854     }
8855
8856   g_free (str);
8857 }
8858
8859 static void
8860 gtk_entry_drag_data_get (GtkWidget        *widget,
8861                          GdkDragContext   *context,
8862                          GtkSelectionData *selection_data,
8863                          guint             info,
8864                          guint             time)
8865 {
8866   gint sel_start, sel_end;
8867   gint i;
8868
8869   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8870   GtkEditable *editable = GTK_EDITABLE (widget);
8871
8872   /* If there is an icon drag going on, exit early. */
8873   for (i = 0; i < MAX_ICONS; i++)
8874     {
8875       EntryIconInfo *icon_info = priv->icons[i];
8876
8877       if (icon_info != NULL)
8878         {
8879           if (icon_info->in_drag)
8880             return;
8881         }
8882     }
8883
8884   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8885     {
8886       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
8887
8888       gtk_selection_data_set_text (selection_data, str, -1);
8889       
8890       g_free (str);
8891     }
8892
8893 }
8894
8895 static void
8896 gtk_entry_drag_data_delete (GtkWidget      *widget,
8897                             GdkDragContext *context)
8898 {
8899   gint sel_start, sel_end;
8900   gint i;
8901
8902   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8903   GtkEditable *editable = GTK_EDITABLE (widget);
8904
8905   /* If there is an icon drag going on, exit early. */
8906   for (i = 0; i < MAX_ICONS; i++)
8907     {
8908       EntryIconInfo *icon_info = priv->icons[i];
8909
8910       if (icon_info != NULL)
8911         {
8912           if (icon_info->in_drag)
8913             return;
8914         }
8915     }
8916   
8917   if (GTK_ENTRY (widget)->editable &&
8918       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8919     gtk_editable_delete_text (editable, sel_start, sel_end);
8920 }
8921
8922 /* We display the cursor when
8923  *
8924  *  - the selection is empty, AND
8925  *  - the widget has focus
8926  */
8927
8928 #define CURSOR_ON_MULTIPLIER 2
8929 #define CURSOR_OFF_MULTIPLIER 1
8930 #define CURSOR_PEND_MULTIPLIER 3
8931 #define CURSOR_DIVIDER 3
8932
8933 static gboolean
8934 cursor_blinks (GtkEntry *entry)
8935 {
8936   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
8937       entry->editable &&
8938       entry->selection_bound == entry->current_pos)
8939     {
8940       GtkSettings *settings;
8941       gboolean blink;
8942
8943       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8944       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
8945
8946       return blink;
8947     }
8948   else
8949     return FALSE;
8950 }
8951
8952 static gint
8953 get_cursor_time (GtkEntry *entry)
8954 {
8955   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8956   gint time;
8957
8958   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
8959
8960   return time;
8961 }
8962
8963 static gint
8964 get_cursor_blink_timeout (GtkEntry *entry)
8965 {
8966   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8967   gint timeout;
8968
8969   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
8970
8971   return timeout;
8972 }
8973
8974 static void
8975 show_cursor (GtkEntry *entry)
8976 {
8977   GtkWidget *widget;
8978
8979   if (!entry->cursor_visible)
8980     {
8981       entry->cursor_visible = TRUE;
8982
8983       widget = GTK_WIDGET (entry);
8984       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
8985         gtk_widget_queue_draw (widget);
8986     }
8987 }
8988
8989 static void
8990 hide_cursor (GtkEntry *entry)
8991 {
8992   GtkWidget *widget;
8993
8994   if (entry->cursor_visible)
8995     {
8996       entry->cursor_visible = FALSE;
8997
8998       widget = GTK_WIDGET (entry);
8999       if (gtk_widget_has_focus (widget) && entry->selection_bound == entry->current_pos)
9000         gtk_widget_queue_draw (widget);
9001     }
9002 }
9003
9004 /*
9005  * Blink!
9006  */
9007 static gint
9008 blink_cb (gpointer data)
9009 {
9010   GtkEntry *entry;
9011   GtkEntryPrivate *priv; 
9012   gint blink_timeout;
9013
9014   entry = GTK_ENTRY (data);
9015   priv = GTK_ENTRY_GET_PRIVATE (entry);
9016  
9017   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9018     {
9019       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9020                  "connect a handler to this signal, it must return\n"
9021                  "FALSE so the entry gets the event as well");
9022
9023       gtk_entry_check_cursor_blink (entry);
9024
9025       return FALSE;
9026     }
9027   
9028   g_assert (entry->selection_bound == entry->current_pos);
9029   
9030   blink_timeout = get_cursor_blink_timeout (entry);
9031   if (priv->blink_time > 1000 * blink_timeout && 
9032       blink_timeout < G_MAXINT/1000) 
9033     {
9034       /* we've blinked enough without the user doing anything, stop blinking */
9035       show_cursor (entry);
9036       entry->blink_timeout = 0;
9037     } 
9038   else if (entry->cursor_visible)
9039     {
9040       hide_cursor (entry);
9041       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9042                                             blink_cb,
9043                                             entry);
9044     }
9045   else
9046     {
9047       show_cursor (entry);
9048       priv->blink_time += get_cursor_time (entry);
9049       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9050                                             blink_cb,
9051                                             entry);
9052     }
9053
9054   /* Remove ourselves */
9055   return FALSE;
9056 }
9057
9058 static void
9059 gtk_entry_check_cursor_blink (GtkEntry *entry)
9060 {
9061   GtkEntryPrivate *priv; 
9062   
9063   priv = GTK_ENTRY_GET_PRIVATE (entry);
9064
9065   if (cursor_blinks (entry))
9066     {
9067       if (!entry->blink_timeout)
9068         {
9069           show_cursor (entry);
9070           entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9071                                                 blink_cb,
9072                                                 entry);
9073         }
9074     }
9075   else
9076     {
9077       if (entry->blink_timeout)  
9078         { 
9079           g_source_remove (entry->blink_timeout);
9080           entry->blink_timeout = 0;
9081         }
9082       
9083       entry->cursor_visible = TRUE;
9084     }
9085   
9086 }
9087
9088 static void
9089 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9090 {
9091   if (cursor_blinks (entry))
9092     {
9093       if (entry->blink_timeout != 0)
9094         g_source_remove (entry->blink_timeout);
9095       
9096       entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9097                                             blink_cb,
9098                                             entry);
9099       show_cursor (entry);
9100     }
9101 }
9102
9103 static void
9104 gtk_entry_reset_blink_time (GtkEntry *entry)
9105 {
9106   GtkEntryPrivate *priv; 
9107   
9108   priv = GTK_ENTRY_GET_PRIVATE (entry);
9109   
9110   priv->blink_time = 0;
9111 }
9112
9113
9114 /* completion */
9115 static gint
9116 gtk_entry_completion_timeout (gpointer data)
9117 {
9118   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9119   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (completion->priv->entry);
9120
9121   completion->priv->completion_timeout = 0;
9122
9123   if (completion->priv->filter_model &&
9124       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9125       >= completion->priv->minimum_key_length)
9126     {
9127       gint matches;
9128       gint actions;
9129       GtkTreeSelection *s;
9130       gboolean popup_single;
9131
9132       gtk_entry_completion_complete (completion);
9133       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9134
9135       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9136
9137       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9138
9139       gtk_tree_selection_unselect_all (s);
9140
9141       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9142
9143       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9144       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9145         { 
9146           if (gtk_widget_get_visible (completion->priv->popup_window))
9147             _gtk_entry_completion_resize_popup (completion);
9148           else
9149             _gtk_entry_completion_popup (completion, priv->completion_device);
9150         }
9151       else 
9152         _gtk_entry_completion_popdown (completion);
9153     }
9154   else if (gtk_widget_get_visible (completion->priv->popup_window))
9155     _gtk_entry_completion_popdown (completion);
9156
9157   return FALSE;
9158 }
9159
9160 static inline gboolean
9161 keyval_is_cursor_move (guint keyval)
9162 {
9163   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9164     return TRUE;
9165
9166   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9167     return TRUE;
9168
9169   if (keyval == GDK_KEY_Page_Up)
9170     return TRUE;
9171
9172   if (keyval == GDK_KEY_Page_Down)
9173     return TRUE;
9174
9175   return FALSE;
9176 }
9177
9178 static gboolean
9179 gtk_entry_completion_key_press (GtkWidget   *widget,
9180                                 GdkEventKey *event,
9181                                 gpointer     user_data)
9182 {
9183   gint matches, actions = 0;
9184   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9185
9186   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9187     return FALSE;
9188
9189   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9190
9191   if (completion->priv->actions)
9192     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9193
9194   if (keyval_is_cursor_move (event->keyval))
9195     {
9196       GtkTreePath *path = NULL;
9197       
9198       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9199         {
9200           if (completion->priv->current_selected < 0)
9201             completion->priv->current_selected = matches + actions - 1;
9202           else
9203             completion->priv->current_selected--;
9204         }
9205       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9206         {
9207           if (completion->priv->current_selected < matches + actions - 1)
9208             completion->priv->current_selected++;
9209           else
9210             completion->priv->current_selected = -1;
9211         }
9212       else if (event->keyval == GDK_KEY_Page_Up)
9213         {
9214           if (completion->priv->current_selected < 0)
9215             completion->priv->current_selected = matches + actions - 1;
9216           else if (completion->priv->current_selected == 0)
9217             completion->priv->current_selected = -1;
9218           else if (completion->priv->current_selected < matches) 
9219             {
9220               completion->priv->current_selected -= 14;
9221               if (completion->priv->current_selected < 0)
9222                 completion->priv->current_selected = 0;
9223             }
9224           else 
9225             {
9226               completion->priv->current_selected -= 14;
9227               if (completion->priv->current_selected < matches - 1)
9228                 completion->priv->current_selected = matches - 1;
9229             }
9230         }
9231       else if (event->keyval == GDK_KEY_Page_Down)
9232         {
9233           if (completion->priv->current_selected < 0)
9234             completion->priv->current_selected = 0;
9235           else if (completion->priv->current_selected < matches - 1)
9236             {
9237               completion->priv->current_selected += 14;
9238               if (completion->priv->current_selected > matches - 1)
9239                 completion->priv->current_selected = matches - 1;
9240             }
9241           else if (completion->priv->current_selected == matches + actions - 1)
9242             {
9243               completion->priv->current_selected = -1;
9244             }
9245           else
9246             {
9247               completion->priv->current_selected += 14;
9248               if (completion->priv->current_selected > matches + actions - 1)
9249                 completion->priv->current_selected = matches + actions - 1;
9250             }
9251         }
9252
9253       if (completion->priv->current_selected < 0)
9254         {
9255           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9256           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9257
9258           if (completion->priv->inline_selection &&
9259               completion->priv->completion_prefix)
9260             {
9261               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9262                                   completion->priv->completion_prefix);
9263               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9264             }
9265         }
9266       else if (completion->priv->current_selected < matches)
9267         {
9268           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9269
9270           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9271           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9272                                     path, NULL, FALSE);
9273
9274           if (completion->priv->inline_selection)
9275             {
9276
9277               GtkTreeIter iter;
9278               GtkTreeIter child_iter;
9279               GtkTreeModel *model = NULL;
9280               GtkTreeSelection *sel;
9281               gboolean entry_set;
9282
9283               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9284               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9285                 return FALSE;
9286
9287               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9288               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9289               
9290               if (completion->priv->completion_prefix == NULL)
9291                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9292
9293               g_signal_emit_by_name (completion, "cursor-on-match", model,
9294                                      &child_iter, &entry_set);
9295             }
9296         }
9297       else if (completion->priv->current_selected - matches >= 0)
9298         {
9299           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9300
9301           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9302           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9303                                     path, NULL, FALSE);
9304
9305           if (completion->priv->inline_selection &&
9306               completion->priv->completion_prefix)
9307             {
9308               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9309                                   completion->priv->completion_prefix);
9310               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9311             }
9312         }
9313
9314       gtk_tree_path_free (path);
9315
9316       return TRUE;
9317     }
9318   else if (event->keyval == GDK_KEY_Escape ||
9319            event->keyval == GDK_KEY_Left ||
9320            event->keyval == GDK_KEY_KP_Left ||
9321            event->keyval == GDK_KEY_Right ||
9322            event->keyval == GDK_KEY_KP_Right) 
9323     {
9324       gboolean retval = TRUE;
9325
9326       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9327       _gtk_entry_completion_popdown (completion);
9328
9329       if (completion->priv->current_selected < 0)
9330         {
9331           retval = FALSE;
9332           goto keypress_completion_out;
9333         }
9334       else if (completion->priv->inline_selection)
9335         {
9336           /* Escape rejects the tentative completion */
9337           if (event->keyval == GDK_KEY_Escape)
9338             {
9339               if (completion->priv->completion_prefix)
9340                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9341                                     completion->priv->completion_prefix);
9342               else 
9343                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9344             }
9345
9346           /* Move the cursor to the end for Right/Esc, to the
9347              beginning for Left */
9348           if (event->keyval == GDK_KEY_Right ||
9349               event->keyval == GDK_KEY_KP_Right ||
9350               event->keyval == GDK_KEY_Escape)
9351             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9352           else
9353             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9354         }
9355
9356 keypress_completion_out:
9357       if (completion->priv->inline_selection)
9358         {
9359           g_free (completion->priv->completion_prefix);
9360           completion->priv->completion_prefix = NULL;
9361         }
9362
9363       return retval;
9364     }
9365   else if (event->keyval == GDK_KEY_Tab || 
9366            event->keyval == GDK_KEY_KP_Tab ||
9367            event->keyval == GDK_KEY_ISO_Left_Tab) 
9368     {
9369       GtkDirectionType dir = event->keyval == GDK_KEY_ISO_Left_Tab ? 
9370         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
9371       
9372       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9373       _gtk_entry_completion_popdown (completion);
9374
9375       g_free (completion->priv->completion_prefix);
9376       completion->priv->completion_prefix = NULL;
9377
9378       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
9379
9380       return TRUE;
9381     }
9382   else if (event->keyval == GDK_KEY_ISO_Enter ||
9383            event->keyval == GDK_KEY_KP_Enter ||
9384            event->keyval == GDK_KEY_Return)
9385     {
9386       GtkTreeIter iter;
9387       GtkTreeModel *model = NULL;
9388       GtkTreeSelection *sel;
9389       gboolean retval = TRUE;
9390
9391       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9392       _gtk_entry_completion_popdown (completion);
9393
9394       if (completion->priv->current_selected < matches)
9395         {
9396           gboolean entry_set;
9397
9398           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9399           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9400             {
9401               g_signal_handler_block (widget, completion->priv->changed_id);
9402               g_signal_emit_by_name (completion, "match-selected",
9403                                      model, &iter, &entry_set);
9404               g_signal_handler_unblock (widget, completion->priv->changed_id);
9405
9406               if (!entry_set)
9407                 {
9408                   gchar *str = NULL;
9409
9410                   gtk_tree_model_get (model, &iter,
9411                                       completion->priv->text_column, &str,
9412                                       -1);
9413
9414                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9415
9416                   /* move the cursor to the end */
9417                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9418
9419                   g_free (str);
9420                 }
9421             }
9422           else
9423             retval = FALSE;
9424         }
9425       else if (completion->priv->current_selected - matches >= 0)
9426         {
9427           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9428           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9429             {
9430               GtkTreePath *path;
9431
9432               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9433               g_signal_emit_by_name (completion, "action-activated",
9434                                      gtk_tree_path_get_indices (path)[0]);
9435               gtk_tree_path_free (path);
9436             }
9437           else
9438             retval = FALSE;
9439         }
9440
9441       g_free (completion->priv->completion_prefix);
9442       completion->priv->completion_prefix = NULL;
9443
9444       return retval;
9445     }
9446
9447   return FALSE;
9448 }
9449
9450 static void
9451 gtk_entry_completion_changed (GtkWidget *entry,
9452                               gpointer   user_data)
9453 {
9454   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9455   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9456   GdkDevice *device;
9457
9458   /* (re)install completion timeout */
9459   if (completion->priv->completion_timeout)
9460     g_source_remove (completion->priv->completion_timeout);
9461
9462   if (!gtk_entry_get_text (GTK_ENTRY (entry)))
9463     return;
9464
9465   /* no need to normalize for this test */
9466   if (completion->priv->minimum_key_length > 0 &&
9467       strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
9468     {
9469       if (gtk_widget_get_visible (completion->priv->popup_window))
9470         _gtk_entry_completion_popdown (completion);
9471       return;
9472     }
9473
9474   device = gtk_get_current_event_device ();
9475
9476   if (device && device->source == GDK_SOURCE_KEYBOARD)
9477     device = gdk_device_get_associated_device (device);
9478
9479   if (device)
9480     priv->completion_device = device;
9481
9482   completion->priv->completion_timeout =
9483     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9484                    gtk_entry_completion_timeout,
9485                    completion);
9486 }
9487
9488 static gboolean
9489 check_completion_callback (GtkEntryCompletion *completion)
9490 {
9491   completion->priv->check_completion_idle = NULL;
9492   
9493   gtk_entry_completion_complete (completion);
9494   gtk_entry_completion_insert_prefix (completion);
9495
9496   return FALSE;
9497 }
9498
9499 static void
9500 clear_completion_callback (GtkEntry   *entry,
9501                            GParamSpec *pspec)
9502 {
9503   if (pspec->name == I_("cursor-position") ||
9504       pspec->name == I_("selection-bound"))
9505     {
9506       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9507       
9508       completion->priv->has_completion = FALSE;
9509     }
9510 }
9511
9512 static gboolean
9513 accept_completion_callback (GtkEntry *entry)
9514 {
9515   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9516
9517   if (completion->priv->has_completion)
9518     gtk_editable_set_position (GTK_EDITABLE (entry),
9519                                gtk_entry_buffer_get_length (get_buffer (entry)));
9520
9521   return FALSE;
9522 }
9523
9524 static void
9525 completion_insert_text_callback (GtkEntry           *entry,
9526                                  const gchar        *text,
9527                                  gint                length,
9528                                  gint                position,
9529                                  GtkEntryCompletion *completion)
9530 {
9531   /* idle to update the selection based on the file list */
9532   if (completion->priv->check_completion_idle == NULL)
9533     {
9534       completion->priv->check_completion_idle = g_idle_source_new ();
9535       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9536       g_source_set_closure (completion->priv->check_completion_idle,
9537                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9538                                                    G_OBJECT (completion)));
9539       g_source_attach (completion->priv->check_completion_idle, NULL);
9540     }
9541 }
9542
9543 static void
9544 completion_changed (GtkEntryCompletion *completion,
9545                     GParamSpec         *pspec,
9546                     gpointer            data)
9547 {
9548   GtkEntry *entry = GTK_ENTRY (data);
9549
9550   if (pspec->name == I_("popup-completion") ||
9551       pspec->name == I_("inline-completion"))
9552     {
9553       disconnect_completion_signals (entry, completion);
9554       connect_completion_signals (entry, completion);
9555     }
9556 }
9557
9558 static void
9559 disconnect_completion_signals (GtkEntry           *entry,
9560                                GtkEntryCompletion *completion)
9561 {
9562   g_signal_handlers_disconnect_by_func (completion, 
9563                                        G_CALLBACK (completion_changed), entry);
9564   if (completion->priv->changed_id > 0 &&
9565       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9566     {
9567       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9568       completion->priv->changed_id = 0;
9569     }
9570   g_signal_handlers_disconnect_by_func (entry, 
9571                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9572   if (completion->priv->insert_text_id > 0 &&
9573       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9574     {
9575       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9576       completion->priv->insert_text_id = 0;
9577     }
9578   g_signal_handlers_disconnect_by_func (entry, 
9579                                         G_CALLBACK (completion_insert_text_callback), completion);
9580   g_signal_handlers_disconnect_by_func (entry, 
9581                                         G_CALLBACK (clear_completion_callback), completion);
9582   g_signal_handlers_disconnect_by_func (entry, 
9583                                         G_CALLBACK (accept_completion_callback), completion);
9584 }
9585
9586 static void
9587 connect_completion_signals (GtkEntry           *entry,
9588                             GtkEntryCompletion *completion)
9589 {
9590   if (completion->priv->popup_completion)
9591     {
9592       completion->priv->changed_id =
9593         g_signal_connect (entry, "changed",
9594                           G_CALLBACK (gtk_entry_completion_changed), completion);
9595       g_signal_connect (entry, "key-press-event",
9596                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9597     }
9598  
9599   if (completion->priv->inline_completion)
9600     {
9601       completion->priv->insert_text_id =
9602         g_signal_connect (entry, "insert-text",
9603                           G_CALLBACK (completion_insert_text_callback), completion);
9604       g_signal_connect (entry, "notify",
9605                         G_CALLBACK (clear_completion_callback), completion);
9606       g_signal_connect (entry, "activate",
9607                         G_CALLBACK (accept_completion_callback), completion);
9608       g_signal_connect (entry, "focus-out-event",
9609                         G_CALLBACK (accept_completion_callback), completion);
9610     }
9611
9612   g_signal_connect (completion, "notify",
9613                     G_CALLBACK (completion_changed), entry);
9614 }
9615
9616 /**
9617  * gtk_entry_set_completion:
9618  * @entry: A #GtkEntry
9619  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9620  *
9621  * Sets @completion to be the auxiliary completion object to use with @entry.
9622  * All further configuration of the completion mechanism is done on
9623  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9624  * @completion is set to %NULL.
9625  *
9626  * Since: 2.4
9627  */
9628 void
9629 gtk_entry_set_completion (GtkEntry           *entry,
9630                           GtkEntryCompletion *completion)
9631 {
9632   GtkEntryCompletion *old;
9633
9634   g_return_if_fail (GTK_IS_ENTRY (entry));
9635   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9636
9637   old = gtk_entry_get_completion (entry);
9638
9639   if (old == completion)
9640     return;
9641   
9642   if (old)
9643     {
9644       if (old->priv->completion_timeout)
9645         {
9646           g_source_remove (old->priv->completion_timeout);
9647           old->priv->completion_timeout = 0;
9648         }
9649
9650       if (gtk_widget_get_mapped (old->priv->popup_window))
9651         _gtk_entry_completion_popdown (old);
9652
9653       disconnect_completion_signals (entry, old);
9654       old->priv->entry = NULL;
9655
9656       g_object_unref (old);
9657     }
9658
9659   if (!completion)
9660     {
9661       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9662       return;
9663     }
9664
9665   /* hook into the entry */
9666   g_object_ref (completion);
9667
9668   connect_completion_signals (entry, completion);    
9669   completion->priv->entry = GTK_WIDGET (entry);
9670   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9671 }
9672
9673 /**
9674  * gtk_entry_get_completion:
9675  * @entry: A #GtkEntry
9676  *
9677  * Returns the auxiliary completion object currently in use by @entry.
9678  *
9679  * Return value: (transfer none): The auxiliary completion object currently
9680  *     in use by @entry.
9681  *
9682  * Since: 2.4
9683  */
9684 GtkEntryCompletion *
9685 gtk_entry_get_completion (GtkEntry *entry)
9686 {
9687   GtkEntryCompletion *completion;
9688
9689   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9690
9691   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9692                                      GTK_ENTRY_COMPLETION_KEY));
9693
9694   return completion;
9695 }
9696
9697 /**
9698  * gtk_entry_set_cursor_hadjustment:
9699  * @entry: a #GtkEntry
9700  * @adjustment: an adjustment which should be adjusted when the cursor 
9701  *              is moved, or %NULL
9702  *
9703  * Hooks up an adjustment to the cursor position in an entry, so that when 
9704  * the cursor is moved, the adjustment is scrolled to show that position. 
9705  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9706  * the adjustment.
9707  *
9708  * The adjustment has to be in pixel units and in the same coordinate system 
9709  * as the entry. 
9710  * 
9711  * Since: 2.12
9712  */
9713 void
9714 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9715                                   GtkAdjustment *adjustment)
9716 {
9717   g_return_if_fail (GTK_IS_ENTRY (entry));
9718   if (adjustment)
9719     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9720
9721   if (adjustment)
9722     g_object_ref (adjustment);
9723
9724   g_object_set_qdata_full (G_OBJECT (entry), 
9725                            quark_cursor_hadjustment,
9726                            adjustment, 
9727                            g_object_unref);
9728 }
9729
9730 /**
9731  * gtk_entry_get_cursor_hadjustment:
9732  * @entry: a #GtkEntry
9733  *
9734  * Retrieves the horizontal cursor adjustment for the entry. 
9735  * See gtk_entry_set_cursor_hadjustment().
9736  *
9737  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9738  *   if none has been set.
9739  *
9740  * Since: 2.12
9741  */
9742 GtkAdjustment*
9743 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9744 {
9745   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9746
9747   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9748 }
9749
9750 /**
9751  * gtk_entry_set_progress_fraction:
9752  * @entry: a #GtkEntry
9753  * @fraction: fraction of the task that's been completed
9754  *
9755  * Causes the entry's progress indicator to "fill in" the given
9756  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9757  * inclusive.
9758  *
9759  * Since: 2.16
9760  */
9761 void
9762 gtk_entry_set_progress_fraction (GtkEntry *entry,
9763                                  gdouble   fraction)
9764 {
9765   GtkWidget       *widget;
9766   GtkEntryPrivate *private;
9767   gdouble          old_fraction;
9768   gint x, y, width, height;
9769   gint old_x, old_y, old_width, old_height;
9770
9771   g_return_if_fail (GTK_IS_ENTRY (entry));
9772
9773   widget = GTK_WIDGET (entry);
9774   private = GTK_ENTRY_GET_PRIVATE (entry);
9775
9776   if (private->progress_pulse_mode)
9777     old_fraction = -1;
9778   else
9779     old_fraction = private->progress_fraction;
9780
9781   if (gtk_widget_is_drawable (widget))
9782     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9783
9784   fraction = CLAMP (fraction, 0.0, 1.0);
9785
9786   private->progress_fraction = fraction;
9787   private->progress_pulse_mode = FALSE;
9788   private->progress_pulse_current = 0.0;
9789
9790   if (gtk_widget_is_drawable (widget))
9791     {
9792       get_progress_area (widget, &x, &y, &width, &height);
9793
9794       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9795         gtk_widget_queue_draw (widget);
9796     }
9797
9798   if (fraction != old_fraction)
9799     g_object_notify (G_OBJECT (entry), "progress-fraction");
9800 }
9801
9802 /**
9803  * gtk_entry_get_progress_fraction:
9804  * @entry: a #GtkEntry
9805  *
9806  * Returns the current fraction of the task that's been completed.
9807  * See gtk_entry_set_progress_fraction().
9808  *
9809  * Return value: a fraction from 0.0 to 1.0
9810  *
9811  * Since: 2.16
9812  */
9813 gdouble
9814 gtk_entry_get_progress_fraction (GtkEntry *entry)
9815 {
9816   GtkEntryPrivate *private;
9817
9818   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9819
9820   private = GTK_ENTRY_GET_PRIVATE (entry);
9821
9822   return private->progress_fraction;
9823 }
9824
9825 /**
9826  * gtk_entry_set_progress_pulse_step:
9827  * @entry: a #GtkEntry
9828  * @fraction: fraction between 0.0 and 1.0
9829  *
9830  * Sets the fraction of total entry width to move the progress
9831  * bouncing block for each call to gtk_entry_progress_pulse().
9832  *
9833  * Since: 2.16
9834  */
9835 void
9836 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9837                                    gdouble   fraction)
9838 {
9839   GtkEntryPrivate *private;
9840
9841   g_return_if_fail (GTK_IS_ENTRY (entry));
9842
9843   private = GTK_ENTRY_GET_PRIVATE (entry);
9844
9845   fraction = CLAMP (fraction, 0.0, 1.0);
9846
9847   if (fraction != private->progress_pulse_fraction)
9848     {
9849       private->progress_pulse_fraction = fraction;
9850
9851       gtk_widget_queue_draw (GTK_WIDGET (entry));
9852
9853       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
9854     }
9855 }
9856
9857 /**
9858  * gtk_entry_get_progress_pulse_step:
9859  * @entry: a #GtkEntry
9860  *
9861  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
9862  *
9863  * Return value: a fraction from 0.0 to 1.0
9864  *
9865  * Since: 2.16
9866  */
9867 gdouble
9868 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
9869 {
9870   GtkEntryPrivate *private;
9871
9872   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9873
9874   private = GTK_ENTRY_GET_PRIVATE (entry);
9875
9876   return private->progress_pulse_fraction;
9877 }
9878
9879 /**
9880  * gtk_entry_progress_pulse:
9881  * @entry: a #GtkEntry
9882  *
9883  * Indicates that some progress is made, but you don't know how much.
9884  * Causes the entry's progress indicator to enter "activity mode,"
9885  * where a block bounces back and forth. Each call to
9886  * gtk_entry_progress_pulse() causes the block to move by a little bit
9887  * (the amount of movement per pulse is determined by
9888  * gtk_entry_set_progress_pulse_step()).
9889  *
9890  * Since: 2.16
9891  */
9892 void
9893 gtk_entry_progress_pulse (GtkEntry *entry)
9894 {
9895   GtkEntryPrivate *private;
9896
9897   g_return_if_fail (GTK_IS_ENTRY (entry));
9898
9899   private = GTK_ENTRY_GET_PRIVATE (entry);
9900
9901   if (private->progress_pulse_mode)
9902     {
9903       if (private->progress_pulse_way_back)
9904         {
9905           private->progress_pulse_current -= private->progress_pulse_fraction;
9906
9907           if (private->progress_pulse_current < 0.0)
9908             {
9909               private->progress_pulse_current = 0.0;
9910               private->progress_pulse_way_back = FALSE;
9911             }
9912         }
9913       else
9914         {
9915           private->progress_pulse_current += private->progress_pulse_fraction;
9916
9917           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
9918             {
9919               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
9920               private->progress_pulse_way_back = TRUE;
9921             }
9922         }
9923     }
9924   else
9925     {
9926       private->progress_fraction = 0.0;
9927       private->progress_pulse_mode = TRUE;
9928       private->progress_pulse_way_back = FALSE;
9929       private->progress_pulse_current = 0.0;
9930     }
9931
9932   gtk_widget_queue_draw (GTK_WIDGET (entry));
9933 }
9934
9935 /* Caps Lock warning for password entries */
9936
9937 static void
9938 show_capslock_feedback (GtkEntry    *entry,
9939                         const gchar *text)
9940 {
9941   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9942
9943   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
9944     {
9945       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
9946       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
9947       priv->caps_lock_warning_shown = TRUE;
9948     }
9949
9950   if (priv->caps_lock_warning_shown)
9951     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
9952   else
9953     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
9954 }
9955
9956 static void
9957 remove_capslock_feedback (GtkEntry *entry)
9958 {
9959   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9960
9961   if (priv->caps_lock_warning_shown)
9962     {
9963       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
9964       priv->caps_lock_warning_shown = FALSE;
9965     } 
9966 }
9967
9968 static void
9969 keymap_state_changed (GdkKeymap *keymap, 
9970                       GtkEntry  *entry)
9971 {
9972   GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9973   char *text = NULL;
9974
9975   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
9976     { 
9977       if (gdk_keymap_get_num_lock_state (keymap)
9978           && gdk_keymap_get_caps_lock_state (keymap))
9979         text = _("Caps Lock and Num Lock are on");
9980       else if (gdk_keymap_get_num_lock_state (keymap))
9981         text = _("Num Lock is on");
9982       else if (gdk_keymap_get_caps_lock_state (keymap))
9983         text = _("Caps Lock is on");
9984     }
9985
9986   if (text)
9987     show_capslock_feedback (entry, text);
9988   else
9989     remove_capslock_feedback (entry);
9990 }