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