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