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