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