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