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