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