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