]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
Desensitize "Select All" when GtkEntry is empty
[~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   GdkKeymap *keymap;
2583
2584   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2585   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2586   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2587   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2588
2589   if (priv->buffer)
2590     {
2591       buffer_disconnect_signals (entry);
2592       g_object_unref (priv->buffer);
2593       priv->buffer = NULL;
2594     }
2595
2596   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (object)));
2597   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
2598   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
2599   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2600 }
2601
2602 static void
2603 gtk_entry_finalize (GObject *object)
2604 {
2605   GtkEntry *entry = GTK_ENTRY (object);
2606   GtkEntryPrivate *priv = entry->priv;
2607   EntryIconInfo *icon_info = NULL;
2608   gint i;
2609
2610   for (i = 0; i < MAX_ICONS; i++)
2611     {
2612       if ((icon_info = priv->icons[i]) != NULL)
2613         {
2614           if (icon_info->target_list != NULL)
2615             {
2616               gtk_target_list_unref (icon_info->target_list);
2617               icon_info->target_list = NULL;
2618             }
2619
2620           g_slice_free (EntryIconInfo, icon_info);
2621           priv->icons[i] = NULL;
2622         }
2623     }
2624
2625   gtk_entry_set_completion (entry, NULL);
2626
2627   if (priv->cached_layout)
2628     g_object_unref (priv->cached_layout);
2629
2630   g_object_unref (priv->im_context);
2631
2632   if (priv->blink_timeout)
2633     g_source_remove (priv->blink_timeout);
2634
2635   if (priv->recompute_idle)
2636     g_source_remove (priv->recompute_idle);
2637
2638   g_free (priv->placeholder_text);
2639   g_free (priv->im_module);
2640
2641   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2642 }
2643
2644 static DisplayMode
2645 gtk_entry_get_display_mode (GtkEntry *entry)
2646 {
2647   GtkEntryPrivate *priv = entry->priv;
2648
2649   if (priv->visible)
2650     return DISPLAY_NORMAL;
2651
2652   if (priv->invisible_char == 0 && priv->invisible_char_set)
2653     return DISPLAY_BLANK;
2654
2655   return DISPLAY_INVISIBLE;
2656 }
2657
2658 static gchar*
2659 gtk_entry_get_display_text (GtkEntry *entry,
2660                             gint      start_pos,
2661                             gint      end_pos)
2662 {
2663   GtkEntryPasswordHint *password_hint;
2664   GtkEntryPrivate *priv;
2665   gunichar invisible_char;
2666   const gchar *start;
2667   const gchar *end;
2668   const gchar *text;
2669   gchar char_str[7];
2670   gint char_len;
2671   GString *str;
2672   guint length;
2673   gint i;
2674
2675   priv = entry->priv;
2676   text = gtk_entry_buffer_get_text (get_buffer (entry));
2677   length = gtk_entry_buffer_get_length (get_buffer (entry));
2678
2679   if (end_pos < 0)
2680     end_pos = length;
2681   if (start_pos > length)
2682     start_pos = length;
2683
2684   if (end_pos <= start_pos)
2685       return g_strdup ("");
2686   else if (priv->visible)
2687     {
2688       start = g_utf8_offset_to_pointer (text, start_pos);
2689       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2690       return g_strndup (start, end - start);
2691     }
2692   else
2693     {
2694       str = g_string_sized_new (length * 2);
2695
2696       /* Figure out what our invisible char is and encode it */
2697       if (!priv->invisible_char)
2698           invisible_char = priv->invisible_char_set ? ' ' : '*';
2699       else
2700           invisible_char = priv->invisible_char;
2701       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2702
2703       /*
2704        * Add hidden characters for each character in the text
2705        * buffer. If there is a password hint, then keep that
2706        * character visible.
2707        */
2708
2709       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2710       for (i = start_pos; i < end_pos; ++i)
2711         {
2712           if (password_hint && i == password_hint->position)
2713             {
2714               start = g_utf8_offset_to_pointer (text, i);
2715               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2716             }
2717           else
2718             {
2719               g_string_append_len (str, char_str, char_len);
2720             }
2721         }
2722
2723       return g_string_free (str, FALSE);
2724     }
2725 }
2726
2727 static void
2728 update_cursors (GtkWidget *widget)
2729 {
2730   GtkEntry *entry = GTK_ENTRY (widget);
2731   GtkEntryPrivate *priv = entry->priv;
2732   EntryIconInfo *icon_info = NULL;
2733   GdkDisplay *display;
2734   GdkCursor *cursor;
2735   gint i;
2736
2737   for (i = 0; i < MAX_ICONS; i++)
2738     {
2739       if ((icon_info = priv->icons[i]) != NULL)
2740         {
2741           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2742             gdk_window_show_unraised (icon_info->window);
2743
2744           /* The icon windows are not children of the visible entry window,
2745            * thus we can't just inherit the xterm cursor. Slight complication 
2746            * here is that for the entry, insensitive => arrow cursor, but for 
2747            * an icon in a sensitive entry, insensitive => xterm cursor.
2748            */
2749           if (gtk_widget_is_sensitive (widget) &&
2750               (icon_info->insensitive || 
2751                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2752             {
2753               display = gtk_widget_get_display (widget);
2754               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2755               gdk_window_set_cursor (icon_info->window, cursor);
2756               g_object_unref (cursor);
2757             }
2758           else
2759             {
2760               gdk_window_set_cursor (icon_info->window, NULL);
2761             }
2762         }
2763     }
2764 }
2765
2766 static void
2767 realize_icon_info (GtkWidget            *widget, 
2768                    GtkEntryIconPosition  icon_pos)
2769 {
2770   GtkEntry *entry = GTK_ENTRY (widget);
2771   GtkEntryPrivate *priv = entry->priv;
2772   EntryIconInfo *icon_info = priv->icons[icon_pos];
2773   GdkWindowAttr attributes;
2774   gint attributes_mask;
2775
2776   g_return_if_fail (icon_info != NULL);
2777
2778   attributes.x = 0;
2779   attributes.y = 0;
2780   attributes.width = 1;
2781   attributes.height = 1;
2782   attributes.window_type = GDK_WINDOW_CHILD;
2783   attributes.wclass = GDK_INPUT_ONLY;
2784   attributes.event_mask = gtk_widget_get_events (widget);
2785   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2786                                 GDK_BUTTON_RELEASE_MASK |
2787                                 GDK_BUTTON1_MOTION_MASK |
2788                                 GDK_BUTTON3_MOTION_MASK |
2789                                 GDK_POINTER_MOTION_HINT_MASK |
2790                                 GDK_POINTER_MOTION_MASK |
2791                                 GDK_ENTER_NOTIFY_MASK |
2792                             GDK_LEAVE_NOTIFY_MASK);
2793   attributes_mask = GDK_WA_X | GDK_WA_Y;
2794
2795   icon_info->window = gdk_window_new (gtk_widget_get_window (widget),
2796                                       &attributes,
2797                                       attributes_mask);
2798   gdk_window_set_user_data (icon_info->window, widget);
2799
2800   gtk_widget_queue_resize (widget);
2801 }
2802
2803 static EntryIconInfo*
2804 construct_icon_info (GtkWidget            *widget, 
2805                      GtkEntryIconPosition  icon_pos)
2806 {
2807   GtkEntry *entry = GTK_ENTRY (widget);
2808   GtkEntryPrivate *priv = entry->priv;
2809   EntryIconInfo *icon_info;
2810
2811   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2812
2813   icon_info = g_slice_new0 (EntryIconInfo);
2814   priv->icons[icon_pos] = icon_info;
2815
2816   if (gtk_widget_get_realized (widget))
2817     realize_icon_info (widget, icon_pos);
2818
2819   return icon_info;
2820 }
2821
2822 static void
2823 gtk_entry_map (GtkWidget *widget)
2824 {
2825   GtkEntry *entry = GTK_ENTRY (widget);
2826   GtkEntryPrivate *priv = entry->priv;
2827   EntryIconInfo *icon_info = NULL;
2828   gint i;
2829
2830   GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2831
2832   gdk_window_show (priv->text_area);
2833
2834   for (i = 0; i < MAX_ICONS; i++)
2835     {
2836       if ((icon_info = priv->icons[i]) != NULL)
2837         {
2838           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2839             gdk_window_show (icon_info->window);
2840         }
2841     }
2842
2843   update_cursors (widget);
2844 }
2845
2846 static void
2847 gtk_entry_unmap (GtkWidget *widget)
2848 {
2849   GtkEntry *entry = GTK_ENTRY (widget);
2850   GtkEntryPrivate *priv = entry->priv;
2851   EntryIconInfo *icon_info = NULL;
2852   gint i;
2853
2854   for (i = 0; i < MAX_ICONS; i++)
2855     {
2856       if ((icon_info = priv->icons[i]) != NULL)
2857         {
2858           if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2859             gdk_window_hide (icon_info->window);
2860         }
2861     }
2862
2863   gdk_window_hide (priv->text_area);
2864
2865   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2866 }
2867
2868 static void
2869 gtk_entry_realize (GtkWidget *widget)
2870 {
2871   GtkEntry *entry;
2872   GtkEntryPrivate *priv;
2873   EntryIconInfo *icon_info;
2874   GdkWindow *window;
2875   GdkWindowAttr attributes;
2876   gint attributes_mask;
2877   gint frame_x, frame_y;
2878   int i;
2879
2880   gtk_widget_set_realized (widget, TRUE);
2881   window = gtk_widget_get_parent_window (widget);
2882   gtk_widget_set_window (widget, window);
2883   g_object_ref (window);
2884
2885   entry = GTK_ENTRY (widget);
2886   priv = entry->priv;
2887
2888   attributes.window_type = GDK_WINDOW_CHILD;
2889   attributes.wclass = GDK_INPUT_ONLY;
2890   attributes.event_mask = gtk_widget_get_events (widget);
2891   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2892                             GDK_BUTTON_RELEASE_MASK |
2893                             GDK_BUTTON1_MOTION_MASK |
2894                             GDK_BUTTON3_MOTION_MASK |
2895                             GDK_POINTER_MOTION_HINT_MASK |
2896                             GDK_POINTER_MOTION_MASK |
2897                             GDK_ENTER_NOTIFY_MASK |
2898                             GDK_LEAVE_NOTIFY_MASK);
2899   attributes_mask = GDK_WA_X | GDK_WA_Y;
2900
2901   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2902
2903   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
2904   attributes.x += frame_x;
2905   attributes.y += frame_y;
2906
2907   if (gtk_widget_is_sensitive (widget))
2908     {
2909       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2910       attributes_mask |= GDK_WA_CURSOR;
2911     }
2912
2913   priv->text_area = gdk_window_new (gtk_widget_get_window (widget),
2914                                     &attributes,
2915                                     attributes_mask);
2916
2917   gdk_window_set_user_data (priv->text_area, entry);
2918
2919   if (attributes_mask & GDK_WA_CURSOR)
2920     g_object_unref (attributes.cursor);
2921
2922   gtk_im_context_set_client_window (priv->im_context, priv->text_area);
2923
2924   gtk_entry_adjust_scroll (entry);
2925   gtk_entry_update_primary_selection (entry);
2926
2927
2928   /* If the icon positions are already setup, create their windows.
2929    * Otherwise if they don't exist yet, then construct_icon_info()
2930    * will create the windows once the widget is already realized.
2931    */
2932   for (i = 0; i < MAX_ICONS; i++)
2933     {
2934       if ((icon_info = priv->icons[i]) != NULL)
2935         {
2936           if (icon_info->window == NULL)
2937             realize_icon_info (widget, i);
2938         }
2939     }
2940 }
2941
2942 static void
2943 gtk_entry_unrealize (GtkWidget *widget)
2944 {
2945   GtkEntry *entry = GTK_ENTRY (widget);
2946   GtkEntryPrivate *priv = entry->priv;
2947   GtkClipboard *clipboard;
2948   EntryIconInfo *icon_info;
2949   gint i;
2950
2951   gtk_entry_reset_layout (entry);
2952   
2953   gtk_im_context_set_client_window (priv->im_context, NULL);
2954
2955   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2956   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2957     gtk_clipboard_clear (clipboard);
2958   
2959   if (priv->text_area)
2960     {
2961       gdk_window_set_user_data (priv->text_area, NULL);
2962       gdk_window_destroy (priv->text_area);
2963       priv->text_area = NULL;
2964     }
2965
2966   if (priv->popup_menu)
2967     {
2968       gtk_widget_destroy (priv->popup_menu);
2969       priv->popup_menu = NULL;
2970     }
2971
2972   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
2973
2974   for (i = 0; i < MAX_ICONS; i++)
2975     {
2976       if ((icon_info = priv->icons[i]) != NULL)
2977         {
2978           if (icon_info->window != NULL)
2979             {
2980               gdk_window_destroy (icon_info->window);
2981               icon_info->window = NULL;
2982             }
2983         }
2984     }
2985 }
2986
2987 void
2988 _gtk_entry_get_borders (GtkEntry *entry,
2989                         gint     *xborder,
2990                         gint     *yborder)
2991 {
2992   GtkEntryPrivate *priv = entry->priv;
2993   GtkWidget *widget = GTK_WIDGET (entry);
2994
2995   if (priv->has_frame)
2996     {
2997       GtkStyleContext *context;
2998       GtkBorder padding;
2999
3000       context = gtk_widget_get_style_context (widget);
3001       gtk_style_context_get_padding (context, 0, &padding);
3002
3003       *xborder = padding.left;
3004       *yborder = padding.top;
3005     }
3006   else
3007     {
3008       *xborder = 0;
3009       *yborder = 0;
3010     }
3011
3012   if (!priv->interior_focus)
3013     {
3014       *xborder += priv->focus_width;
3015       *yborder += priv->focus_width;
3016     }
3017 }
3018
3019 static void
3020 gtk_entry_get_preferred_width (GtkWidget *widget,
3021                                gint      *minimum,
3022                                gint      *natural)
3023 {
3024   GtkEntry *entry = GTK_ENTRY (widget);
3025   GtkEntryPrivate *priv = entry->priv;
3026   PangoFontMetrics *metrics;
3027   gint xborder, yborder;
3028   GtkBorder inner_border;
3029   PangoContext *context;
3030   GtkStyleContext *style_context;
3031   GtkStateFlags state;
3032   gint icon_widths = 0;
3033   gint icon_width, i;
3034   gint width;
3035
3036   context = gtk_widget_get_pango_context (widget);
3037
3038   style_context = gtk_widget_get_style_context (widget);
3039   state = gtk_widget_get_state_flags (widget);
3040
3041   metrics = pango_context_get_metrics (context,
3042                                        gtk_style_context_get_font (style_context, state),
3043                                        pango_context_get_language (context));
3044
3045   _gtk_entry_get_borders (entry, &xborder, &yborder);
3046   _gtk_entry_effective_inner_border (entry, &inner_border);
3047
3048   if (priv->width_chars < 0)
3049     width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
3050   else
3051     {
3052       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
3053       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3054       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
3055
3056       width = char_pixels * priv->width_chars + xborder * 2 + inner_border.left + inner_border.right;
3057     }
3058
3059   for (i = 0; i < MAX_ICONS; i++)
3060     {
3061       icon_width = get_icon_width (entry, i);
3062       if (icon_width > 0)
3063         icon_widths += icon_width + 2 * priv->icon_margin;
3064     }
3065
3066   if (icon_widths > width)
3067     width += icon_widths;
3068
3069   pango_font_metrics_unref (metrics);
3070
3071   *minimum = width;
3072   *natural = width;
3073 }
3074
3075 static void
3076 gtk_entry_get_preferred_height (GtkWidget *widget,
3077                                 gint      *minimum,
3078                                 gint      *natural)
3079 {
3080   GtkEntry *entry = GTK_ENTRY (widget);
3081   GtkEntryPrivate *priv = entry->priv;
3082   PangoFontMetrics *metrics;
3083   gint xborder, yborder;
3084   GtkBorder inner_border;
3085   GtkStyleContext *style_context;
3086   GtkStateFlags state;
3087   PangoContext *context;
3088   gint height;
3089
3090   context = gtk_widget_get_pango_context (widget);
3091
3092   style_context = gtk_widget_get_style_context (widget);
3093   state = gtk_widget_get_state_flags (widget);
3094
3095   metrics = pango_context_get_metrics (context,
3096                                        gtk_style_context_get_font (style_context, state),
3097                                        pango_context_get_language (context));
3098
3099   priv->ascent = pango_font_metrics_get_ascent (metrics);
3100   priv->descent = pango_font_metrics_get_descent (metrics);
3101
3102   _gtk_entry_get_borders (entry, &xborder, &yborder);
3103   _gtk_entry_effective_inner_border (entry, &inner_border);
3104
3105   height = PANGO_PIXELS (priv->ascent + priv->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
3106
3107   pango_font_metrics_unref (metrics);
3108
3109   *minimum = height;
3110   *natural = height;
3111 }
3112
3113 static void
3114 place_windows (GtkEntry *entry)
3115 {
3116   GtkWidget *widget = GTK_WIDGET (entry);
3117   GtkEntryPrivate *priv = entry->priv;
3118   gint x, y, width, height;
3119   gint frame_x, frame_y;
3120   GtkAllocation primary;
3121   GtkAllocation secondary;
3122   EntryIconInfo *icon_info = NULL;
3123
3124   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
3125   get_text_area_size (entry, &x, &y, &width, &height);
3126   get_icon_allocations (entry, &primary, &secondary);
3127
3128   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3129     y += priv->focus_width;
3130
3131   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3132     x += secondary.width;
3133   else
3134     x += primary.width;
3135   width -= primary.width + secondary.width;
3136
3137   x += frame_x;
3138   y += frame_y;
3139   primary.x += frame_x;
3140   primary.y += frame_y;
3141   secondary.x += frame_x;
3142   secondary.y += frame_y;
3143
3144   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
3145     gdk_window_move_resize (icon_info->window,
3146                             primary.x, primary.y,
3147                             primary.width, primary.height);
3148
3149   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
3150     gdk_window_move_resize (icon_info->window,
3151                             secondary.x, secondary.y,
3152                             secondary.width, secondary.height);
3153
3154   gdk_window_move_resize (priv->text_area, x, y, width, height);
3155 }
3156
3157 static void
3158 gtk_entry_get_text_area_size (GtkEntry *entry,
3159                               gint     *x,
3160                               gint     *y,
3161                               gint     *width,
3162                               gint     *height)
3163 {
3164   GtkEntryPrivate *priv = entry->priv;
3165   GtkWidget *widget = GTK_WIDGET (entry);
3166   GtkAllocation allocation;
3167   GtkRequisition requisition;
3168   gint frame_height;
3169   gint xborder, yborder;
3170
3171   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3172   gtk_widget_get_allocation (widget, &allocation);
3173   _gtk_entry_get_borders (entry, &xborder, &yborder);
3174
3175   if (gtk_widget_get_realized (widget))
3176     get_frame_size (entry, TRUE, NULL, NULL, NULL, &frame_height);
3177   else
3178     frame_height = requisition.height;
3179
3180   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3181     frame_height -= 2 * priv->focus_width;
3182
3183   if (x)
3184     *x = xborder;
3185
3186   if (y)
3187     *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
3188
3189   if (width)
3190     *width = allocation.width - xborder * 2;
3191
3192   if (height)
3193     *height = requisition.height - yborder * 2;
3194 }
3195
3196 static void
3197 get_text_area_size (GtkEntry *entry,
3198                     gint     *x,
3199                     gint     *y,
3200                     gint     *width,
3201                     gint     *height)
3202 {
3203   GtkEntryClass *class;
3204
3205   g_return_if_fail (GTK_IS_ENTRY (entry));
3206
3207   class = GTK_ENTRY_GET_CLASS (entry);
3208
3209   if (class->get_text_area_size)
3210     class->get_text_area_size (entry, x, y, width, height);
3211 }
3212
3213
3214 static void
3215 get_frame_size (GtkEntry *entry,
3216                 gboolean  relative_to_window,
3217                 gint     *x,
3218                 gint     *y,
3219                 gint     *width,
3220                 gint     *height)
3221 {
3222   GtkEntryPrivate *priv = entry->priv;
3223   GtkAllocation allocation;
3224   GtkRequisition requisition;
3225   GtkWidget *widget = GTK_WIDGET (entry);
3226
3227   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3228   gtk_widget_get_allocation (widget, &allocation);
3229
3230   if (x)
3231     *x = relative_to_window ? allocation.x : 0;
3232
3233   if (y)
3234     {
3235       if (priv->is_cell_renderer)
3236         *y = 0;
3237       else
3238         *y = (allocation.height - requisition.height) / 2;
3239
3240       if (relative_to_window)
3241         *y += allocation.y;
3242     }
3243
3244   if (width)
3245     *width = allocation.width;
3246
3247   if (height)
3248     {
3249       if (priv->is_cell_renderer)
3250         *height = allocation.height;
3251       else
3252         *height = requisition.height;
3253     }
3254 }
3255
3256 void
3257 _gtk_entry_effective_inner_border (GtkEntry  *entry,
3258                                    GtkBorder *border)
3259 {
3260   GtkBorder *tmp_border;
3261
3262   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
3263
3264   if (tmp_border)
3265     {
3266       *border = *tmp_border;
3267       return;
3268     }
3269
3270   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
3271
3272   if (tmp_border)
3273     {
3274       *border = *tmp_border;
3275       gtk_border_free (tmp_border);
3276       return;
3277     }
3278
3279   *border = default_inner_border;
3280 }
3281
3282 static void
3283 gtk_entry_size_allocate (GtkWidget     *widget,
3284                          GtkAllocation *allocation)
3285 {
3286   GtkEntry *entry = GTK_ENTRY (widget);
3287
3288   gtk_widget_set_allocation (widget, allocation);
3289
3290   if (gtk_widget_get_realized (widget))
3291     {
3292       GtkEntryCompletion* completion;
3293
3294       place_windows (entry);
3295       gtk_entry_recompute (entry);
3296
3297       completion = gtk_entry_get_completion (entry);
3298       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3299         _gtk_entry_completion_resize_popup (completion);
3300     }
3301 }
3302
3303 static gboolean
3304 should_prelight (GtkEntry             *entry,
3305                  GtkEntryIconPosition  icon_pos)
3306 {
3307   GtkEntryPrivate *priv = entry->priv;
3308   EntryIconInfo *icon_info = priv->icons[icon_pos];
3309   gboolean prelight;
3310
3311   if (!icon_info)
3312     return FALSE;
3313
3314   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3315     return FALSE;
3316
3317   if (icon_info->pressed)
3318     return FALSE;
3319
3320   gtk_widget_style_get (GTK_WIDGET (entry),
3321                         "icon-prelight", &prelight,
3322                         NULL);
3323
3324   return prelight;
3325 }
3326
3327 static void
3328 draw_icon (GtkWidget            *widget,
3329            cairo_t              *cr,
3330            GtkEntryIconPosition  icon_pos)
3331 {
3332   GtkEntry *entry = GTK_ENTRY (widget);
3333   GtkEntryPrivate *priv = entry->priv;
3334   EntryIconInfo *icon_info = priv->icons[icon_pos];
3335   GdkPixbuf *pixbuf;
3336   gint x, y, width, height;
3337   GtkStyleContext *context;
3338   GtkIconSource *icon_source;
3339   GtkStateFlags state;
3340
3341   if (!icon_info)
3342     return;
3343
3344   gtk_entry_ensure_pixbuf (entry, icon_pos);
3345
3346   if (icon_info->pixbuf == NULL)
3347     return;
3348
3349   width = gdk_window_get_width (icon_info->window);
3350   height = gdk_window_get_height (icon_info->window);
3351
3352   /* size_allocate hasn't been called yet. These are the default values.
3353    */
3354   if (width == 1 || height == 1)
3355     return;
3356
3357   pixbuf = icon_info->pixbuf;
3358   g_object_ref (pixbuf);
3359
3360   if (gdk_pixbuf_get_height (pixbuf) > height)
3361     {
3362       GdkPixbuf *temp_pixbuf;
3363       gint scale;
3364
3365       scale = height - 2 * priv->icon_margin;
3366       temp_pixbuf = gdk_pixbuf_scale_simple (pixbuf, scale, scale,
3367                                              GDK_INTERP_BILINEAR);
3368       g_object_unref (pixbuf);
3369       pixbuf = temp_pixbuf;
3370     }
3371
3372   x = (width  - gdk_pixbuf_get_width (pixbuf)) / 2;
3373   y = (height - gdk_pixbuf_get_height (pixbuf)) / 2;
3374
3375   icon_source = gtk_icon_source_new ();
3376   gtk_icon_source_set_pixbuf (icon_source, pixbuf);
3377   gtk_icon_source_set_state_wildcarded (icon_source, TRUE);
3378
3379   state = 0;
3380   if (!gtk_widget_is_sensitive (widget) || icon_info->insensitive)
3381     state |= GTK_STATE_FLAG_INSENSITIVE;
3382   else if (icon_info->prelight)
3383     state |= GTK_STATE_FLAG_PRELIGHT;
3384
3385   context = gtk_widget_get_style_context (widget);
3386   gtk_style_context_save (context);
3387   gtk_style_context_set_state (context, state);
3388   pixbuf = gtk_render_icon_pixbuf (context, icon_source, (GtkIconSize)-1);
3389   gtk_style_context_restore (context);
3390
3391   gtk_icon_source_free (icon_source);
3392
3393   gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
3394   cairo_paint (cr);
3395
3396   g_object_unref (pixbuf);
3397 }
3398
3399
3400 static void
3401 gtk_entry_draw_frame (GtkWidget       *widget,
3402                       GtkStyleContext *context,
3403                       cairo_t         *cr)
3404 {
3405   GtkEntry *entry = GTK_ENTRY (widget);
3406   GtkEntryPrivate *priv = entry->priv;
3407   gint x = 0, y = 0, width, height;
3408   gint frame_x, frame_y;
3409
3410   cairo_save (cr);
3411
3412   get_frame_size (GTK_ENTRY (widget), FALSE, &frame_x, &frame_y, &width, &height);
3413
3414   cairo_translate (cr, frame_x, frame_y);
3415
3416   /* Fix a problem with some themes which assume that entry->text_area's
3417    * width equals widget->window's width
3418    * http://bugzilla.gnome.org/show_bug.cgi?id=466000 */
3419   if (GTK_IS_SPIN_BUTTON (widget))
3420     {
3421       gint xborder, yborder;
3422
3423       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3424       _gtk_entry_get_borders (GTK_ENTRY (widget), &xborder, &yborder);
3425
3426       x -= xborder;
3427       width += xborder * 2;
3428     }
3429
3430   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3431     {
3432       x += priv->focus_width;
3433       y += priv->focus_width;
3434       width -= 2 * priv->focus_width;
3435       height -= 2 * priv->focus_width;
3436     }
3437
3438   gtk_render_background (context, cr,
3439                          x, y, width, height);
3440
3441   if (priv->has_frame)
3442     gtk_render_frame (context, cr,
3443                       x, y, width, height);
3444
3445   gtk_entry_draw_progress (widget, context, cr);
3446
3447   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3448     {
3449       x -= priv->focus_width;
3450       y -= priv->focus_width;
3451       width += 2 * priv->focus_width;
3452       height += 2 * priv->focus_width;
3453
3454       gtk_render_focus (context, cr,
3455                         0, 0, width, height);
3456     }
3457
3458   cairo_restore (cr);
3459 }
3460
3461 static void
3462 get_progress_area (GtkWidget *widget,
3463                    gint       *x,
3464                    gint       *y,
3465                    gint       *width,
3466                    gint       *height)
3467 {
3468   GtkEntry *entry = GTK_ENTRY (widget);
3469   GtkEntryPrivate *private = entry->priv;
3470   GtkBorder *progress_border;
3471
3472   get_text_area_size (entry, x, y, width, height);
3473
3474   if (!private->interior_focus)
3475     {
3476       *x -= private->focus_width;
3477       *y -= private->focus_width;
3478       *width += 2 * private->focus_width;
3479       *height += 2 * private->focus_width;
3480     }
3481
3482   gtk_widget_style_get (widget, "progress-border", &progress_border, NULL);
3483
3484   if (progress_border)
3485     {
3486       *x += progress_border->left;
3487       *y += progress_border->top;
3488       *width -= progress_border->left + progress_border->right;
3489       *height -= progress_border->top + progress_border->bottom;
3490
3491       gtk_border_free (progress_border);
3492     }
3493
3494   if (private->progress_pulse_mode)
3495     {
3496       gdouble value = private->progress_pulse_current;
3497
3498       *x += (gint) floor(value * (*width));
3499       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3500     }
3501   else if (private->progress_fraction > 0)
3502     {
3503       gdouble value = private->progress_fraction;
3504
3505       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3506         {
3507           gint bar_width;
3508
3509           bar_width = floor(value * (*width) + 0.5);
3510           *x += *width - bar_width;
3511           *width = bar_width;
3512         }
3513       else
3514         {
3515           *width = (gint) floor(value * (*width) + 0.5);
3516         }
3517     }
3518   else
3519     {
3520       *width = 0;
3521       *height = 0;
3522     }
3523 }
3524
3525 static void
3526 gtk_entry_draw_progress (GtkWidget       *widget,
3527                          GtkStyleContext *context,
3528                          cairo_t         *cr)
3529 {
3530   gint x, y, width, height;
3531
3532   get_progress_area (widget, &x, &y, &width, &height);
3533
3534   if ((width <= 0) || (height <= 0))
3535     return;
3536
3537   gtk_style_context_save (context);
3538   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
3539
3540   gtk_render_activity (context, cr,
3541                        x, y, width, height);
3542
3543   gtk_style_context_restore (context);
3544 }
3545
3546 static gint
3547 gtk_entry_draw (GtkWidget *widget,
3548                 cairo_t   *cr)
3549 {
3550   GtkEntry *entry = GTK_ENTRY (widget);
3551   GtkStyleContext *context;
3552   GtkStateFlags state;
3553   GtkEntryPrivate *priv = entry->priv;
3554   int i;
3555
3556   context = gtk_widget_get_style_context (widget);
3557   state = gtk_widget_get_state_flags (widget);
3558
3559   gtk_style_context_save (context);
3560   gtk_style_context_set_state (context, state);
3561
3562   /* Draw entry_bg, shadow, progress and focus */
3563   gtk_entry_draw_frame (widget, context, cr);
3564
3565   /* Draw text and cursor */
3566   cairo_save (cr);
3567
3568   gtk_cairo_transform_to_window (cr, widget, priv->text_area);
3569
3570   if (priv->dnd_position != -1)
3571     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3572   
3573   gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3574
3575   /* When no text is being displayed at all, don't show the cursor */
3576   if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3577       gtk_widget_has_focus (widget) &&
3578       priv->selection_bound == priv->current_pos && priv->cursor_visible)
3579     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3580
3581   cairo_restore (cr);
3582
3583   /* Draw icons */
3584   for (i = 0; i < MAX_ICONS; i++)
3585     {
3586       EntryIconInfo *icon_info = priv->icons[i];
3587
3588       if (icon_info != NULL)
3589         {
3590           cairo_save (cr);
3591
3592           gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3593
3594           draw_icon (widget, cr, i);
3595
3596           cairo_restore (cr);
3597         }
3598     }
3599
3600   gtk_style_context_restore (context);
3601
3602   return FALSE;
3603 }
3604
3605 static gint
3606 gtk_entry_enter_notify (GtkWidget *widget,
3607                         GdkEventCrossing *event)
3608 {
3609   GtkEntry *entry = GTK_ENTRY (widget);
3610   GtkEntryPrivate *priv = entry->priv;
3611   gint i;
3612
3613   for (i = 0; i < MAX_ICONS; i++)
3614     {
3615       EntryIconInfo *icon_info = priv->icons[i];
3616
3617       if (icon_info != NULL && event->window == icon_info->window)
3618         {
3619           if (should_prelight (entry, i))
3620             {
3621               icon_info->prelight = TRUE;
3622               gtk_widget_queue_draw (widget);
3623             }
3624
3625           break;
3626         }
3627     }
3628
3629     return FALSE;
3630 }
3631
3632 static gint
3633 gtk_entry_leave_notify (GtkWidget        *widget,
3634                         GdkEventCrossing *event)
3635 {
3636   GtkEntry *entry = GTK_ENTRY (widget);
3637   GtkEntryPrivate *priv = entry->priv;
3638   gint i;
3639
3640   for (i = 0; i < MAX_ICONS; i++)
3641     {
3642       EntryIconInfo *icon_info = priv->icons[i];
3643
3644       if (icon_info != NULL && event->window == icon_info->window)
3645         {
3646           /* a grab means that we may never see the button release */
3647           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3648             icon_info->pressed = FALSE;
3649
3650           if (should_prelight (entry, i))
3651             {
3652               icon_info->prelight = FALSE;
3653               gtk_widget_queue_draw (widget);
3654             }
3655
3656           break;
3657         }
3658     }
3659
3660   return FALSE;
3661 }
3662
3663 static void
3664 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3665                             gint     **ranges,
3666                             gint      *n_ranges)
3667 {
3668   gint start_char, end_char;
3669
3670   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3671     {
3672       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3673       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3674       const char *text = pango_layout_get_text (layout);
3675       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3676       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3677       gint real_n_ranges, i;
3678
3679       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3680
3681       if (ranges)
3682         {
3683           gint *r = *ranges;
3684           
3685           for (i = 0; i < real_n_ranges; ++i)
3686             {
3687               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3688               r[2 * i] = r[2 * i] / PANGO_SCALE;
3689             }
3690         }
3691       
3692       if (n_ranges)
3693         *n_ranges = real_n_ranges;
3694     }
3695   else
3696     {
3697       if (n_ranges)
3698         *n_ranges = 0;
3699       if (ranges)
3700         *ranges = NULL;
3701     }
3702 }
3703
3704 static gboolean
3705 in_selection (GtkEntry *entry,
3706               gint      x)
3707 {
3708   gint *ranges;
3709   gint n_ranges, i;
3710   gint retval = FALSE;
3711
3712   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3713
3714   for (i = 0; i < n_ranges; ++i)
3715     {
3716       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3717         {
3718           retval = TRUE;
3719           break;
3720         }
3721     }
3722
3723   g_free (ranges);
3724   return retval;
3725 }
3726               
3727 static gint
3728 gtk_entry_button_press (GtkWidget      *widget,
3729                         GdkEventButton *event)
3730 {
3731   GtkEntry *entry = GTK_ENTRY (widget);
3732   GtkEditable *editable = GTK_EDITABLE (widget);
3733   GtkEntryPrivate *priv = entry->priv;
3734   EntryIconInfo *icon_info = NULL;
3735   gint tmp_pos;
3736   gint sel_start, sel_end;
3737   gint i;
3738
3739   for (i = 0; i < MAX_ICONS; i++)
3740     {
3741       icon_info = priv->icons[i];
3742
3743       if (!icon_info || icon_info->insensitive)
3744         continue;
3745
3746       if (event->window == icon_info->window)
3747         {
3748           if (should_prelight (entry, i))
3749             {
3750               icon_info->prelight = FALSE;
3751               gtk_widget_queue_draw (widget);
3752             }
3753
3754           priv->start_x = event->x;
3755           priv->start_y = event->y;
3756           icon_info->pressed = TRUE;
3757
3758           if (!icon_info->nonactivatable)
3759             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3760
3761           return TRUE;
3762         }
3763     }
3764
3765   if (event->window != priv->text_area ||
3766       (priv->button && event->button != priv->button))
3767     return FALSE;
3768
3769   gtk_entry_reset_blink_time (entry);
3770
3771   priv->button = event->button;
3772   
3773   if (!gtk_widget_has_focus (widget))
3774     {
3775       priv->in_click = TRUE;
3776       gtk_widget_grab_focus (widget);
3777       priv->in_click = FALSE;
3778     }
3779   
3780   tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
3781     
3782   if (event->button == 1)
3783     {
3784       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3785       
3786       priv->select_words = FALSE;
3787       priv->select_lines = FALSE;
3788
3789       if (event->state & GDK_SHIFT_MASK)
3790         {
3791           _gtk_entry_reset_im_context (entry);
3792           
3793           if (!have_selection) /* select from the current position to the clicked position */
3794             sel_start = sel_end = priv->current_pos;
3795           
3796           if (tmp_pos > sel_start && tmp_pos < sel_end)
3797             {
3798               /* Truncate current selection, but keep it as big as possible */
3799               if (tmp_pos - sel_start > sel_end - tmp_pos)
3800                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3801               else
3802                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3803             }
3804           else
3805             {
3806               gboolean extend_to_left;
3807               gint start, end;
3808
3809               /* Figure out what click selects and extend current selection */
3810               switch (event->type)
3811                 {
3812                 case GDK_BUTTON_PRESS:
3813                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3814                   break;
3815                   
3816                 case GDK_2BUTTON_PRESS:
3817                   priv->select_words = TRUE;
3818                   gtk_entry_select_word (entry);
3819                   break;
3820                   
3821                 case GDK_3BUTTON_PRESS:
3822                   priv->select_lines = TRUE;
3823                   gtk_entry_select_line (entry);
3824                   break;
3825
3826                 default:
3827                   break;
3828                 }
3829
3830               start = MIN (priv->current_pos, priv->selection_bound);
3831               start = MIN (sel_start, start);
3832               
3833               end = MAX (priv->current_pos, priv->selection_bound);
3834               end = MAX (sel_end, end);
3835
3836               if (tmp_pos == sel_start || tmp_pos == sel_end)
3837                 extend_to_left = (tmp_pos == start);
3838               else
3839                 extend_to_left = (end == sel_end);
3840               
3841               if (extend_to_left)
3842                 gtk_entry_set_positions (entry, start, end);
3843               else
3844                 gtk_entry_set_positions (entry, end, start);
3845             }
3846         }
3847       else /* no shift key */
3848         switch (event->type)
3849         {
3850         case GDK_BUTTON_PRESS:
3851           if (in_selection (entry, event->x + priv->scroll_offset))
3852             {
3853               /* Click inside the selection - we'll either start a drag, or
3854                * clear the selection
3855                */
3856               priv->in_drag = TRUE;
3857               priv->drag_start_x = event->x + priv->scroll_offset;
3858               priv->drag_start_y = event->y;
3859             }
3860           else
3861             gtk_editable_set_position (editable, tmp_pos);
3862           break;
3863  
3864         case GDK_2BUTTON_PRESS:
3865           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3866            * receiving a GDK_2BUTTON_PRESS so we need to reset
3867            * priv->in_drag which may have been set above
3868            */
3869           priv->in_drag = FALSE;
3870           priv->select_words = TRUE;
3871           gtk_entry_select_word (entry);
3872           break;
3873         
3874         case GDK_3BUTTON_PRESS:
3875           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3876            * receiving a GDK_3BUTTON_PRESS so we need to reset
3877            * priv->in_drag which may have been set above
3878            */
3879           priv->in_drag = FALSE;
3880           priv->select_lines = TRUE;
3881           gtk_entry_select_line (entry);
3882           break;
3883
3884         default:
3885           break;
3886         }
3887
3888       return TRUE;
3889     }
3890   else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3891     {
3892       if (priv->editable)
3893         {
3894           priv->insert_pos = tmp_pos;
3895           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3896           return TRUE;
3897         }
3898       else
3899         {
3900           gtk_widget_error_bell (widget);
3901         }
3902     }
3903   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3904     {
3905       gtk_entry_do_popup (entry, event);
3906       priv->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3907
3908       return TRUE;
3909     }
3910
3911   return FALSE;
3912 }
3913
3914 static gint
3915 gtk_entry_button_release (GtkWidget      *widget,
3916                           GdkEventButton *event)
3917 {
3918   GtkEntry *entry = GTK_ENTRY (widget);
3919   GtkEntryPrivate *priv = entry->priv;
3920   EntryIconInfo *icon_info = NULL;
3921   gint i;
3922
3923   for (i = 0; i < MAX_ICONS; i++)
3924     {
3925       icon_info = priv->icons[i];
3926
3927       if (!icon_info || icon_info->insensitive)
3928         continue;
3929
3930       if (event->window == icon_info->window)
3931         {
3932           icon_info->pressed = FALSE;
3933
3934           if (should_prelight (entry, i) &&
3935               event->x >= 0 && event->y >= 0 &&
3936               event->x < gdk_window_get_width (icon_info->window) &&
3937               event->y < gdk_window_get_height (icon_info->window))
3938             {
3939               icon_info->prelight = TRUE;
3940               gtk_widget_queue_draw (widget);
3941             }
3942
3943           if (!icon_info->nonactivatable)
3944             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3945
3946           return TRUE;
3947         }
3948     }
3949
3950   if (event->window != priv->text_area || priv->button != event->button)
3951     return FALSE;
3952
3953   if (priv->in_drag)
3954     {
3955       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
3956
3957       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3958
3959       priv->in_drag = 0;
3960     }
3961   
3962   priv->button = 0;
3963   
3964   gtk_entry_update_primary_selection (entry);
3965               
3966   return TRUE;
3967 }
3968
3969 static gchar *
3970 _gtk_entry_get_selected_text (GtkEntry *entry)
3971 {
3972   GtkEditable *editable = GTK_EDITABLE (entry);
3973   gint         start_text, end_text;
3974   gchar       *text = NULL;
3975
3976   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3977     text = gtk_editable_get_chars (editable, start_text, end_text);
3978
3979   return text;
3980 }
3981
3982 static gint
3983 gtk_entry_motion_notify (GtkWidget      *widget,
3984                          GdkEventMotion *event)
3985 {
3986   GtkEntry *entry = GTK_ENTRY (widget);
3987   GtkEntryPrivate *priv = entry->priv;
3988   EntryIconInfo *icon_info = NULL;
3989   gint tmp_pos;
3990   gint i;
3991
3992   for (i = 0; i < MAX_ICONS; i++)
3993     {
3994       icon_info = priv->icons[i];
3995
3996       if (!icon_info || icon_info->insensitive)
3997         continue;
3998
3999       if (event->window == icon_info->window)
4000         {
4001           if (icon_info->pressed &&
4002               icon_info->target_list != NULL &&
4003               gtk_drag_check_threshold (widget,
4004                                         priv->start_x,
4005                                         priv->start_y,
4006                                         event->x,
4007                                         event->y))
4008             {
4009               icon_info->in_drag = TRUE;
4010               icon_info->pressed = FALSE;
4011               gtk_drag_begin (widget,
4012                               icon_info->target_list,
4013                               icon_info->actions,
4014                               1,
4015                               (GdkEvent*)event);
4016             }
4017
4018           return TRUE;
4019         }
4020     }
4021
4022   if (priv->mouse_cursor_obscured)
4023     {
4024       GdkCursor *cursor;
4025
4026       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4027       gdk_window_set_cursor (priv->text_area, cursor);
4028       g_object_unref (cursor);
4029       priv->mouse_cursor_obscured = FALSE;
4030     }
4031
4032   if (event->window != priv->text_area || priv->button != 1)
4033     return FALSE;
4034
4035   if (priv->select_lines)
4036     return TRUE;
4037
4038   gdk_event_request_motions (event);
4039
4040   if (priv->in_drag)
4041     {
4042       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
4043           gtk_drag_check_threshold (widget,
4044                                     priv->drag_start_x, priv->drag_start_y,
4045                                     event->x + priv->scroll_offset, event->y))
4046         {
4047           GdkDragContext *context;
4048           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
4049           guint actions = priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
4050           gchar *text = NULL;
4051           cairo_surface_t *surface;
4052
4053           gtk_target_list_add_text_targets (target_list, 0);
4054
4055           text = _gtk_entry_get_selected_text (entry);
4056           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
4057
4058           context = gtk_drag_begin (widget, target_list, actions,
4059                                     priv->button, (GdkEvent *)event);
4060           
4061           if (surface)
4062             gtk_drag_set_icon_surface (context, surface);
4063           else
4064             gtk_drag_set_icon_default (context);
4065           
4066           if (surface)
4067             cairo_surface_destroy (surface);
4068           g_free (text);
4069
4070           priv->in_drag = FALSE;
4071           priv->button = 0;
4072           
4073           gtk_target_list_unref (target_list);
4074         }
4075     }
4076   else
4077     {
4078       if (event->y < 0)
4079         tmp_pos = 0;
4080       else if (event->y >= gdk_window_get_height (priv->text_area))
4081         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4082       else
4083         tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
4084
4085       if (priv->select_words)
4086         {
4087           gint min, max;
4088           gint old_min, old_max;
4089           gint pos, bound;
4090           
4091           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4092           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4093
4094           pos = priv->current_pos;
4095           bound = priv->selection_bound;
4096
4097           old_min = MIN(priv->current_pos, priv->selection_bound);
4098           old_max = MAX(priv->current_pos, priv->selection_bound);
4099           
4100           if (min < old_min)
4101             {
4102               pos = min;
4103               bound = old_max;
4104             }
4105           else if (old_max < max) 
4106             {
4107               pos = max;
4108               bound = old_min;
4109             }
4110           else if (pos == old_min) 
4111             {
4112               if (priv->current_pos != min)
4113                 pos = max;
4114             }
4115           else 
4116             {
4117               if (priv->current_pos != max)
4118                 pos = min;
4119             }
4120         
4121           gtk_entry_set_positions (entry, pos, bound);
4122         }
4123       else
4124         gtk_entry_set_positions (entry, tmp_pos, -1);
4125     }
4126       
4127   return TRUE;
4128 }
4129
4130 static void
4131 set_invisible_cursor (GdkWindow *window)
4132 {
4133   GdkDisplay *display;
4134   GdkCursor *cursor;
4135
4136   display = gdk_window_get_display (window);
4137   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4138
4139   gdk_window_set_cursor (window, cursor);
4140
4141   g_object_unref (cursor);
4142 }
4143
4144 static void
4145 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4146 {
4147   GtkEntryPrivate *priv = entry->priv;
4148
4149   if (priv->mouse_cursor_obscured)
4150     return;
4151
4152   set_invisible_cursor (priv->text_area);
4153
4154   priv->mouse_cursor_obscured = TRUE;
4155 }
4156
4157 static gint
4158 gtk_entry_key_press (GtkWidget   *widget,
4159                      GdkEventKey *event)
4160 {
4161   GtkEntry *entry = GTK_ENTRY (widget);
4162   GtkEntryPrivate *priv = entry->priv;
4163
4164   gtk_entry_reset_blink_time (entry);
4165   gtk_entry_pend_cursor_blink (entry);
4166
4167   if (priv->editable)
4168     {
4169       if (gtk_im_context_filter_keypress (priv->im_context, event))
4170         {
4171           gtk_entry_obscure_mouse_cursor (entry);
4172           priv->need_im_reset = TRUE;
4173           return TRUE;
4174         }
4175     }
4176
4177   if (event->keyval == GDK_KEY_Return || 
4178       event->keyval == GDK_KEY_KP_Enter || 
4179       event->keyval == GDK_KEY_ISO_Enter || 
4180       event->keyval == GDK_KEY_Escape)
4181     {
4182       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4183
4184       if (completion && completion->priv->completion_timeout)
4185         {
4186           g_source_remove (completion->priv->completion_timeout);
4187           completion->priv->completion_timeout = 0;
4188         }
4189
4190       _gtk_entry_reset_im_context (entry);
4191     }
4192
4193   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4194     /* Activate key bindings
4195      */
4196     return TRUE;
4197
4198   if (!priv->editable && event->length)
4199     gtk_widget_error_bell (widget);
4200
4201   return FALSE;
4202 }
4203
4204 static gint
4205 gtk_entry_key_release (GtkWidget   *widget,
4206                        GdkEventKey *event)
4207 {
4208   GtkEntry *entry = GTK_ENTRY (widget);
4209   GtkEntryPrivate *priv = entry->priv;
4210
4211   if (priv->editable)
4212     {
4213       if (gtk_im_context_filter_keypress (priv->im_context, event))
4214         {
4215           priv->need_im_reset = TRUE;
4216           return TRUE;
4217         }
4218     }
4219
4220   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4221 }
4222
4223 static gint
4224 gtk_entry_focus_in (GtkWidget     *widget,
4225                     GdkEventFocus *event)
4226 {
4227   GtkEntry *entry = GTK_ENTRY (widget);
4228   GtkEntryPrivate *priv = entry->priv;
4229   GdkKeymap *keymap;
4230
4231   gtk_widget_queue_draw (widget);
4232
4233   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4234
4235   if (priv->editable)
4236     {
4237       priv->need_im_reset = TRUE;
4238       gtk_im_context_focus_in (priv->im_context);
4239       keymap_state_changed (keymap, entry);
4240       g_signal_connect (keymap, "state-changed", 
4241                         G_CALLBACK (keymap_state_changed), entry);
4242     }
4243
4244   g_signal_connect (keymap, "direction-changed",
4245                     G_CALLBACK (keymap_direction_changed), entry);
4246
4247   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4248       priv->placeholder_text != NULL)
4249     {
4250       gtk_entry_recompute (entry);
4251     }
4252   else
4253     {
4254       gtk_entry_reset_blink_time (entry);
4255       gtk_entry_check_cursor_blink (entry);
4256     }
4257
4258   return FALSE;
4259 }
4260
4261 static gint
4262 gtk_entry_focus_out (GtkWidget     *widget,
4263                      GdkEventFocus *event)
4264 {
4265   GtkEntry *entry = GTK_ENTRY (widget);
4266   GtkEntryPrivate *priv = entry->priv;
4267   GtkEntryCompletion *completion;
4268   GdkKeymap *keymap;
4269
4270   gtk_widget_queue_draw (widget);
4271
4272   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4273
4274   if (priv->editable)
4275     {
4276       priv->need_im_reset = TRUE;
4277       gtk_im_context_focus_out (priv->im_context);
4278       remove_capslock_feedback (entry);
4279     }
4280
4281   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4282       priv->placeholder_text != NULL)
4283     {
4284       gtk_entry_recompute (entry);
4285     }
4286   else
4287     {
4288       gtk_entry_check_cursor_blink (entry);
4289     }
4290
4291   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4292   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4293
4294   completion = gtk_entry_get_completion (entry);
4295   if (completion)
4296     _gtk_entry_completion_popdown (completion);
4297
4298   return FALSE;
4299 }
4300
4301 static void
4302 gtk_entry_grab_focus (GtkWidget *widget)
4303 {
4304   GtkEntry *entry = GTK_ENTRY (widget);
4305   GtkEntryPrivate *priv = entry->priv;
4306   gboolean select_on_focus;
4307
4308   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4309
4310   if (priv->editable && !priv->in_click)
4311     {
4312       g_object_get (gtk_widget_get_settings (widget),
4313                     "gtk-entry-select-on-focus",
4314                     &select_on_focus,
4315                     NULL);
4316
4317       if (select_on_focus)
4318         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4319     }
4320 }
4321
4322 static void
4323 gtk_entry_direction_changed (GtkWidget        *widget,
4324                              GtkTextDirection  previous_dir)
4325 {
4326   GtkEntry *entry = GTK_ENTRY (widget);
4327
4328   gtk_entry_recompute (entry);
4329
4330   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4331 }
4332
4333 static void
4334 gtk_entry_state_flags_changed (GtkWidget     *widget,
4335                                GtkStateFlags  previous_state)
4336 {
4337   GtkEntry *entry = GTK_ENTRY (widget);
4338   GtkEntryPrivate *priv = entry->priv;
4339   GdkCursor *cursor;
4340
4341   if (gtk_widget_get_realized (widget))
4342     {
4343       if (gtk_widget_is_sensitive (widget))
4344         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4345       else
4346         cursor = NULL;
4347
4348       gdk_window_set_cursor (priv->text_area, cursor);
4349
4350       if (cursor)
4351         g_object_unref (cursor);
4352
4353       priv->mouse_cursor_obscured = FALSE;
4354
4355       update_cursors (widget);
4356     }
4357
4358   if (!gtk_widget_is_sensitive (widget))
4359     {
4360       /* Clear any selection */
4361       gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
4362     }
4363
4364   gtk_entry_update_cached_style_values (entry);
4365 }
4366
4367 static void
4368 gtk_entry_screen_changed (GtkWidget *widget,
4369                           GdkScreen *old_screen)
4370 {
4371   gtk_entry_recompute (GTK_ENTRY (widget));
4372 }
4373
4374 /* GtkEditable method implementations
4375  */
4376 static void
4377 gtk_entry_insert_text (GtkEditable *editable,
4378                        const gchar *new_text,
4379                        gint         new_text_length,
4380                        gint        *position)
4381 {
4382   g_object_ref (editable);
4383
4384   /*
4385    * The incoming text may a password or other secret. We make sure
4386    * not to copy it into temporary buffers.
4387    */
4388
4389   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4390
4391   g_object_unref (editable);
4392 }
4393
4394 static void
4395 gtk_entry_delete_text (GtkEditable *editable,
4396                        gint         start_pos,
4397                        gint         end_pos)
4398 {
4399   g_object_ref (editable);
4400
4401   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4402
4403   g_object_unref (editable);
4404 }
4405
4406 static gchar *    
4407 gtk_entry_get_chars      (GtkEditable   *editable,
4408                           gint           start_pos,
4409                           gint           end_pos)
4410 {
4411   GtkEntry *entry = GTK_ENTRY (editable);
4412   const gchar *text;
4413   gint text_length;
4414   gint start_index, end_index;
4415
4416   text = gtk_entry_buffer_get_text (get_buffer (entry));
4417   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4418
4419   if (end_pos < 0)
4420     end_pos = text_length;
4421
4422   start_pos = MIN (text_length, start_pos);
4423   end_pos = MIN (text_length, end_pos);
4424
4425   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4426   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4427
4428   return g_strndup (text + start_index, end_index - start_index);
4429 }
4430
4431 static void
4432 gtk_entry_real_set_position (GtkEditable *editable,
4433                              gint         position)
4434 {
4435   GtkEntry *entry = GTK_ENTRY (editable);
4436   GtkEntryPrivate *priv = entry->priv;
4437
4438   guint length;
4439
4440   length = gtk_entry_buffer_get_length (get_buffer (entry));
4441   if (position < 0 || position > length)
4442     position = length;
4443
4444   if (position != priv->current_pos ||
4445       position != priv->selection_bound)
4446     {
4447       _gtk_entry_reset_im_context (entry);
4448       gtk_entry_set_positions (entry, position, position);
4449     }
4450 }
4451
4452 static gint
4453 gtk_entry_get_position (GtkEditable *editable)
4454 {
4455   GtkEntry *entry = GTK_ENTRY (editable);
4456   GtkEntryPrivate *priv = entry->priv;
4457
4458   return priv->current_pos;
4459 }
4460
4461 static void
4462 gtk_entry_set_selection_bounds (GtkEditable *editable,
4463                                 gint         start,
4464                                 gint         end)
4465 {
4466   GtkEntry *entry = GTK_ENTRY (editable);
4467   guint length;
4468
4469   length = gtk_entry_buffer_get_length (get_buffer (entry));
4470   if (start < 0)
4471     start = length;
4472   if (end < 0)
4473     end = length;
4474   
4475   _gtk_entry_reset_im_context (entry);
4476
4477   gtk_entry_set_positions (entry,
4478                            MIN (end, length),
4479                            MIN (start, length));
4480
4481   gtk_entry_update_primary_selection (entry);
4482 }
4483
4484 static gboolean
4485 gtk_entry_get_selection_bounds (GtkEditable *editable,
4486                                 gint        *start,
4487                                 gint        *end)
4488 {
4489   GtkEntry *entry = GTK_ENTRY (editable);
4490   GtkEntryPrivate *priv = entry->priv;
4491
4492   *start = priv->selection_bound;
4493   *end = priv->current_pos;
4494
4495   return (priv->selection_bound != priv->current_pos);
4496 }
4497
4498 static void
4499 icon_theme_changed (GtkEntry *entry)
4500 {
4501   GtkEntryPrivate *priv = entry->priv;
4502   gint i;
4503
4504   for (i = 0; i < MAX_ICONS; i++)
4505     {
4506       EntryIconInfo *icon_info = priv->icons[i];
4507       if (icon_info != NULL) 
4508         {
4509           if (icon_info->storage_type == GTK_IMAGE_ICON_NAME)
4510             gtk_entry_set_icon_from_icon_name (entry, i, icon_info->icon_name);
4511           else if (icon_info->storage_type == GTK_IMAGE_STOCK)
4512             gtk_entry_set_icon_from_stock (entry, i, icon_info->stock_id);
4513           else if (icon_info->storage_type == GTK_IMAGE_GICON)
4514             gtk_entry_set_icon_from_gicon (entry, i, icon_info->gicon);
4515         }
4516     }
4517
4518   gtk_widget_queue_draw (GTK_WIDGET (entry));
4519 }
4520
4521 static void
4522 icon_margin_changed (GtkEntry *entry)
4523 {
4524   GtkEntryPrivate *priv = entry->priv;
4525   GtkBorder border;
4526
4527   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4528
4529   priv->icon_margin = border.left;
4530 }
4531
4532 static void
4533 gtk_entry_update_cached_style_values (GtkEntry *entry)
4534 {
4535   GtkEntryPrivate *priv = entry->priv;
4536   gint focus_width;
4537   gboolean interior_focus;
4538
4539   gtk_widget_style_get (GTK_WIDGET (entry),
4540                         "focus-line-width", &focus_width,
4541                         "interior-focus", &interior_focus,
4542                         NULL);
4543   priv->focus_width = focus_width;
4544   priv->interior_focus = interior_focus;
4545
4546   if (!priv->invisible_char_set)
4547     {
4548       gunichar ch = find_invisible_char (GTK_WIDGET (entry));
4549
4550       if (priv->invisible_char != ch)
4551         {
4552           priv->invisible_char = ch;
4553           g_object_notify (G_OBJECT (entry), "invisible-char");
4554         }
4555     }
4556 }
4557
4558 static void 
4559 gtk_entry_style_updated (GtkWidget *widget)
4560 {
4561   GtkEntry *entry = GTK_ENTRY (widget);
4562
4563   GTK_WIDGET_CLASS (gtk_entry_parent_class)->style_updated (widget);
4564
4565   gtk_entry_update_cached_style_values (entry);
4566
4567   gtk_entry_recompute (entry);
4568
4569   icon_theme_changed (entry);
4570   icon_margin_changed (entry);
4571 }
4572
4573 /* GtkCellEditable method implementations
4574  */
4575 static void
4576 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4577 {
4578   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4579   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4580 }
4581
4582 static gboolean
4583 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4584                                    GdkEventKey *key_event,
4585                                    gpointer     data)
4586 {
4587   GtkEntryPrivate *priv = entry->priv;
4588
4589   if (key_event->keyval == GDK_KEY_Escape)
4590     {
4591       priv->editing_canceled = TRUE;
4592       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4593       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4594
4595       return TRUE;
4596     }
4597
4598   /* override focus */
4599   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4600     {
4601       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4602       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4603
4604       return TRUE;
4605     }
4606
4607   return FALSE;
4608 }
4609
4610 static void
4611 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4612                          GdkEvent        *event)
4613 {
4614   GtkEntry *entry = GTK_ENTRY (cell_editable);
4615   GtkEntryPrivate *priv = entry->priv;
4616
4617   priv->is_cell_renderer = TRUE;
4618
4619   g_signal_connect (cell_editable, "activate",
4620                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4621   g_signal_connect (cell_editable, "key-press-event",
4622                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4623 }
4624
4625 static void
4626 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4627 {
4628   if (password_hint->source_id)
4629     g_source_remove (password_hint->source_id);
4630
4631   g_slice_free (GtkEntryPasswordHint, password_hint);
4632 }
4633
4634
4635 static gboolean
4636 gtk_entry_remove_password_hint (gpointer data)
4637 {
4638   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4639   password_hint->position = -1;
4640
4641   /* Force the string to be redrawn, but now without a visible character */
4642   gtk_entry_recompute (GTK_ENTRY (data));
4643   return FALSE;
4644 }
4645
4646 /* Default signal handlers
4647  */
4648 static void
4649 gtk_entry_real_insert_text (GtkEditable *editable,
4650                             const gchar *new_text,
4651                             gint         new_text_length,
4652                             gint        *position)
4653 {
4654   guint n_inserted;
4655   gint n_chars;
4656
4657   n_chars = g_utf8_strlen (new_text, new_text_length);
4658
4659   /*
4660    * The actual insertion into the buffer. This will end up firing the
4661    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4662    * buffer_notify_text(), buffer_notify_length()
4663    */
4664   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4665
4666   if (n_inserted != n_chars)
4667       gtk_widget_error_bell (GTK_WIDGET (editable));
4668
4669   *position += n_inserted;
4670 }
4671
4672 static void
4673 gtk_entry_real_delete_text (GtkEditable *editable,
4674                             gint         start_pos,
4675                             gint         end_pos)
4676 {
4677   /*
4678    * The actual deletion from the buffer. This will end up firing the
4679    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4680    * buffer_notify_text(), buffer_notify_length()
4681    */
4682
4683   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4684 }
4685
4686 /* GtkEntryBuffer signal handlers
4687  */
4688 static void
4689 buffer_inserted_text (GtkEntryBuffer *buffer,
4690                       guint           position,
4691                       const gchar    *chars,
4692                       guint           n_chars,
4693                       GtkEntry       *entry)
4694 {
4695   GtkEntryPrivate *priv = entry->priv;
4696   guint password_hint_timeout;
4697
4698   if (priv->current_pos > position)
4699     priv->current_pos += n_chars;
4700
4701   if (priv->selection_bound > position)
4702     priv->selection_bound += n_chars;
4703
4704   /* Calculate the password hint if it needs to be displayed. */
4705   if (n_chars == 1 && !priv->visible)
4706     {
4707       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4708                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4709                     NULL);
4710
4711       if (password_hint_timeout > 0)
4712         {
4713           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4714                                                                     quark_password_hint);
4715           if (!password_hint)
4716             {
4717               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4718               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4719                                        (GDestroyNotify)gtk_entry_password_hint_free);
4720             }
4721
4722           password_hint->position = position;
4723           if (password_hint->source_id)
4724             g_source_remove (password_hint->source_id);
4725           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4726                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4727         }
4728     }
4729 }
4730
4731 static void
4732 buffer_deleted_text (GtkEntryBuffer *buffer,
4733                      guint           position,
4734                      guint           n_chars,
4735                      GtkEntry       *entry)
4736 {
4737   GtkEntryPrivate *priv = entry->priv;
4738   guint end_pos = position + n_chars;
4739   gint selection_bound;
4740   guint current_pos;
4741
4742   current_pos = priv->current_pos;
4743   if (current_pos > position)
4744     current_pos -= MIN (current_pos, end_pos) - position;
4745
4746   selection_bound = priv->selection_bound;
4747   if (selection_bound > position)
4748     selection_bound -= MIN (selection_bound, end_pos) - position;
4749
4750   gtk_entry_set_positions (entry, current_pos, selection_bound);
4751
4752   /* We might have deleted the selection */
4753   gtk_entry_update_primary_selection (entry);
4754
4755   /* Disable the password hint if one exists. */
4756   if (!priv->visible)
4757     {
4758       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4759                                                                 quark_password_hint);
4760       if (password_hint)
4761         {
4762           if (password_hint->source_id)
4763             g_source_remove (password_hint->source_id);
4764           password_hint->source_id = 0;
4765           password_hint->position = -1;
4766         }
4767     }
4768 }
4769
4770 static void
4771 buffer_notify_text (GtkEntryBuffer *buffer,
4772                     GParamSpec     *spec,
4773                     GtkEntry       *entry)
4774 {
4775   gtk_entry_recompute (entry);
4776   emit_changed (entry);
4777   g_object_notify (G_OBJECT (entry), "text");
4778 }
4779
4780 static void
4781 buffer_notify_length (GtkEntryBuffer *buffer,
4782                       GParamSpec     *spec,
4783                       GtkEntry       *entry)
4784 {
4785   g_object_notify (G_OBJECT (entry), "text-length");
4786 }
4787
4788 static void
4789 buffer_notify_max_length (GtkEntryBuffer *buffer,
4790                           GParamSpec     *spec,
4791                           GtkEntry       *entry)
4792 {
4793   g_object_notify (G_OBJECT (entry), "max-length");
4794 }
4795
4796 static void
4797 buffer_connect_signals (GtkEntry *entry)
4798 {
4799   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4800   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4801   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4802   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4803   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4804 }
4805
4806 static void
4807 buffer_disconnect_signals (GtkEntry *entry)
4808 {
4809   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4810   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4811   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4812   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4813   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4814 }
4815
4816 /* Compute the X position for an offset that corresponds to the "more important
4817  * cursor position for that offset. We use this when trying to guess to which
4818  * end of the selection we should go to when the user hits the left or
4819  * right arrow key.
4820  */
4821 static gint
4822 get_better_cursor_x (GtkEntry *entry,
4823                      gint      offset)
4824 {
4825   GtkEntryPrivate *priv = entry->priv;
4826   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4827   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4828   gboolean split_cursor;
4829   
4830   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4831   const gchar *text = pango_layout_get_text (layout);
4832   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4833   
4834   PangoRectangle strong_pos, weak_pos;
4835   
4836   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4837                 "gtk-split-cursor", &split_cursor,
4838                 NULL);
4839
4840   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4841
4842   if (split_cursor)
4843     return strong_pos.x / PANGO_SCALE;
4844   else
4845     return (keymap_direction == priv->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4846 }
4847
4848 static void
4849 gtk_entry_move_cursor (GtkEntry       *entry,
4850                        GtkMovementStep step,
4851                        gint            count,
4852                        gboolean        extend_selection)
4853 {
4854   GtkEntryPrivate *priv = entry->priv;
4855   gint new_pos = priv->current_pos;
4856
4857   _gtk_entry_reset_im_context (entry);
4858
4859   if (priv->current_pos != priv->selection_bound && !extend_selection)
4860     {
4861       /* If we have a current selection and aren't extending it, move to the
4862        * start/or end of the selection as appropriate
4863        */
4864       switch (step)
4865         {
4866         case GTK_MOVEMENT_VISUAL_POSITIONS:
4867           {
4868             gint current_x = get_better_cursor_x (entry, priv->current_pos);
4869             gint bound_x = get_better_cursor_x (entry, priv->selection_bound);
4870
4871             if (count <= 0)
4872               new_pos = current_x < bound_x ? priv->current_pos : priv->selection_bound;
4873             else 
4874               new_pos = current_x > bound_x ? priv->current_pos : priv->selection_bound;
4875             break;
4876           }
4877         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4878         case GTK_MOVEMENT_WORDS:
4879           if (count < 0)
4880             new_pos = MIN (priv->current_pos, priv->selection_bound);
4881           else
4882             new_pos = MAX (priv->current_pos, priv->selection_bound);
4883           break;
4884         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4885         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4886         case GTK_MOVEMENT_BUFFER_ENDS:
4887           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4888           break;
4889         case GTK_MOVEMENT_DISPLAY_LINES:
4890         case GTK_MOVEMENT_PARAGRAPHS:
4891         case GTK_MOVEMENT_PAGES:
4892         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4893           break;
4894         }
4895     }
4896   else
4897     {
4898       switch (step)
4899         {
4900         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4901           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4902           break;
4903         case GTK_MOVEMENT_VISUAL_POSITIONS:
4904           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4905           if (priv->current_pos == new_pos)
4906             {
4907               if (!extend_selection)
4908                 {
4909                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4910                                                  count > 0 ?
4911                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4912                     {
4913                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4914
4915                       if (toplevel)
4916                         gtk_widget_child_focus (toplevel,
4917                                                 count > 0 ?
4918                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4919                     }
4920                 }
4921               else
4922                 {
4923                   gtk_widget_error_bell (GTK_WIDGET (entry));
4924                 }
4925             }
4926           break;
4927         case GTK_MOVEMENT_WORDS:
4928           while (count > 0)
4929             {
4930               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4931               count--;
4932             }
4933           while (count < 0)
4934             {
4935               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4936               count++;
4937             }
4938           if (priv->current_pos == new_pos)
4939             gtk_widget_error_bell (GTK_WIDGET (entry));
4940           break;
4941         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4942         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4943         case GTK_MOVEMENT_BUFFER_ENDS:
4944           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4945           if (priv->current_pos == new_pos)
4946             gtk_widget_error_bell (GTK_WIDGET (entry));
4947           break;
4948         case GTK_MOVEMENT_DISPLAY_LINES:
4949         case GTK_MOVEMENT_PARAGRAPHS:
4950         case GTK_MOVEMENT_PAGES:
4951         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4952           break;
4953         }
4954     }
4955
4956   if (extend_selection)
4957     gtk_editable_select_region (GTK_EDITABLE (entry), priv->selection_bound, new_pos);
4958   else
4959     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4960   
4961   gtk_entry_pend_cursor_blink (entry);
4962 }
4963
4964 static void
4965 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4966                             const gchar *str)
4967 {
4968   GtkEntryPrivate *priv = entry->priv;
4969   GtkEditable *editable = GTK_EDITABLE (entry);
4970   gint pos = priv->current_pos;
4971
4972   if (priv->editable)
4973     {
4974       _gtk_entry_reset_im_context (entry);
4975
4976       gtk_editable_insert_text (editable, str, -1, &pos);
4977       gtk_editable_set_position (editable, pos);
4978     }
4979 }
4980
4981 static void
4982 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4983                               GtkDeleteType   type,
4984                               gint            count)
4985 {
4986   GtkEntryPrivate *priv = entry->priv;
4987   GtkEditable *editable = GTK_EDITABLE (entry);
4988   gint start_pos = priv->current_pos;
4989   gint end_pos = priv->current_pos;
4990   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
4991   
4992   _gtk_entry_reset_im_context (entry);
4993
4994   if (!priv->editable)
4995     {
4996       gtk_widget_error_bell (GTK_WIDGET (entry));
4997       return;
4998     }
4999
5000   if (priv->selection_bound != priv->current_pos)
5001     {
5002       gtk_editable_delete_selection (editable);
5003       return;
5004     }
5005   
5006   switch (type)
5007     {
5008     case GTK_DELETE_CHARS:
5009       end_pos = gtk_entry_move_logically (entry, priv->current_pos, count);
5010       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
5011       break;
5012     case GTK_DELETE_WORDS:
5013       if (count < 0)
5014         {
5015           /* Move to end of current word, or if not on a word, end of previous word */
5016           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
5017           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5018         }
5019       else if (count > 0)
5020         {
5021           /* Move to beginning of current word, or if not on a word, begining of next word */
5022           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
5023           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5024         }
5025         
5026       /* Fall through */
5027     case GTK_DELETE_WORD_ENDS:
5028       while (count < 0)
5029         {
5030           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5031           count++;
5032         }
5033       while (count > 0)
5034         {
5035           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5036           count--;
5037         }
5038       gtk_editable_delete_text (editable, start_pos, end_pos);
5039       break;
5040     case GTK_DELETE_DISPLAY_LINE_ENDS:
5041     case GTK_DELETE_PARAGRAPH_ENDS:
5042       if (count < 0)
5043         gtk_editable_delete_text (editable, 0, priv->current_pos);
5044       else
5045         gtk_editable_delete_text (editable, priv->current_pos, -1);
5046       break;
5047     case GTK_DELETE_DISPLAY_LINES:
5048     case GTK_DELETE_PARAGRAPHS:
5049       gtk_editable_delete_text (editable, 0, -1);  
5050       break;
5051     case GTK_DELETE_WHITESPACE:
5052       gtk_entry_delete_whitespace (entry);
5053       break;
5054     }
5055
5056   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
5057     gtk_widget_error_bell (GTK_WIDGET (entry));
5058
5059   gtk_entry_pend_cursor_blink (entry);
5060 }
5061
5062 static void
5063 gtk_entry_backspace (GtkEntry *entry)
5064 {
5065   GtkEntryPrivate *priv = entry->priv;
5066   GtkEditable *editable = GTK_EDITABLE (entry);
5067   gint prev_pos;
5068
5069   _gtk_entry_reset_im_context (entry);
5070
5071   if (!priv->editable)
5072     {
5073       gtk_widget_error_bell (GTK_WIDGET (entry));
5074       return;
5075     }
5076
5077   if (priv->selection_bound != priv->current_pos)
5078     {
5079       gtk_editable_delete_selection (editable);
5080       return;
5081     }
5082
5083   prev_pos = gtk_entry_move_logically (entry, priv->current_pos, -1);
5084
5085   if (prev_pos < priv->current_pos)
5086     {
5087       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5088       PangoLogAttr *log_attrs;
5089       gint n_attrs;
5090
5091       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5092
5093       /* Deleting parts of characters */
5094       if (log_attrs[priv->current_pos].backspace_deletes_character)
5095         {
5096           gchar *cluster_text;
5097           gchar *normalized_text;
5098           glong  len;
5099
5100           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5101                                                      priv->current_pos);
5102           normalized_text = g_utf8_normalize (cluster_text,
5103                                               strlen (cluster_text),
5104                                               G_NORMALIZE_NFD);
5105           len = g_utf8_strlen (normalized_text, -1);
5106
5107           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5108           if (len > 1)
5109             {
5110               gint pos = priv->current_pos;
5111
5112               gtk_editable_insert_text (editable, normalized_text,
5113                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5114                                         &pos);
5115               gtk_editable_set_position (editable, pos);
5116             }
5117
5118           g_free (normalized_text);
5119           g_free (cluster_text);
5120         }
5121       else
5122         {
5123           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5124         }
5125       
5126       g_free (log_attrs);
5127     }
5128   else
5129     {
5130       gtk_widget_error_bell (GTK_WIDGET (entry));
5131     }
5132
5133   gtk_entry_pend_cursor_blink (entry);
5134 }
5135
5136 static void
5137 gtk_entry_copy_clipboard (GtkEntry *entry)
5138 {
5139   GtkEntryPrivate *priv = entry->priv;
5140   GtkEditable *editable = GTK_EDITABLE (entry);
5141   gint start, end;
5142   gchar *str;
5143
5144   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5145     {
5146       if (!priv->visible)
5147         {
5148           gtk_widget_error_bell (GTK_WIDGET (entry));
5149           return;
5150         }
5151
5152       str = gtk_entry_get_display_text (entry, start, end);
5153       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5154                                                         GDK_SELECTION_CLIPBOARD),
5155                               str, -1);
5156       g_free (str);
5157     }
5158 }
5159
5160 static void
5161 gtk_entry_cut_clipboard (GtkEntry *entry)
5162 {
5163   GtkEntryPrivate *priv = entry->priv;
5164   GtkEditable *editable = GTK_EDITABLE (entry);
5165   gint start, end;
5166
5167   if (!priv->visible)
5168     {
5169       gtk_widget_error_bell (GTK_WIDGET (entry));
5170       return;
5171     }
5172
5173   gtk_entry_copy_clipboard (entry);
5174
5175   if (priv->editable)
5176     {
5177       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5178         gtk_editable_delete_text (editable, start, end);
5179     }
5180   else
5181     {
5182       gtk_widget_error_bell (GTK_WIDGET (entry));
5183     }
5184 }
5185
5186 static void
5187 gtk_entry_paste_clipboard (GtkEntry *entry)
5188 {
5189   GtkEntryPrivate *priv = entry->priv;
5190
5191   if (priv->editable)
5192     gtk_entry_paste (entry, GDK_SELECTION_CLIPBOARD);
5193   else
5194     gtk_widget_error_bell (GTK_WIDGET (entry));
5195 }
5196
5197 static void
5198 gtk_entry_delete_cb (GtkEntry *entry)
5199 {
5200   GtkEntryPrivate *priv = entry->priv;
5201   GtkEditable *editable = GTK_EDITABLE (entry);
5202   gint start, end;
5203
5204   if (priv->editable)
5205     {
5206       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5207         gtk_editable_delete_text (editable, start, end);
5208     }
5209 }
5210
5211 static void
5212 gtk_entry_toggle_overwrite (GtkEntry *entry)
5213 {
5214   GtkEntryPrivate *priv = entry->priv;
5215
5216   priv->overwrite_mode = !priv->overwrite_mode;
5217   gtk_entry_pend_cursor_blink (entry);
5218   gtk_widget_queue_draw (GTK_WIDGET (entry));
5219 }
5220
5221 static void
5222 gtk_entry_select_all (GtkEntry *entry)
5223 {
5224   gtk_entry_select_line (entry);
5225 }
5226
5227 static void
5228 gtk_entry_real_activate (GtkEntry *entry)
5229 {
5230   GtkEntryPrivate *priv = entry->priv;
5231   GtkWindow *window;
5232   GtkWidget *default_widget, *focus_widget;
5233   GtkWidget *toplevel;
5234   GtkWidget *widget;
5235
5236   widget = GTK_WIDGET (entry);
5237
5238   if (priv->activates_default)
5239     {
5240       toplevel = gtk_widget_get_toplevel (widget);
5241       if (GTK_IS_WINDOW (toplevel))
5242         {
5243           window = GTK_WINDOW (toplevel);
5244
5245           if (window)
5246             {
5247               default_widget = gtk_window_get_default_widget (window);
5248               focus_widget = gtk_window_get_focus (window);
5249               if (widget != default_widget &&
5250                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5251                 gtk_window_activate_default (window);
5252             }
5253         }
5254     }
5255 }
5256
5257 static void
5258 keymap_direction_changed (GdkKeymap *keymap,
5259                           GtkEntry  *entry)
5260 {
5261   gtk_entry_recompute (entry);
5262 }
5263
5264 /* IM Context Callbacks
5265  */
5266
5267 static void
5268 gtk_entry_commit_cb (GtkIMContext *context,
5269                      const gchar  *str,
5270                      GtkEntry     *entry)
5271 {
5272   GtkEntryPrivate *priv = entry->priv;
5273
5274   if (priv->editable)
5275     gtk_entry_enter_text (entry, str);
5276 }
5277
5278 static void 
5279 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5280                               GtkEntry     *entry)
5281 {
5282   GtkEntryPrivate *priv = entry->priv;
5283
5284   if (priv->editable)
5285     {
5286       gchar *preedit_string;
5287       gint cursor_pos;
5288
5289       gtk_im_context_get_preedit_string (priv->im_context,
5290                                          &preedit_string, NULL,
5291                                          &cursor_pos);
5292       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5293       priv->preedit_length = strlen (preedit_string);
5294       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5295       priv->preedit_cursor = cursor_pos;
5296       g_free (preedit_string);
5297     
5298       gtk_entry_recompute (entry);
5299     }
5300 }
5301
5302 static gboolean
5303 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5304                                    GtkEntry     *entry)
5305 {
5306   GtkEntryPrivate *priv = entry->priv;
5307   gchar *text;
5308
5309   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5310   text = gtk_entry_get_display_text (entry, 0, -1);
5311   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5312                                   g_utf8_offset_to_pointer (text, priv->current_pos) - text);
5313   g_free (text);
5314
5315   return TRUE;
5316 }
5317
5318 static gboolean
5319 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5320                                  gint          offset,
5321                                  gint          n_chars,
5322                                  GtkEntry     *entry)
5323 {
5324   GtkEntryPrivate *priv = entry->priv;
5325
5326   if (priv->editable)
5327     gtk_editable_delete_text (GTK_EDITABLE (entry),
5328                               priv->current_pos + offset,
5329                               priv->current_pos + offset + n_chars);
5330
5331   return TRUE;
5332 }
5333
5334 /* Internal functions
5335  */
5336
5337 /* Used for im_commit_cb and inserting Unicode chars */
5338 static void
5339 gtk_entry_enter_text (GtkEntry       *entry,
5340                       const gchar    *str)
5341 {
5342   GtkEntryPrivate *priv = entry->priv;
5343   GtkEditable *editable = GTK_EDITABLE (entry);
5344   gint tmp_pos;
5345   gboolean old_need_im_reset;
5346
5347   old_need_im_reset = priv->need_im_reset;
5348   priv->need_im_reset = FALSE;
5349
5350   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5351     gtk_editable_delete_selection (editable);
5352   else
5353     {
5354       if (priv->overwrite_mode)
5355         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5356     }
5357
5358   tmp_pos = priv->current_pos;
5359   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5360   gtk_editable_set_position (editable, tmp_pos);
5361
5362   priv->need_im_reset = old_need_im_reset;
5363 }
5364
5365 /* All changes to priv->current_pos and priv->selection_bound
5366  * should go through this function.
5367  */
5368 static void
5369 gtk_entry_set_positions (GtkEntry *entry,
5370                          gint      current_pos,
5371                          gint      selection_bound)
5372 {
5373   GtkEntryPrivate *priv = entry->priv;
5374   gboolean changed = FALSE;
5375
5376   g_object_freeze_notify (G_OBJECT (entry));
5377   
5378   if (current_pos != -1 &&
5379       priv->current_pos != current_pos)
5380     {
5381       priv->current_pos = current_pos;
5382       changed = TRUE;
5383
5384       g_object_notify (G_OBJECT (entry), "cursor-position");
5385     }
5386
5387   if (selection_bound != -1 &&
5388       priv->selection_bound != selection_bound)
5389     {
5390       priv->selection_bound = selection_bound;
5391       changed = TRUE;
5392       
5393       g_object_notify (G_OBJECT (entry), "selection-bound");
5394     }
5395
5396   g_object_thaw_notify (G_OBJECT (entry));
5397
5398   if (changed) 
5399     {
5400       gtk_entry_move_adjustments (entry);
5401       gtk_entry_recompute (entry);
5402     }
5403 }
5404
5405 static void
5406 gtk_entry_reset_layout (GtkEntry *entry)
5407 {
5408   GtkEntryPrivate *priv = entry->priv;
5409
5410   if (priv->cached_layout)
5411     {
5412       g_object_unref (priv->cached_layout);
5413       priv->cached_layout = NULL;
5414     }
5415 }
5416
5417 static void
5418 update_im_cursor_location (GtkEntry *entry)
5419 {
5420   GtkEntryPrivate *priv = entry->priv;
5421   GdkRectangle area;
5422   gint strong_x;
5423   gint strong_xoffset;
5424   gint area_width, area_height;
5425
5426   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5427   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5428
5429   strong_xoffset = strong_x - priv->scroll_offset;
5430   if (strong_xoffset < 0)
5431     {
5432       strong_xoffset = 0;
5433     }
5434   else if (strong_xoffset > area_width)
5435     {
5436       strong_xoffset = area_width;
5437     }
5438   area.x = strong_xoffset;
5439   area.y = 0;
5440   area.width = 0;
5441   area.height = area_height;
5442
5443   gtk_im_context_set_cursor_location (priv->im_context, &area);
5444 }
5445
5446 static gboolean
5447 recompute_idle_func (gpointer data)
5448 {
5449   GtkEntry *entry = GTK_ENTRY (data);
5450   GtkEntryPrivate *priv = entry->priv;
5451
5452   priv->recompute_idle = 0;
5453
5454   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5455     {
5456       gtk_entry_adjust_scroll (entry);
5457       gtk_widget_queue_draw (GTK_WIDGET (entry));
5458
5459       update_im_cursor_location (entry);
5460     }
5461
5462   return FALSE;
5463 }
5464
5465 static void
5466 gtk_entry_recompute (GtkEntry *entry)
5467 {
5468   GtkEntryPrivate *priv = entry->priv;
5469
5470   gtk_entry_reset_layout (entry);
5471   gtk_entry_check_cursor_blink (entry);
5472
5473   if (!priv->recompute_idle)
5474     {
5475       priv->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5476                                                recompute_idle_func, entry, NULL); 
5477     }
5478 }
5479
5480 static void
5481 gtk_entry_get_placeholder_text_color (GtkEntry   *entry,
5482                                       PangoColor *color)
5483 {
5484   GtkWidget *widget = GTK_WIDGET (entry);
5485   GtkStyleContext *context;
5486   GdkRGBA fg = { 0.5, 0.5, 0.5 };
5487
5488   context = gtk_widget_get_style_context (widget);
5489   gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
5490
5491   color->red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
5492   color->green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
5493   color->blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
5494 }
5495
5496 static inline gboolean
5497 show_placeholder_text (GtkEntry *entry)
5498 {
5499   GtkEntryPrivate *priv = entry->priv;
5500
5501   if (!gtk_widget_has_focus (GTK_WIDGET (entry)) &&
5502       gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
5503       priv->placeholder_text != NULL)
5504     return TRUE;
5505
5506   return FALSE;
5507 }
5508
5509 static PangoLayout *
5510 gtk_entry_create_layout (GtkEntry *entry,
5511                          gboolean  include_preedit)
5512 {
5513   GtkEntryPrivate *priv = entry->priv;
5514   GtkWidget *widget = GTK_WIDGET (entry);
5515   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5516   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5517   gboolean placeholder_layout = show_placeholder_text (entry);
5518
5519   gchar *preedit_string = NULL;
5520   gint preedit_length = 0;
5521   PangoAttrList *preedit_attrs = NULL;
5522
5523   gchar *display;
5524   guint n_bytes;
5525
5526   pango_layout_set_single_paragraph_mode (layout, TRUE);
5527
5528   display = placeholder_layout ? g_strdup (priv->placeholder_text) : gtk_entry_get_display_text (entry, 0, -1);
5529   n_bytes = strlen (display);
5530
5531   if (!placeholder_layout && include_preedit)
5532     {
5533       gtk_im_context_get_preedit_string (priv->im_context,
5534                                          &preedit_string, &preedit_attrs, NULL);
5535       preedit_length = priv->preedit_length;
5536     }
5537   else if (placeholder_layout)
5538     {
5539       PangoColor color;
5540       PangoAttribute *attr;
5541
5542       gtk_entry_get_placeholder_text_color (entry, &color);
5543       attr = pango_attr_foreground_new (color.red, color.green, color.blue);
5544       attr->start_index = 0;
5545       attr->end_index = G_MAXINT;
5546       pango_attr_list_insert (tmp_attrs, attr);
5547     }
5548
5549   if (preedit_length)
5550     {
5551       GString *tmp_string = g_string_new (display);
5552       gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
5553
5554       g_string_insert (tmp_string, cursor_index, preedit_string);
5555       
5556       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5557       
5558       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5559                               cursor_index, preedit_length);
5560       
5561       g_string_free (tmp_string, TRUE);
5562     }
5563   else
5564     {
5565       PangoDirection pango_dir;
5566       
5567       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5568         pango_dir = pango_find_base_dir (display, n_bytes);
5569       else
5570         pango_dir = PANGO_DIRECTION_NEUTRAL;
5571
5572       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5573         {
5574           if (gtk_widget_has_focus (widget))
5575             {
5576               GdkDisplay *display = gtk_widget_get_display (widget);
5577               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5578               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5579                 pango_dir = PANGO_DIRECTION_RTL;
5580               else
5581                 pango_dir = PANGO_DIRECTION_LTR;
5582             }
5583           else
5584             {
5585               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5586                 pango_dir = PANGO_DIRECTION_RTL;
5587               else
5588                 pango_dir = PANGO_DIRECTION_LTR;
5589             }
5590         }
5591
5592       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5593                                   pango_dir);
5594
5595       priv->resolved_dir = pango_dir;
5596
5597       pango_layout_set_text (layout, display, n_bytes);
5598     }
5599       
5600   pango_layout_set_attributes (layout, tmp_attrs);
5601
5602   g_free (preedit_string);
5603   g_free (display);
5604
5605   if (preedit_attrs)
5606     pango_attr_list_unref (preedit_attrs);
5607       
5608   pango_attr_list_unref (tmp_attrs);
5609
5610   return layout;
5611 }
5612
5613 static PangoLayout *
5614 gtk_entry_ensure_layout (GtkEntry *entry,
5615                          gboolean  include_preedit)
5616 {
5617   GtkEntryPrivate *priv = entry->priv;
5618
5619   if (priv->preedit_length > 0 &&
5620       !include_preedit != !priv->cache_includes_preedit)
5621     gtk_entry_reset_layout (entry);
5622
5623   if (!priv->cached_layout)
5624     {
5625       priv->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5626       priv->cache_includes_preedit = include_preedit;
5627     }
5628
5629   return priv->cached_layout;
5630 }
5631
5632 static void
5633 get_layout_position (GtkEntry *entry,
5634                      gint     *x,
5635                      gint     *y)
5636 {
5637   GtkEntryPrivate *priv = entry->priv;
5638   PangoLayout *layout;
5639   PangoRectangle logical_rect;
5640   gint area_width, area_height;
5641   GtkBorder inner_border;
5642   gint y_pos;
5643   PangoLayoutLine *line;
5644   
5645   layout = gtk_entry_ensure_layout (entry, TRUE);
5646
5647   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5648   _gtk_entry_effective_inner_border (entry, &inner_border);
5649
5650   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5651
5652   line = pango_layout_get_lines_readonly (layout)->data;
5653   pango_layout_line_get_extents (line, NULL, &logical_rect);
5654   
5655   /* Align primarily for locale's ascent/descent */
5656   y_pos = ((area_height - priv->ascent - priv->descent) / 2 +
5657            priv->ascent + logical_rect.y);
5658
5659   /* Now see if we need to adjust to fit in actual drawn string */
5660   if (logical_rect.height > area_height)
5661     y_pos = (area_height - logical_rect.height) / 2;
5662   else if (y_pos < 0)
5663     y_pos = 0;
5664   else if (y_pos + logical_rect.height > area_height)
5665     y_pos = area_height - logical_rect.height;
5666   
5667   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5668
5669   if (x)
5670     *x = inner_border.left - priv->scroll_offset;
5671
5672   if (y)
5673     *y = y_pos;
5674 }
5675
5676 static void
5677 draw_text_with_color (GtkEntry *entry,
5678                       cairo_t  *cr,
5679                       GdkRGBA  *default_color)
5680 {
5681   GtkEntryPrivate *priv = entry->priv;
5682   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5683   GtkWidget *widget;
5684   gint x, y;
5685   gint start_pos, end_pos;
5686
5687   widget = GTK_WIDGET (entry);
5688
5689   cairo_save (cr);
5690
5691   get_layout_position (entry, &x, &y);
5692
5693   cairo_move_to (cr, x, y);
5694   gdk_cairo_set_source_rgba (cr, default_color);
5695   pango_cairo_show_layout (cr, layout);
5696
5697   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5698     {
5699       gint *ranges;
5700       gint n_ranges, i;
5701       PangoRectangle logical_rect;
5702       GdkRGBA selection_color, text_color;
5703       GtkBorder inner_border;
5704       GtkStyleContext *context;
5705       GtkStateFlags state;
5706
5707       context = gtk_widget_get_style_context (widget);
5708       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5709       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5710
5711       state = GTK_STATE_FLAG_SELECTED;
5712
5713       if (gtk_widget_has_focus (widget))
5714         state |= GTK_STATE_FLAG_FOCUSED;
5715
5716       gtk_style_context_get_background_color (context, state, &selection_color);
5717       gtk_style_context_get_color (context, state, &text_color);
5718
5719       _gtk_entry_effective_inner_border (entry, &inner_border);
5720
5721       for (i = 0; i < n_ranges; ++i)
5722         cairo_rectangle (cr,
5723                          inner_border.left - priv->scroll_offset + ranges[2 * i],
5724                          y,
5725                          ranges[2 * i + 1],
5726                          logical_rect.height);
5727
5728       cairo_clip (cr);
5729           
5730       gdk_cairo_set_source_rgba (cr, &selection_color);
5731       cairo_paint (cr);
5732
5733       cairo_move_to (cr, x, y);
5734       gdk_cairo_set_source_rgba (cr, &text_color);
5735       pango_cairo_show_layout (cr, layout);
5736   
5737       g_free (ranges);
5738     }
5739   cairo_restore (cr);
5740 }
5741
5742 static void
5743 gtk_entry_draw_text (GtkEntry *entry,
5744                      cairo_t  *cr)
5745 {
5746   GtkEntryPrivate *priv = entry->priv;
5747   GtkWidget *widget = GTK_WIDGET (entry);
5748   GtkStateFlags state = 0;
5749   GdkRGBA text_color, bar_text_color;
5750   GtkStyleContext *context;
5751   gint width, height;
5752   gint progress_x, progress_y, progress_width, progress_height;
5753   gint clip_width, clip_height;
5754
5755   /* Nothing to display at all */
5756   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5757     return;
5758
5759   state = gtk_widget_get_state_flags (widget);
5760   context = gtk_widget_get_style_context (widget);
5761
5762   gtk_style_context_get_color (context, state, &text_color);
5763
5764   /* Get foreground color for progressbars */
5765   gtk_style_context_save (context);
5766   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
5767   gtk_style_context_get_color (context, state, &bar_text_color);
5768   gtk_style_context_restore (context);
5769
5770   get_progress_area (widget,
5771                      &progress_x, &progress_y,
5772                      &progress_width, &progress_height);
5773
5774   cairo_save (cr);
5775
5776   clip_width = gdk_window_get_width (priv->text_area);
5777   clip_height = gdk_window_get_height (priv->text_area);
5778   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5779   cairo_clip (cr);
5780
5781   /* If the color is the same, or the progress area has a zero
5782    * size, then we only need to draw once. */
5783   if (gdk_rgba_equal (&text_color, &bar_text_color) ||
5784       ((progress_width == 0) || (progress_height == 0)))
5785     {
5786       draw_text_with_color (entry, cr, &text_color);
5787     }
5788   else
5789     {
5790       int frame_x, frame_y, area_x, area_y;
5791
5792       width = gdk_window_get_width (priv->text_area);
5793       height = gdk_window_get_height (priv->text_area);
5794
5795       cairo_save (cr);
5796
5797       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5798       cairo_rectangle (cr, 0, 0, width, height);
5799
5800       /* progres area is frame-relative, we need it text-area window
5801        * relative */
5802       get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
5803       gdk_window_get_position (priv->text_area, &area_x, &area_y);
5804       progress_x += frame_x - area_x;
5805       progress_y += frame_y - area_y;
5806
5807       cairo_rectangle (cr, progress_x, progress_y,
5808                        progress_width, progress_height);
5809       cairo_clip (cr);
5810       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5811   
5812       draw_text_with_color (entry, cr, &text_color);
5813       cairo_restore (cr);
5814
5815       cairo_save (cr);
5816
5817       cairo_rectangle (cr, progress_x, progress_y,
5818                        progress_width, progress_height);
5819       cairo_clip (cr);
5820
5821       draw_text_with_color (entry, cr, &bar_text_color);
5822
5823       cairo_restore (cr);
5824     }
5825
5826   cairo_restore (cr);
5827 }
5828
5829 static void
5830 draw_insertion_cursor (GtkEntry      *entry,
5831                        cairo_t       *cr,
5832                        GdkRectangle  *cursor_location,
5833                        gboolean       is_primary,
5834                        PangoDirection direction,
5835                        gboolean       draw_arrow)
5836 {
5837   GtkWidget *widget = GTK_WIDGET (entry);
5838   GtkTextDirection text_dir;
5839
5840   if (direction == PANGO_DIRECTION_LTR)
5841     text_dir = GTK_TEXT_DIR_LTR;
5842   else
5843     text_dir = GTK_TEXT_DIR_RTL;
5844
5845   gtk_draw_insertion_cursor (widget, cr,
5846                              cursor_location,
5847                              is_primary, text_dir, draw_arrow);
5848 }
5849
5850 static void
5851 gtk_entry_draw_cursor (GtkEntry  *entry,
5852                        cairo_t   *cr,
5853                        CursorType type)
5854 {
5855   GtkEntryPrivate *priv = entry->priv;
5856   GtkWidget *widget = GTK_WIDGET (entry);
5857   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5858   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5859   GdkRectangle cursor_location;
5860   gboolean split_cursor;
5861   PangoRectangle cursor_rect;
5862   GtkBorder inner_border;
5863   gint xoffset;
5864   gint text_area_height;
5865   gint cursor_index;
5866   gboolean block;
5867   gboolean block_at_line_end;
5868   PangoLayout *layout;
5869   const char *text;
5870
5871   _gtk_entry_effective_inner_border (entry, &inner_border);
5872
5873   xoffset = inner_border.left - priv->scroll_offset;
5874
5875   text_area_height = gdk_window_get_height (priv->text_area);
5876
5877   layout = gtk_entry_ensure_layout (entry, TRUE);
5878   text = pango_layout_get_text (layout);
5879   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
5880   if (!priv->overwrite_mode)
5881     block = FALSE;
5882   else
5883     block = _gtk_text_util_get_block_cursor_location (layout,
5884                                                       cursor_index, &cursor_rect, &block_at_line_end);
5885
5886   if (!block)
5887     {
5888       gint strong_x, weak_x;
5889       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
5890       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
5891       gint x1 = 0;
5892       gint x2 = 0;
5893
5894       gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
5895
5896       g_object_get (gtk_widget_get_settings (widget),
5897                     "gtk-split-cursor", &split_cursor,
5898                     NULL);
5899
5900       dir1 = priv->resolved_dir;
5901   
5902       if (split_cursor)
5903         {
5904           x1 = strong_x;
5905
5906           if (weak_x != strong_x)
5907             {
5908               dir2 = (priv->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
5909               x2 = weak_x;
5910             }
5911         }
5912       else
5913         {
5914           if (keymap_direction == priv->resolved_dir)
5915             x1 = strong_x;
5916           else
5917             x1 = weak_x;
5918         }
5919
5920       cursor_location.x = xoffset + x1;
5921       cursor_location.y = inner_border.top;
5922       cursor_location.width = 0;
5923       cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
5924
5925       draw_insertion_cursor (entry, cr,
5926                              &cursor_location, TRUE, dir1,
5927                              dir2 != PANGO_DIRECTION_NEUTRAL);
5928   
5929       if (dir2 != PANGO_DIRECTION_NEUTRAL)
5930         {
5931           cursor_location.x = xoffset + x2;
5932           draw_insertion_cursor (entry, cr,
5933                                  &cursor_location, FALSE, dir2,
5934                                  TRUE);
5935         }
5936     }
5937   else /* overwrite_mode */
5938     {
5939       GtkStyleContext *context;
5940       GdkRGBA cursor_color;
5941       GdkRectangle rect;
5942       gint x, y;
5943
5944       cairo_save (cr);
5945
5946       get_layout_position (entry, &x, &y);
5947
5948       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5949       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5950       rect.width = PANGO_PIXELS (cursor_rect.width);
5951       rect.height = PANGO_PIXELS (cursor_rect.height);
5952
5953       context = gtk_widget_get_style_context (widget);
5954
5955       _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
5956       gdk_cairo_set_source_rgba (cr, &cursor_color);
5957       gdk_cairo_rectangle (cr, &rect);
5958       cairo_fill (cr);
5959
5960       if (!block_at_line_end)
5961         {
5962           GtkStateFlags state;
5963           GdkRGBA color;
5964
5965           state = gtk_widget_get_state_flags (widget);
5966           gtk_style_context_get_background_color (context, state, &color);
5967
5968           gdk_cairo_rectangle (cr, &rect);
5969           cairo_clip (cr);
5970           cairo_move_to (cr, x, y);
5971           gdk_cairo_set_source_rgba (cr, &color);
5972           pango_cairo_show_layout (cr, layout);
5973         }
5974
5975       cairo_restore (cr);
5976     }
5977 }
5978
5979 void
5980 _gtk_entry_reset_im_context (GtkEntry *entry)
5981 {
5982   GtkEntryPrivate *priv = entry->priv;
5983
5984   if (priv->need_im_reset)
5985     {
5986       priv->need_im_reset = FALSE;
5987       gtk_im_context_reset (priv->im_context);
5988     }
5989 }
5990
5991 /**
5992  * gtk_entry_reset_im_context:
5993  * @entry: a #GtkEntry
5994  *
5995  * Reset the input method context of the entry if needed.
5996  *
5997  * This can be necessary in the case where modifying the buffer
5998  * would confuse on-going input method behavior.
5999  *
6000  * Since: 2.22
6001  */
6002 void
6003 gtk_entry_reset_im_context (GtkEntry *entry)
6004 {
6005   g_return_if_fail (GTK_IS_ENTRY (entry));
6006
6007   _gtk_entry_reset_im_context (entry);
6008 }
6009
6010 /**
6011  * gtk_entry_im_context_filter_keypress:
6012  * @entry: a #GtkEntry
6013  * @event: (type Gdk.EventKey): the key event
6014  *
6015  * Allow the #GtkEntry input method to internally handle key press
6016  * and release events. If this function returns %TRUE, then no further
6017  * processing should be done for this key event. See
6018  * gtk_im_context_filter_keypress().
6019  *
6020  * Note that you are expected to call this function from your handler
6021  * when overriding key event handling. This is needed in the case when
6022  * you need to insert your own key handling between the input method
6023  * and the default key event handling of the #GtkEntry.
6024  * See gtk_text_view_reset_im_context() for an example of use.
6025  *
6026  * Return value: %TRUE if the input method handled the key event.
6027  *
6028  * Since: 2.22
6029  */
6030 gboolean
6031 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
6032                                       GdkEventKey *event)
6033 {
6034   GtkEntryPrivate *priv;
6035
6036   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6037
6038   priv = entry->priv;
6039
6040   return gtk_im_context_filter_keypress (priv->im_context, event);
6041 }
6042
6043 GtkIMContext*
6044 _gtk_entry_get_im_context (GtkEntry *entry)
6045 {
6046   return entry->priv->im_context;
6047 }
6048
6049 static gint
6050 gtk_entry_find_position (GtkEntry *entry,
6051                          gint      x)
6052 {
6053   GtkEntryPrivate *priv = entry->priv;
6054   PangoLayout *layout;
6055   PangoLayoutLine *line;
6056   gint index;
6057   gint pos;
6058   gint trailing;
6059   const gchar *text;
6060   gint cursor_index;
6061   
6062   layout = gtk_entry_ensure_layout (entry, TRUE);
6063   text = pango_layout_get_text (layout);
6064   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
6065
6066   line = pango_layout_get_lines_readonly (layout)->data;
6067   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
6068
6069   if (index >= cursor_index && priv->preedit_length)
6070     {
6071       if (index >= cursor_index + priv->preedit_length)
6072         index -= priv->preedit_length;
6073       else
6074         {
6075           index = cursor_index;
6076           trailing = 0;
6077         }
6078     }
6079
6080   pos = g_utf8_pointer_to_offset (text, text + index);
6081   pos += trailing;
6082
6083   return pos;
6084 }
6085
6086 static void
6087 gtk_entry_get_cursor_locations (GtkEntry   *entry,
6088                                 CursorType  type,
6089                                 gint       *strong_x,
6090                                 gint       *weak_x)
6091 {
6092   GtkEntryPrivate *priv = entry->priv;
6093   DisplayMode mode = gtk_entry_get_display_mode (entry);
6094
6095   /* Nothing to display at all, so no cursor is relevant */
6096   if (mode == DISPLAY_BLANK)
6097     {
6098       if (strong_x)
6099         *strong_x = 0;
6100       
6101       if (weak_x)
6102         *weak_x = 0;
6103     }
6104   else
6105     {
6106       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
6107       const gchar *text = pango_layout_get_text (layout);
6108       PangoRectangle strong_pos, weak_pos;
6109       gint index;
6110   
6111       if (type == CURSOR_STANDARD)
6112         {
6113           index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
6114         }
6115       else /* type == CURSOR_DND */
6116         {
6117           index = g_utf8_offset_to_pointer (text, priv->dnd_position) - text;
6118
6119           if (priv->dnd_position > priv->current_pos)
6120             {
6121               if (mode == DISPLAY_NORMAL)
6122                 index += priv->preedit_length;
6123               else
6124                 {
6125                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
6126                   index += preedit_len_chars * g_unichar_to_utf8 (priv->invisible_char, NULL);
6127                 }
6128             }
6129         }
6130       
6131       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
6132       
6133       if (strong_x)
6134         *strong_x = strong_pos.x / PANGO_SCALE;
6135       
6136       if (weak_x)
6137         *weak_x = weak_pos.x / PANGO_SCALE;
6138     }
6139 }
6140
6141 static void
6142 gtk_entry_adjust_scroll (GtkEntry *entry)
6143 {
6144   GtkEntryPrivate *priv = entry->priv;
6145   gint min_offset, max_offset;
6146   gint text_area_width, text_width;
6147   GtkBorder inner_border;
6148   gint strong_x, weak_x;
6149   gint strong_xoffset, weak_xoffset;
6150   gfloat xalign;
6151   PangoLayout *layout;
6152   PangoLayoutLine *line;
6153   PangoRectangle logical_rect;
6154
6155   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6156     return;
6157
6158   _gtk_entry_effective_inner_border (entry, &inner_border);
6159
6160   text_area_width = gdk_window_get_width (priv->text_area);
6161   text_area_width -= inner_border.left + inner_border.right;
6162   if (text_area_width < 0)
6163     text_area_width = 0;
6164
6165   layout = gtk_entry_ensure_layout (entry, TRUE);
6166   line = pango_layout_get_lines_readonly (layout)->data;
6167
6168   pango_layout_line_get_extents (line, NULL, &logical_rect);
6169
6170   /* Display as much text as we can */
6171
6172   if (priv->resolved_dir == PANGO_DIRECTION_LTR)
6173       xalign = priv->xalign;
6174   else
6175       xalign = 1.0 - priv->xalign;
6176
6177   text_width = PANGO_PIXELS(logical_rect.width);
6178
6179   if (text_width > text_area_width)
6180     {
6181       min_offset = 0;
6182       max_offset = text_width - text_area_width;
6183     }
6184   else
6185     {
6186       min_offset = (text_width - text_area_width) * xalign;
6187       max_offset = min_offset;
6188     }
6189
6190   priv->scroll_offset = CLAMP (priv->scroll_offset, min_offset, max_offset);
6191
6192   /* And make sure cursors are on screen. Note that the cursor is
6193    * actually drawn one pixel into the INNER_BORDER space on
6194    * the right, when the scroll is at the utmost right. This
6195    * looks better to to me than confining the cursor inside the
6196    * border entirely, though it means that the cursor gets one
6197    * pixel closer to the edge of the widget on the right than
6198    * on the left. This might need changing if one changed
6199    * INNER_BORDER from 2 to 1, as one would do on a
6200    * small-screen-real-estate display.
6201    *
6202    * We always make sure that the strong cursor is on screen, and
6203    * put the weak cursor on screen if possible.
6204    */
6205
6206   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
6207   
6208   strong_xoffset = strong_x - priv->scroll_offset;
6209
6210   if (strong_xoffset < 0)
6211     {
6212       priv->scroll_offset += strong_xoffset;
6213       strong_xoffset = 0;
6214     }
6215   else if (strong_xoffset > text_area_width)
6216     {
6217       priv->scroll_offset += strong_xoffset - text_area_width;
6218       strong_xoffset = text_area_width;
6219     }
6220
6221   weak_xoffset = weak_x - priv->scroll_offset;
6222
6223   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
6224     {
6225       priv->scroll_offset += weak_xoffset;
6226     }
6227   else if (weak_xoffset > text_area_width &&
6228            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
6229     {
6230       priv->scroll_offset += weak_xoffset - text_area_width;
6231     }
6232
6233   g_object_notify (G_OBJECT (entry), "scroll-offset");
6234 }
6235
6236 static void
6237 gtk_entry_move_adjustments (GtkEntry *entry)
6238 {
6239   GtkWidget *widget = GTK_WIDGET (entry);
6240   GtkAllocation allocation;
6241   GtkAdjustment *adjustment;
6242   PangoContext *context;
6243   PangoFontMetrics *metrics;
6244   GtkStyleContext *style_context;
6245   GtkStateFlags state;
6246   gint x, layout_x, border_x, border_y;
6247   gint char_width;
6248
6249   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6250   if (!adjustment)
6251     return;
6252
6253   gtk_widget_get_allocation (widget, &allocation);
6254
6255   /* Cursor position, layout offset, border width, and widget allocation */
6256   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6257   get_layout_position (entry, &layout_x, NULL);
6258   _gtk_entry_get_borders (entry, &border_x, &border_y);
6259   x += allocation.x + layout_x + border_x;
6260
6261   /* Approximate width of a char, so user can see what is ahead/behind */
6262   context = gtk_widget_get_pango_context (widget);
6263   style_context = gtk_widget_get_style_context (widget);
6264   state = gtk_widget_get_state_flags (widget);
6265
6266   metrics = pango_context_get_metrics (context,
6267                                        gtk_style_context_get_font (style_context, state),
6268                                        pango_context_get_language (context));
6269   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6270
6271   /* Scroll it */
6272   gtk_adjustment_clamp_page (adjustment, 
6273                              x - (char_width + 1),   /* one char + one pixel before */
6274                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6275 }
6276
6277 static gint
6278 gtk_entry_move_visually (GtkEntry *entry,
6279                          gint      start,
6280                          gint      count)
6281 {
6282   GtkEntryPrivate *priv = entry->priv;
6283   gint index;
6284   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6285   const gchar *text;
6286
6287   text = pango_layout_get_text (layout);
6288   
6289   index = g_utf8_offset_to_pointer (text, start) - text;
6290
6291   while (count != 0)
6292     {
6293       int new_index, new_trailing;
6294       gboolean split_cursor;
6295       gboolean strong;
6296
6297       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6298                     "gtk-split-cursor", &split_cursor,
6299                     NULL);
6300
6301       if (split_cursor)
6302         strong = TRUE;
6303       else
6304         {
6305           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6306           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6307
6308           strong = keymap_direction == priv->resolved_dir;
6309         }
6310       
6311       if (count > 0)
6312         {
6313           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6314           count--;
6315         }
6316       else
6317         {
6318           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6319           count++;
6320         }
6321
6322       if (new_index < 0)
6323         index = 0;
6324       else if (new_index != G_MAXINT)
6325         index = new_index;
6326       
6327       while (new_trailing--)
6328         index = g_utf8_next_char (text + index) - text;
6329     }
6330   
6331   return g_utf8_pointer_to_offset (text, text + index);
6332 }
6333
6334 static gint
6335 gtk_entry_move_logically (GtkEntry *entry,
6336                           gint      start,
6337                           gint      count)
6338 {
6339   gint new_pos = start;
6340   guint length;
6341
6342   length = gtk_entry_buffer_get_length (get_buffer (entry));
6343
6344   /* Prevent any leak of information */
6345   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6346     {
6347       new_pos = CLAMP (start + count, 0, length);
6348     }
6349   else
6350     {
6351       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6352       PangoLogAttr *log_attrs;
6353       gint n_attrs;
6354
6355       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6356
6357       while (count > 0 && new_pos < length)
6358         {
6359           do
6360             new_pos++;
6361           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6362           
6363           count--;
6364         }
6365       while (count < 0 && new_pos > 0)
6366         {
6367           do
6368             new_pos--;
6369           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6370           
6371           count++;
6372         }
6373       
6374       g_free (log_attrs);
6375     }
6376
6377   return new_pos;
6378 }
6379
6380 static gint
6381 gtk_entry_move_forward_word (GtkEntry *entry,
6382                              gint      start,
6383                              gboolean  allow_whitespace)
6384 {
6385   gint new_pos = start;
6386   guint length;
6387
6388   length = gtk_entry_buffer_get_length (get_buffer (entry));
6389
6390   /* Prevent any leak of information */
6391   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6392     {
6393       new_pos = length;
6394     }
6395   else if (new_pos < length)
6396     {
6397       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6398       PangoLogAttr *log_attrs;
6399       gint n_attrs;
6400
6401       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6402       
6403       /* Find the next word boundary */
6404       new_pos++;
6405       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6406                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6407         new_pos++;
6408
6409       g_free (log_attrs);
6410     }
6411
6412   return new_pos;
6413 }
6414
6415
6416 static gint
6417 gtk_entry_move_backward_word (GtkEntry *entry,
6418                               gint      start,
6419                               gboolean  allow_whitespace)
6420 {
6421   gint new_pos = start;
6422
6423   /* Prevent any leak of information */
6424   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6425     {
6426       new_pos = 0;
6427     }
6428   else if (start > 0)
6429     {
6430       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6431       PangoLogAttr *log_attrs;
6432       gint n_attrs;
6433
6434       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6435
6436       new_pos = start - 1;
6437
6438       /* Find the previous word boundary */
6439       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6440                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6441         new_pos--;
6442
6443       g_free (log_attrs);
6444     }
6445
6446   return new_pos;
6447 }
6448
6449 static void
6450 gtk_entry_delete_whitespace (GtkEntry *entry)
6451 {
6452   GtkEntryPrivate *priv = entry->priv;
6453   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6454   PangoLogAttr *log_attrs;
6455   gint n_attrs;
6456   gint start, end;
6457
6458   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6459
6460   start = end = priv->current_pos;
6461   
6462   while (start > 0 && log_attrs[start-1].is_white)
6463     start--;
6464
6465   while (end < n_attrs && log_attrs[end].is_white)
6466     end++;
6467
6468   g_free (log_attrs);
6469
6470   if (start != end)
6471     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6472 }
6473
6474
6475 static void
6476 gtk_entry_select_word (GtkEntry *entry)
6477 {
6478   GtkEntryPrivate *priv = entry->priv;
6479   gint start_pos = gtk_entry_move_backward_word (entry, priv->current_pos, TRUE);
6480   gint end_pos = gtk_entry_move_forward_word (entry, priv->current_pos, TRUE);
6481
6482   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6483 }
6484
6485 static void
6486 gtk_entry_select_line (GtkEntry *entry)
6487 {
6488   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6489 }
6490
6491 static gint
6492 truncate_multiline (const gchar *text)
6493 {
6494   gint length;
6495
6496   for (length = 0;
6497        text[length] && text[length] != '\n' && text[length] != '\r';
6498        length++);
6499
6500   return length;
6501 }
6502
6503 static void
6504 paste_received (GtkClipboard *clipboard,
6505                 const gchar  *text,
6506                 gpointer      data)
6507 {
6508   GtkEntry *entry = GTK_ENTRY (data);
6509   GtkEditable *editable = GTK_EDITABLE (entry);
6510   GtkEntryPrivate *priv = entry->priv;
6511
6512   if (priv->button == 2)
6513     {
6514       gint pos, start, end;
6515       pos = priv->insert_pos;
6516       gtk_editable_get_selection_bounds (editable, &start, &end);
6517       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6518         gtk_editable_select_region (editable, pos, pos);
6519     }
6520       
6521   if (text)
6522     {
6523       gint pos, start, end;
6524       gint length = -1;
6525       gboolean popup_completion;
6526       GtkEntryCompletion *completion;
6527
6528       completion = gtk_entry_get_completion (entry);
6529
6530       if (priv->truncate_multiline)
6531         length = truncate_multiline (text);
6532
6533       /* only complete if the selection is at the end */
6534       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6535                           MAX (priv->current_pos, priv->selection_bound));
6536
6537       if (completion)
6538         {
6539           if (gtk_widget_get_mapped (completion->priv->popup_window))
6540             _gtk_entry_completion_popdown (completion);
6541
6542           if (!popup_completion && completion->priv->changed_id > 0)
6543             g_signal_handler_block (entry, completion->priv->changed_id);
6544         }
6545
6546       begin_change (entry);
6547       g_object_freeze_notify (G_OBJECT (entry));
6548       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6549         gtk_editable_delete_text (editable, start, end);
6550
6551       pos = priv->current_pos;
6552       gtk_editable_insert_text (editable, text, length, &pos);
6553       gtk_editable_set_position (editable, pos);
6554       g_object_thaw_notify (G_OBJECT (entry));
6555       end_change (entry);
6556
6557       if (completion &&
6558           !popup_completion && completion->priv->changed_id > 0)
6559         g_signal_handler_unblock (entry, completion->priv->changed_id);
6560     }
6561
6562   g_object_unref (entry);
6563 }
6564
6565 static void
6566 gtk_entry_paste (GtkEntry *entry,
6567                  GdkAtom   selection)
6568 {
6569   g_object_ref (entry);
6570   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6571                               paste_received, entry);
6572 }
6573
6574 static void
6575 primary_get_cb (GtkClipboard     *clipboard,
6576                 GtkSelectionData *selection_data,
6577                 guint             info,
6578                 gpointer          data)
6579 {
6580   GtkEntry *entry = GTK_ENTRY (data);
6581   gint start, end;
6582   
6583   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6584     {
6585       gchar *str = gtk_entry_get_display_text (entry, start, end);
6586       gtk_selection_data_set_text (selection_data, str, -1);
6587       g_free (str);
6588     }
6589 }
6590
6591 static void
6592 primary_clear_cb (GtkClipboard *clipboard,
6593                   gpointer      data)
6594 {
6595   GtkEntry *entry = GTK_ENTRY (data);
6596   GtkEntryPrivate *priv = entry->priv;
6597
6598   gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
6599 }
6600
6601 static void
6602 gtk_entry_update_primary_selection (GtkEntry *entry)
6603 {
6604   GtkTargetList *list;
6605   GtkTargetEntry *targets;
6606   GtkClipboard *clipboard;
6607   gint start, end;
6608   gint n_targets;
6609
6610   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6611     return;
6612
6613   list = gtk_target_list_new (NULL, 0);
6614   gtk_target_list_add_text_targets (list, 0);
6615
6616   targets = gtk_target_table_new_from_list (list, &n_targets);
6617
6618   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6619   
6620   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6621     {
6622       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6623                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6624         primary_clear_cb (clipboard, entry);
6625     }
6626   else
6627     {
6628       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6629         gtk_clipboard_clear (clipboard);
6630     }
6631
6632   gtk_target_table_free (targets, n_targets);
6633   gtk_target_list_unref (list);
6634 }
6635
6636 static void
6637 gtk_entry_clear (GtkEntry             *entry,
6638                  GtkEntryIconPosition  icon_pos)
6639 {
6640   GtkEntryPrivate *priv = entry->priv;
6641   EntryIconInfo *icon_info = priv->icons[icon_pos];
6642
6643   if (!icon_info || icon_info->storage_type == GTK_IMAGE_EMPTY)
6644     return;
6645
6646   g_object_freeze_notify (G_OBJECT (entry));
6647
6648   /* Explicitly check, as the pointer may become invalidated
6649    * during destruction.
6650    */
6651   if (GDK_IS_WINDOW (icon_info->window))
6652     gdk_window_hide (icon_info->window);
6653
6654   if (icon_info->pixbuf)
6655     {
6656       g_object_unref (icon_info->pixbuf);
6657       icon_info->pixbuf = NULL;
6658     }
6659
6660   switch (icon_info->storage_type)
6661     {
6662     case GTK_IMAGE_PIXBUF:
6663       g_object_notify (G_OBJECT (entry),
6664                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6665       break;
6666
6667     case GTK_IMAGE_STOCK:
6668       g_free (icon_info->stock_id);
6669       icon_info->stock_id = NULL;
6670       g_object_notify (G_OBJECT (entry),
6671                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6672       break;
6673
6674     case GTK_IMAGE_ICON_NAME:
6675       g_free (icon_info->icon_name);
6676       icon_info->icon_name = NULL;
6677       g_object_notify (G_OBJECT (entry),
6678                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6679       break;
6680
6681     case GTK_IMAGE_GICON:
6682       if (icon_info->gicon)
6683         {
6684           g_object_unref (icon_info->gicon);
6685           icon_info->gicon = NULL;
6686         }
6687       g_object_notify (G_OBJECT (entry),
6688                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6689       break;
6690
6691     default:
6692       g_assert_not_reached ();
6693       break;
6694     }
6695
6696   icon_info->storage_type = GTK_IMAGE_EMPTY;
6697   g_object_notify (G_OBJECT (entry),
6698                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6699
6700   g_object_thaw_notify (G_OBJECT (entry));
6701 }
6702
6703 static GdkPixbuf *
6704 create_normal_pixbuf (GtkStyleContext *context,
6705                       const gchar     *stock_id,
6706                       GtkIconSize      icon_size)
6707 {
6708   GtkIconSet *icon_set;
6709   GdkPixbuf *pixbuf;
6710
6711   gtk_style_context_save (context);
6712
6713   /* Unset any state */
6714   gtk_style_context_set_state (context, 0);
6715
6716   icon_set = gtk_style_context_lookup_icon_set (context, stock_id);
6717   pixbuf = gtk_icon_set_render_icon_pixbuf (icon_set, context, icon_size);
6718
6719   gtk_style_context_restore (context);
6720
6721   return pixbuf;
6722 }
6723
6724 static void
6725 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6726                          GtkEntryIconPosition  icon_pos)
6727 {
6728   GtkEntryPrivate *priv = entry->priv;
6729   EntryIconInfo *icon_info = priv->icons[icon_pos];
6730   GtkStyleContext *context;
6731   GtkIconInfo *info;
6732   GtkIconTheme *icon_theme;
6733   GtkSettings *settings;
6734   GtkWidget *widget;
6735   GdkScreen *screen;
6736   gint width, height;
6737
6738   if (!icon_info || icon_info->pixbuf)
6739     return;
6740
6741   widget = GTK_WIDGET (entry);
6742   context = gtk_widget_get_style_context (widget);
6743
6744   switch (icon_info->storage_type)
6745     {
6746     case GTK_IMAGE_EMPTY:
6747     case GTK_IMAGE_PIXBUF:
6748       break;
6749     case GTK_IMAGE_STOCK:
6750       icon_info->pixbuf = create_normal_pixbuf (context, icon_info->stock_id,
6751                                                 GTK_ICON_SIZE_MENU);
6752
6753       if (!icon_info->pixbuf)
6754         icon_info->pixbuf = create_normal_pixbuf (context,
6755                                                   GTK_STOCK_MISSING_IMAGE,
6756                                                   GTK_ICON_SIZE_MENU);
6757       break;
6758
6759     case GTK_IMAGE_ICON_NAME:
6760       screen = gtk_widget_get_screen (widget);
6761       if (screen)
6762         {
6763           icon_theme = gtk_icon_theme_get_for_screen (screen);
6764           settings = gtk_settings_get_for_screen (screen);
6765
6766           gtk_icon_size_lookup_for_settings (settings,
6767                                              GTK_ICON_SIZE_MENU,
6768                                              &width, &height);
6769
6770           icon_info->pixbuf = gtk_icon_theme_load_icon (icon_theme,
6771                                                         icon_info->icon_name,
6772                                                         MIN (width, height),
6773                                                         0, NULL);
6774
6775           if (icon_info->pixbuf == NULL)
6776             icon_info->pixbuf = create_normal_pixbuf (context,
6777                                                       GTK_STOCK_MISSING_IMAGE,
6778                                                       GTK_ICON_SIZE_MENU);
6779         }
6780       break;
6781
6782     case GTK_IMAGE_GICON:
6783       screen = gtk_widget_get_screen (widget);
6784       if (screen)
6785         {
6786           icon_theme = gtk_icon_theme_get_for_screen (screen);
6787           settings = gtk_settings_get_for_screen (screen);
6788
6789           gtk_icon_size_lookup_for_settings (settings,
6790                                              GTK_ICON_SIZE_MENU,
6791                                              &width, &height);
6792
6793           info = gtk_icon_theme_lookup_by_gicon (icon_theme,
6794                                                  icon_info->gicon,
6795                                                  MIN (width, height), 
6796                                                  GTK_ICON_LOOKUP_USE_BUILTIN);
6797           if (info)
6798             {
6799               icon_info->pixbuf = gtk_icon_info_load_icon (info, NULL);
6800               gtk_icon_info_free (info);
6801             }
6802
6803           if (icon_info->pixbuf == NULL)
6804             icon_info->pixbuf = create_normal_pixbuf (context,
6805                                                       GTK_STOCK_MISSING_IMAGE,
6806                                                       GTK_ICON_SIZE_MENU);
6807         }
6808       break;
6809
6810     default:
6811       g_assert_not_reached ();
6812       break;
6813     }
6814     
6815   if (icon_info->pixbuf != NULL && icon_info->window != NULL)
6816     gdk_window_show_unraised (icon_info->window);
6817 }
6818
6819
6820 /* Public API
6821  */
6822
6823 /**
6824  * gtk_entry_new:
6825  *
6826  * Creates a new entry.
6827  *
6828  * Return value: a new #GtkEntry.
6829  */
6830 GtkWidget*
6831 gtk_entry_new (void)
6832 {
6833   return g_object_new (GTK_TYPE_ENTRY, NULL);
6834 }
6835
6836 /**
6837  * gtk_entry_new_with_buffer:
6838  * @buffer: The buffer to use for the new #GtkEntry.
6839  *
6840  * Creates a new entry with the specified text buffer.
6841  *
6842  * Return value: a new #GtkEntry
6843  *
6844  * Since: 2.18
6845  */
6846 GtkWidget*
6847 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6848 {
6849   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6850   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6851 }
6852
6853 static GtkEntryBuffer*
6854 get_buffer (GtkEntry *entry)
6855 {
6856   GtkEntryPrivate *priv = entry->priv;
6857
6858   if (priv->buffer == NULL)
6859     {
6860       GtkEntryBuffer *buffer;
6861       buffer = gtk_entry_buffer_new (NULL, 0);
6862       gtk_entry_set_buffer (entry, buffer);
6863       g_object_unref (buffer);
6864     }
6865
6866   return priv->buffer;
6867 }
6868
6869 /**
6870  * gtk_entry_get_buffer:
6871  * @entry: a #GtkEntry
6872  *
6873  * Get the #GtkEntryBuffer object which holds the text for
6874  * this widget.
6875  *
6876  * Since: 2.18
6877  *
6878  * Returns: (transfer none): A #GtkEntryBuffer object.
6879  */
6880 GtkEntryBuffer*
6881 gtk_entry_get_buffer (GtkEntry *entry)
6882 {
6883   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6884
6885   return get_buffer (entry);
6886 }
6887
6888 /**
6889  * gtk_entry_set_buffer:
6890  * @entry: a #GtkEntry
6891  * @buffer: a #GtkEntryBuffer
6892  *
6893  * Set the #GtkEntryBuffer object which holds the text for
6894  * this widget.
6895  *
6896  * Since: 2.18
6897  */
6898 void
6899 gtk_entry_set_buffer (GtkEntry       *entry,
6900                       GtkEntryBuffer *buffer)
6901 {
6902   GtkEntryPrivate *priv;
6903   GObject *obj;
6904
6905   g_return_if_fail (GTK_IS_ENTRY (entry));
6906
6907   priv = entry->priv;
6908
6909   if (buffer)
6910     {
6911       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6912       g_object_ref (buffer);
6913     }
6914
6915   if (priv->buffer)
6916     {
6917       buffer_disconnect_signals (entry);
6918       g_object_unref (priv->buffer);
6919     }
6920
6921   priv->buffer = buffer;
6922
6923   if (priv->buffer)
6924      buffer_connect_signals (entry);
6925
6926   obj = G_OBJECT (entry);
6927   g_object_freeze_notify (obj);
6928   g_object_notify (obj, "buffer");
6929   g_object_notify (obj, "text");
6930   g_object_notify (obj, "text-length");
6931   g_object_notify (obj, "max-length");
6932   g_object_notify (obj, "visibility");
6933   g_object_notify (obj, "invisible-char");
6934   g_object_notify (obj, "invisible-char-set");
6935   g_object_thaw_notify (obj);
6936
6937   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6938   gtk_entry_recompute (entry);
6939 }
6940
6941 /**
6942  * gtk_entry_get_text_area:
6943  * @entry: a #GtkEntry
6944  * @text_area: (out): Return location for the text area.
6945  *
6946  * Gets the area where the entry's text is drawn. This function is
6947  * useful when drawing something to the entry in a draw callback.
6948  *
6949  * If the entry is not realized, @text_area is filled with zeros.
6950  *
6951  * See also gtk_entry_get_icon_area().
6952  *
6953  * Since: 3.0
6954  **/
6955 void
6956 gtk_entry_get_text_area (GtkEntry     *entry,
6957                          GdkRectangle *text_area)
6958 {
6959   GtkEntryPrivate *priv;
6960
6961   g_return_if_fail (GTK_IS_ENTRY (entry));
6962   g_return_if_fail (text_area != NULL);
6963
6964   priv = entry->priv;
6965
6966   if (priv->text_area)
6967     {
6968       GtkAllocation allocation;
6969       gint x, y;
6970
6971       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6972       gdk_window_get_position (priv->text_area, &x, &y);
6973
6974       text_area->x = x - allocation.x;
6975       text_area->y = y - allocation.y;
6976       text_area->width = gdk_window_get_width (priv->text_area);
6977       text_area->height = gdk_window_get_height (priv->text_area);
6978     }
6979   else
6980     {
6981       text_area->x = 0;
6982       text_area->y = 0;
6983       text_area->width = 0;
6984       text_area->height = 0;
6985     }
6986 }
6987
6988 /**
6989  * gtk_entry_set_text:
6990  * @entry: a #GtkEntry
6991  * @text: the new text
6992  *
6993  * Sets the text in the widget to the given
6994  * value, replacing the current contents.
6995  *
6996  * See gtk_entry_buffer_set_text().
6997  */
6998 void
6999 gtk_entry_set_text (GtkEntry    *entry,
7000                     const gchar *text)
7001 {
7002   gint tmp_pos;
7003   GtkEntryCompletion *completion;
7004
7005   g_return_if_fail (GTK_IS_ENTRY (entry));
7006   g_return_if_fail (text != NULL);
7007
7008   /* Actually setting the text will affect the cursor and selection;
7009    * if the contents don't actually change, this will look odd to the user.
7010    */
7011   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
7012     return;
7013
7014   completion = gtk_entry_get_completion (entry);
7015   if (completion && completion->priv->changed_id > 0)
7016     g_signal_handler_block (entry, completion->priv->changed_id);
7017
7018   begin_change (entry);
7019   g_object_freeze_notify (G_OBJECT (entry));
7020   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
7021   tmp_pos = 0;
7022   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
7023   g_object_thaw_notify (G_OBJECT (entry));
7024   end_change (entry);
7025
7026   if (completion && completion->priv->changed_id > 0)
7027     g_signal_handler_unblock (entry, completion->priv->changed_id);
7028 }
7029
7030 /**
7031  * gtk_entry_set_visibility:
7032  * @entry: a #GtkEntry
7033  * @visible: %TRUE if the contents of the entry are displayed
7034  *           as plaintext
7035  *
7036  * Sets whether the contents of the entry are visible or not. 
7037  * When visibility is set to %FALSE, characters are displayed 
7038  * as the invisible char, and will also appear that way when 
7039  * the text in the entry widget is copied elsewhere.
7040  *
7041  * By default, GTK+ picks the best invisible character available
7042  * in the current font, but it can be changed with
7043  * gtk_entry_set_invisible_char().
7044  */
7045 void
7046 gtk_entry_set_visibility (GtkEntry *entry,
7047                           gboolean visible)
7048 {
7049   GtkEntryPrivate *priv;
7050
7051   g_return_if_fail (GTK_IS_ENTRY (entry));
7052
7053   priv = entry->priv;
7054
7055   visible = visible != FALSE;
7056
7057   if (priv->visible != visible)
7058     {
7059       priv->visible = visible;
7060
7061       g_object_notify (G_OBJECT (entry), "visibility");
7062       gtk_entry_recompute (entry);
7063     }
7064 }
7065
7066 /**
7067  * gtk_entry_get_visibility:
7068  * @entry: a #GtkEntry
7069  *
7070  * Retrieves whether the text in @entry is visible. See
7071  * gtk_entry_set_visibility().
7072  *
7073  * Return value: %TRUE if the text is currently visible
7074  **/
7075 gboolean
7076 gtk_entry_get_visibility (GtkEntry *entry)
7077 {
7078   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7079
7080   return entry->priv->visible;
7081 }
7082
7083 /**
7084  * gtk_entry_set_invisible_char:
7085  * @entry: a #GtkEntry
7086  * @ch: a Unicode character
7087  * 
7088  * Sets the character to use in place of the actual text when
7089  * gtk_entry_set_visibility() has been called to set text visibility
7090  * to %FALSE. i.e. this is the character used in "password mode" to
7091  * show the user how many characters have been typed. By default, GTK+
7092  * picks the best invisible char available in the current font. If you
7093  * set the invisible char to 0, then the user will get no feedback
7094  * at all; there will be no text on the screen as they type.
7095  **/
7096 void
7097 gtk_entry_set_invisible_char (GtkEntry *entry,
7098                               gunichar  ch)
7099 {
7100   GtkEntryPrivate *priv;
7101
7102   g_return_if_fail (GTK_IS_ENTRY (entry));
7103
7104   priv = entry->priv;
7105
7106   if (!priv->invisible_char_set)
7107     {
7108       priv->invisible_char_set = TRUE;
7109       g_object_notify (G_OBJECT (entry), "invisible-char-set");
7110     }
7111
7112   if (ch == priv->invisible_char)
7113     return;
7114
7115   priv->invisible_char = ch;
7116   g_object_notify (G_OBJECT (entry), "invisible-char");
7117   gtk_entry_recompute (entry);  
7118 }
7119
7120 /**
7121  * gtk_entry_get_invisible_char:
7122  * @entry: a #GtkEntry
7123  *
7124  * Retrieves the character displayed in place of the real characters
7125  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
7126  *
7127  * Return value: the current invisible char, or 0, if the entry does not
7128  *               show invisible text at all. 
7129  **/
7130 gunichar
7131 gtk_entry_get_invisible_char (GtkEntry *entry)
7132 {
7133   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7134
7135   return entry->priv->invisible_char;
7136 }
7137
7138 /**
7139  * gtk_entry_unset_invisible_char:
7140  * @entry: a #GtkEntry
7141  *
7142  * Unsets the invisible char previously set with
7143  * gtk_entry_set_invisible_char(). So that the
7144  * default invisible char is used again.
7145  *
7146  * Since: 2.16
7147  **/
7148 void
7149 gtk_entry_unset_invisible_char (GtkEntry *entry)
7150 {
7151   GtkEntryPrivate *priv;
7152   gunichar ch;
7153
7154   g_return_if_fail (GTK_IS_ENTRY (entry));
7155
7156   priv = entry->priv;
7157
7158   if (!priv->invisible_char_set)
7159     return;
7160
7161   priv->invisible_char_set = FALSE;
7162   ch = find_invisible_char (GTK_WIDGET (entry));
7163
7164   if (priv->invisible_char != ch)
7165     {
7166       priv->invisible_char = ch;
7167       g_object_notify (G_OBJECT (entry), "invisible-char");
7168     }
7169
7170   g_object_notify (G_OBJECT (entry), "invisible-char-set");
7171   gtk_entry_recompute (entry);
7172 }
7173
7174 /**
7175  * gtk_entry_set_overwrite_mode:
7176  * @entry: a #GtkEntry
7177  * @overwrite: new value
7178  * 
7179  * Sets whether the text is overwritten when typing in the #GtkEntry.
7180  *
7181  * Since: 2.14
7182  **/
7183 void
7184 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7185                               gboolean  overwrite)
7186 {
7187   GtkEntryPrivate *priv = entry->priv;
7188
7189   g_return_if_fail (GTK_IS_ENTRY (entry));
7190
7191   if (priv->overwrite_mode == overwrite)
7192     return;
7193   
7194   gtk_entry_toggle_overwrite (entry);
7195
7196   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7197 }
7198
7199 /**
7200  * gtk_entry_get_overwrite_mode:
7201  * @entry: a #GtkEntry
7202  * 
7203  * Gets the value set by gtk_entry_set_overwrite_mode().
7204  * 
7205  * Return value: whether the text is overwritten when typing.
7206  *
7207  * Since: 2.14
7208  **/
7209 gboolean
7210 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7211 {
7212   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7213
7214   return entry->priv->overwrite_mode;
7215 }
7216
7217 /**
7218  * gtk_entry_get_text:
7219  * @entry: a #GtkEntry
7220  *
7221  * Retrieves the contents of the entry widget.
7222  * See also gtk_editable_get_chars().
7223  *
7224  * This is equivalent to:
7225  *
7226  * <informalexample><programlisting>
7227  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7228  * </programlisting></informalexample>
7229  *
7230  * Return value: a pointer to the contents of the widget as a
7231  *      string. This string points to internally allocated
7232  *      storage in the widget and must not be freed, modified or
7233  *      stored.
7234  **/
7235 G_CONST_RETURN gchar*
7236 gtk_entry_get_text (GtkEntry *entry)
7237 {
7238   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7239
7240   return gtk_entry_buffer_get_text (get_buffer (entry));
7241 }
7242
7243 /**
7244  * gtk_entry_set_max_length:
7245  * @entry: a #GtkEntry
7246  * @max: the maximum length of the entry, or 0 for no maximum.
7247  *   (other than the maximum length of entries.) The value passed in will
7248  *   be clamped to the range 0-65536.
7249  * 
7250  * Sets the maximum allowed length of the contents of the widget. If
7251  * the current contents are longer than the given length, then they
7252  * will be truncated to fit.
7253  *
7254  * This is equivalent to:
7255  *
7256  * <informalexample><programlisting>
7257  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7258  * </programlisting></informalexample>
7259  **/
7260 void
7261 gtk_entry_set_max_length (GtkEntry     *entry,
7262                           gint          max)
7263 {
7264   g_return_if_fail (GTK_IS_ENTRY (entry));
7265   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7266 }
7267
7268 /**
7269  * gtk_entry_get_max_length:
7270  * @entry: a #GtkEntry
7271  *
7272  * Retrieves the maximum allowed length of the text in
7273  * @entry. See gtk_entry_set_max_length().
7274  *
7275  * This is equivalent to:
7276  *
7277  * <informalexample><programlisting>
7278  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7279  * </programlisting></informalexample>
7280  *
7281  * Return value: the maximum allowed number of characters
7282  *               in #GtkEntry, or 0 if there is no maximum.
7283  **/
7284 gint
7285 gtk_entry_get_max_length (GtkEntry *entry)
7286 {
7287   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7288
7289   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7290 }
7291
7292 /**
7293  * gtk_entry_get_text_length:
7294  * @entry: a #GtkEntry
7295  *
7296  * Retrieves the current length of the text in
7297  * @entry. 
7298  *
7299  * This is equivalent to:
7300  *
7301  * <informalexample><programlisting>
7302  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7303  * </programlisting></informalexample>
7304  *
7305  * Return value: the current number of characters
7306  *               in #GtkEntry, or 0 if there are none.
7307  *
7308  * Since: 2.14
7309  **/
7310 guint16
7311 gtk_entry_get_text_length (GtkEntry *entry)
7312 {
7313   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7314
7315   return gtk_entry_buffer_get_length (get_buffer (entry));
7316 }
7317
7318 /**
7319  * gtk_entry_set_activates_default:
7320  * @entry: a #GtkEntry
7321  * @setting: %TRUE to activate window's default widget on Enter keypress
7322  *
7323  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7324  * widget for the window containing the entry. This usually means that
7325  * the dialog box containing the entry will be closed, since the default
7326  * widget is usually one of the dialog buttons.
7327  *
7328  * (For experts: if @setting is %TRUE, the entry calls
7329  * gtk_window_activate_default() on the window containing the entry, in
7330  * the default handler for the #GtkWidget::activate signal.)
7331  **/
7332 void
7333 gtk_entry_set_activates_default (GtkEntry *entry,
7334                                  gboolean  setting)
7335 {
7336   GtkEntryPrivate *priv;
7337
7338   g_return_if_fail (GTK_IS_ENTRY (entry));
7339
7340   priv = entry->priv;
7341
7342   setting = setting != FALSE;
7343
7344   if (setting != priv->activates_default)
7345     {
7346       priv->activates_default = setting;
7347       g_object_notify (G_OBJECT (entry), "activates-default");
7348     }
7349 }
7350
7351 /**
7352  * gtk_entry_get_activates_default:
7353  * @entry: a #GtkEntry
7354  * 
7355  * Retrieves the value set by gtk_entry_set_activates_default().
7356  * 
7357  * Return value: %TRUE if the entry will activate the default widget
7358  **/
7359 gboolean
7360 gtk_entry_get_activates_default (GtkEntry *entry)
7361 {
7362   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7363
7364   return entry->priv->activates_default;
7365 }
7366
7367 /**
7368  * gtk_entry_set_width_chars:
7369  * @entry: a #GtkEntry
7370  * @n_chars: width in chars
7371  *
7372  * Changes the size request of the entry to be about the right size
7373  * for @n_chars characters. Note that it changes the size
7374  * <emphasis>request</emphasis>, the size can still be affected by
7375  * how you pack the widget into containers. If @n_chars is -1, the
7376  * size reverts to the default entry size.
7377  **/
7378 void
7379 gtk_entry_set_width_chars (GtkEntry *entry,
7380                            gint      n_chars)
7381 {
7382   GtkEntryPrivate *priv;
7383
7384   g_return_if_fail (GTK_IS_ENTRY (entry));
7385
7386   priv = entry->priv;
7387
7388   if (priv->width_chars != n_chars)
7389     {
7390       priv->width_chars = n_chars;
7391       g_object_notify (G_OBJECT (entry), "width-chars");
7392       gtk_widget_queue_resize (GTK_WIDGET (entry));
7393     }
7394 }
7395
7396 /**
7397  * gtk_entry_get_width_chars:
7398  * @entry: a #GtkEntry
7399  * 
7400  * Gets the value set by gtk_entry_set_width_chars().
7401  * 
7402  * Return value: number of chars to request space for, or negative if unset
7403  **/
7404 gint
7405 gtk_entry_get_width_chars (GtkEntry *entry)
7406 {
7407   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7408
7409   return entry->priv->width_chars;
7410 }
7411
7412 /**
7413  * gtk_entry_set_has_frame:
7414  * @entry: a #GtkEntry
7415  * @setting: new value
7416  * 
7417  * Sets whether the entry has a beveled frame around it.
7418  **/
7419 void
7420 gtk_entry_set_has_frame (GtkEntry *entry,
7421                          gboolean  setting)
7422 {
7423   GtkEntryPrivate *priv;
7424
7425   g_return_if_fail (GTK_IS_ENTRY (entry));
7426
7427   priv = entry->priv;
7428
7429   setting = (setting != FALSE);
7430
7431   if (priv->has_frame == setting)
7432     return;
7433
7434   gtk_widget_queue_resize (GTK_WIDGET (entry));
7435   priv->has_frame = setting;
7436   g_object_notify (G_OBJECT (entry), "has-frame");
7437 }
7438
7439 /**
7440  * gtk_entry_get_has_frame:
7441  * @entry: a #GtkEntry
7442  * 
7443  * Gets the value set by gtk_entry_set_has_frame().
7444  * 
7445  * Return value: whether the entry has a beveled frame
7446  **/
7447 gboolean
7448 gtk_entry_get_has_frame (GtkEntry *entry)
7449 {
7450   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7451
7452   return entry->priv->has_frame;
7453 }
7454
7455 /**
7456  * gtk_entry_set_inner_border:
7457  * @entry: a #GtkEntry
7458  * @border: (allow-none): a #GtkBorder, or %NULL
7459  *
7460  * Sets %entry's inner-border property to %border, or clears it if %NULL
7461  * is passed. The inner-border is the area around the entry's text, but
7462  * inside its frame.
7463  *
7464  * If set, this property overrides the inner-border style property.
7465  * Overriding the style-provided border is useful when you want to do
7466  * in-place editing of some text in a canvas or list widget, where
7467  * pixel-exact positioning of the entry is important.
7468  *
7469  * Since: 2.10
7470  **/
7471 void
7472 gtk_entry_set_inner_border (GtkEntry        *entry,
7473                             const GtkBorder *border)
7474 {
7475   g_return_if_fail (GTK_IS_ENTRY (entry));
7476
7477   gtk_widget_queue_resize (GTK_WIDGET (entry));
7478
7479   if (border)
7480     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7481                              gtk_border_copy (border),
7482                              (GDestroyNotify) gtk_border_free);
7483   else
7484     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7485
7486   g_object_notify (G_OBJECT (entry), "inner-border");
7487 }
7488
7489 /**
7490  * gtk_entry_get_inner_border:
7491  * @entry: a #GtkEntry
7492  *
7493  * This function returns the entry's #GtkEntry:inner-border property. See
7494  * gtk_entry_set_inner_border() for more information.
7495  *
7496  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7497  *
7498  * Since: 2.10
7499  **/
7500 G_CONST_RETURN GtkBorder *
7501 gtk_entry_get_inner_border (GtkEntry *entry)
7502 {
7503   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7504
7505   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7506 }
7507
7508 /**
7509  * gtk_entry_get_layout:
7510  * @entry: a #GtkEntry
7511  * 
7512  * Gets the #PangoLayout used to display the entry.
7513  * The layout is useful to e.g. convert text positions to
7514  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7515  * The returned layout is owned by the entry and must not be 
7516  * modified or freed by the caller.
7517  *
7518  * Keep in mind that the layout text may contain a preedit string, so
7519  * gtk_entry_layout_index_to_text_index() and
7520  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7521  * indices in the layout to byte indices in the entry contents.
7522  *
7523  * Return value: (transfer none): the #PangoLayout for this entry
7524  **/
7525 PangoLayout*
7526 gtk_entry_get_layout (GtkEntry *entry)
7527 {
7528   PangoLayout *layout;
7529   
7530   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7531
7532   layout = gtk_entry_ensure_layout (entry, TRUE);
7533
7534   return layout;
7535 }
7536
7537
7538 /**
7539  * gtk_entry_layout_index_to_text_index:
7540  * @entry: a #GtkEntry
7541  * @layout_index: byte index into the entry layout text
7542  * 
7543  * Converts from a position in the entry contents (returned
7544  * by gtk_entry_get_text()) to a position in the
7545  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7546  * with text retrieved via pango_layout_get_text()).
7547  * 
7548  * Return value: byte index into the entry contents
7549  **/
7550 gint
7551 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7552                                       gint      layout_index)
7553 {
7554   GtkEntryPrivate *priv;
7555   PangoLayout *layout;
7556   const gchar *text;
7557   gint cursor_index;
7558   
7559   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7560
7561   priv = entry->priv;
7562
7563   layout = gtk_entry_ensure_layout (entry, TRUE);
7564   text = pango_layout_get_text (layout);
7565   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7566
7567   if (layout_index >= cursor_index && priv->preedit_length)
7568     {
7569       if (layout_index >= cursor_index + priv->preedit_length)
7570         layout_index -= priv->preedit_length;
7571       else
7572         layout_index = cursor_index;
7573     }
7574
7575   return layout_index;
7576 }
7577
7578 /**
7579  * gtk_entry_text_index_to_layout_index:
7580  * @entry: a #GtkEntry
7581  * @text_index: byte index into the entry contents
7582  * 
7583  * Converts from a position in the entry's #PangoLayout (returned by
7584  * gtk_entry_get_layout()) to a position in the entry contents
7585  * (returned by gtk_entry_get_text()).
7586  * 
7587  * Return value: byte index into the entry layout text
7588  **/
7589 gint
7590 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7591                                       gint      text_index)
7592 {
7593   GtkEntryPrivate *priv;
7594   PangoLayout *layout;
7595   const gchar *text;
7596   gint cursor_index;
7597
7598   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7599
7600   priv = entry->priv;
7601
7602   layout = gtk_entry_ensure_layout (entry, TRUE);
7603   text = pango_layout_get_text (layout);
7604   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7605
7606   if (text_index > cursor_index)
7607     text_index += priv->preedit_length;
7608
7609   return text_index;
7610 }
7611
7612 /**
7613  * gtk_entry_get_layout_offsets:
7614  * @entry: a #GtkEntry
7615  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
7616  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
7617  *
7618  *
7619  * Obtains the position of the #PangoLayout used to render text
7620  * in the entry, in widget coordinates. Useful if you want to line
7621  * up the text in an entry with some other text, e.g. when using the
7622  * entry to implement editable cells in a sheet widget.
7623  *
7624  * Also useful to convert mouse events into coordinates inside the
7625  * #PangoLayout, e.g. to take some action if some part of the entry text
7626  * is clicked.
7627  * 
7628  * Note that as the user scrolls around in the entry the offsets will
7629  * change; you'll need to connect to the "notify::scroll-offset"
7630  * signal to track this. Remember when using the #PangoLayout
7631  * functions you need to convert to and from pixels using
7632  * PANGO_PIXELS() or #PANGO_SCALE.
7633  *
7634  * Keep in mind that the layout text may contain a preedit string, so
7635  * gtk_entry_layout_index_to_text_index() and
7636  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7637  * indices in the layout to byte indices in the entry contents.
7638  **/
7639 void
7640 gtk_entry_get_layout_offsets (GtkEntry *entry,
7641                               gint     *x,
7642                               gint     *y)
7643 {
7644   gint text_area_x, text_area_y;
7645   
7646   g_return_if_fail (GTK_IS_ENTRY (entry));
7647
7648   /* this gets coords relative to text area */
7649   get_layout_position (entry, x, y);
7650
7651   /* convert to widget coords */
7652   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7653   
7654   if (x)
7655     *x += text_area_x;
7656
7657   if (y)
7658     *y += text_area_y;
7659 }
7660
7661
7662 /**
7663  * gtk_entry_set_alignment:
7664  * @entry: a #GtkEntry
7665  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7666  *          Reversed for RTL layouts
7667  * 
7668  * Sets the alignment for the contents of the entry. This controls
7669  * the horizontal positioning of the contents when the displayed
7670  * text is shorter than the width of the entry.
7671  *
7672  * Since: 2.4
7673  **/
7674 void
7675 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7676 {
7677   GtkEntryPrivate *priv;
7678
7679   g_return_if_fail (GTK_IS_ENTRY (entry));
7680
7681   priv = entry->priv;
7682
7683   if (xalign < 0.0)
7684     xalign = 0.0;
7685   else if (xalign > 1.0)
7686     xalign = 1.0;
7687
7688   if (xalign != priv->xalign)
7689     {
7690       priv->xalign = xalign;
7691
7692       gtk_entry_recompute (entry);
7693
7694       g_object_notify (G_OBJECT (entry), "xalign");
7695     }
7696 }
7697
7698 /**
7699  * gtk_entry_get_alignment:
7700  * @entry: a #GtkEntry
7701  * 
7702  * Gets the value set by gtk_entry_set_alignment().
7703  * 
7704  * Return value: the alignment
7705  *
7706  * Since: 2.4
7707  **/
7708 gfloat
7709 gtk_entry_get_alignment (GtkEntry *entry)
7710 {
7711   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7712
7713   return entry->priv->xalign;
7714 }
7715
7716 /**
7717  * gtk_entry_set_icon_from_pixbuf:
7718  * @entry: a #GtkEntry
7719  * @icon_pos: Icon position
7720  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7721  *
7722  * Sets the icon shown in the specified position using a pixbuf.
7723  *
7724  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7725  *
7726  * Since: 2.16
7727  */
7728 void
7729 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7730                                 GtkEntryIconPosition  icon_pos,
7731                                 GdkPixbuf            *pixbuf)
7732 {
7733   GtkEntryPrivate *priv;
7734   EntryIconInfo *icon_info;
7735
7736   g_return_if_fail (GTK_IS_ENTRY (entry));
7737   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7738
7739   priv = entry->priv;
7740
7741   if ((icon_info = priv->icons[icon_pos]) == NULL)
7742     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7743
7744   g_object_freeze_notify (G_OBJECT (entry));
7745
7746   if (pixbuf)
7747     g_object_ref (pixbuf);
7748
7749   gtk_entry_clear (entry, icon_pos);
7750
7751   if (pixbuf)
7752     {
7753       icon_info->storage_type = GTK_IMAGE_PIXBUF;
7754       icon_info->pixbuf = pixbuf;
7755
7756       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7757         {
7758           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7759           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7760         }
7761       else
7762         {
7763           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7764           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7765         }
7766
7767       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7768           gdk_window_show_unraised (icon_info->window);
7769     }
7770
7771   gtk_entry_ensure_pixbuf (entry, icon_pos);
7772   
7773   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7774     gtk_widget_queue_resize (GTK_WIDGET (entry));
7775
7776   g_object_thaw_notify (G_OBJECT (entry));
7777 }
7778
7779 /**
7780  * gtk_entry_set_icon_from_stock:
7781  * @entry: A #GtkEntry
7782  * @icon_pos: Icon position
7783  * @stock_id: (allow-none): The name of the stock item, or %NULL
7784  *
7785  * Sets the icon shown in the entry at the specified position from
7786  * a stock image.
7787  *
7788  * If @stock_id is %NULL, no icon will be shown in the specified position.
7789  *
7790  * Since: 2.16
7791  */
7792 void
7793 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7794                                GtkEntryIconPosition  icon_pos,
7795                                const gchar          *stock_id)
7796 {
7797   GtkEntryPrivate *priv;
7798   EntryIconInfo *icon_info;
7799   gchar *new_id;
7800
7801   g_return_if_fail (GTK_IS_ENTRY (entry));
7802   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7803
7804   priv = entry->priv;
7805
7806   if ((icon_info = priv->icons[icon_pos]) == NULL)
7807     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7808
7809   g_object_freeze_notify (G_OBJECT (entry));
7810
7811   /* need to dup before clearing */
7812   new_id = g_strdup (stock_id);
7813
7814   gtk_entry_clear (entry, icon_pos);
7815
7816   if (new_id != NULL)
7817     {
7818       icon_info->storage_type = GTK_IMAGE_STOCK;
7819       icon_info->stock_id = new_id;
7820
7821       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7822         {
7823           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7824           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7825         }
7826       else
7827         {
7828           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7829           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7830         }
7831
7832       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7833           gdk_window_show_unraised (icon_info->window);
7834     }
7835
7836   gtk_entry_ensure_pixbuf (entry, icon_pos);
7837
7838   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7839     gtk_widget_queue_resize (GTK_WIDGET (entry));
7840
7841   g_object_thaw_notify (G_OBJECT (entry));
7842 }
7843
7844 /**
7845  * gtk_entry_set_icon_from_icon_name:
7846  * @entry: A #GtkEntry
7847  * @icon_pos: The position at which to set the icon
7848  * @icon_name: (allow-none): An icon name, or %NULL
7849  *
7850  * Sets the icon shown in the entry at the specified position
7851  * from the current icon theme.
7852  *
7853  * If the icon name isn't known, a "broken image" icon will be displayed
7854  * instead.
7855  *
7856  * If @icon_name is %NULL, no icon will be shown in the specified position.
7857  *
7858  * Since: 2.16
7859  */
7860 void
7861 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7862                                    GtkEntryIconPosition  icon_pos,
7863                                    const gchar          *icon_name)
7864 {
7865   GtkEntryPrivate *priv;
7866   EntryIconInfo *icon_info;
7867   gchar *new_name;
7868
7869   g_return_if_fail (GTK_IS_ENTRY (entry));
7870   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7871
7872   priv = entry->priv;
7873
7874   if ((icon_info = priv->icons[icon_pos]) == NULL)
7875     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7876
7877   g_object_freeze_notify (G_OBJECT (entry));
7878
7879   /* need to dup before clearing */
7880   new_name = g_strdup (icon_name);
7881
7882   gtk_entry_clear (entry, icon_pos);
7883
7884   if (new_name != NULL)
7885     {
7886       icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7887       icon_info->icon_name = new_name;
7888
7889       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7890         {
7891           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7892           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7893         }
7894       else
7895         {
7896           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7897           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7898         }
7899
7900       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7901           gdk_window_show_unraised (icon_info->window);
7902     }
7903
7904   gtk_entry_ensure_pixbuf (entry, icon_pos);
7905
7906   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7907     gtk_widget_queue_resize (GTK_WIDGET (entry));
7908
7909   g_object_thaw_notify (G_OBJECT (entry));
7910 }
7911
7912 /**
7913  * gtk_entry_set_icon_from_gicon:
7914  * @entry: A #GtkEntry
7915  * @icon_pos: The position at which to set the icon
7916  * @icon: (allow-none): The icon to set, or %NULL
7917  *
7918  * Sets the icon shown in the entry at the specified position
7919  * from the current icon theme.
7920  * If the icon isn't known, a "broken image" icon will be displayed
7921  * instead.
7922  *
7923  * If @icon is %NULL, no icon will be shown in the specified position.
7924  *
7925  * Since: 2.16
7926  */
7927 void
7928 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7929                                GtkEntryIconPosition  icon_pos,
7930                                GIcon                *icon)
7931 {
7932   GtkEntryPrivate *priv;
7933   EntryIconInfo *icon_info;
7934
7935   g_return_if_fail (GTK_IS_ENTRY (entry));
7936   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7937
7938   priv = entry->priv;
7939
7940   if ((icon_info = priv->icons[icon_pos]) == NULL)
7941     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7942
7943   g_object_freeze_notify (G_OBJECT (entry));
7944
7945   /* need to ref before clearing */
7946   if (icon)
7947     g_object_ref (icon);
7948
7949   gtk_entry_clear (entry, icon_pos);
7950
7951   if (icon)
7952     {
7953       icon_info->storage_type = GTK_IMAGE_GICON;
7954       icon_info->gicon = icon;
7955
7956       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7957         {
7958           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7959           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7960         }
7961       else
7962         {
7963           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7964           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7965         }
7966
7967       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7968           gdk_window_show_unraised (icon_info->window);
7969     }
7970
7971   gtk_entry_ensure_pixbuf (entry, icon_pos);
7972
7973   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7974     gtk_widget_queue_resize (GTK_WIDGET (entry));
7975
7976   g_object_thaw_notify (G_OBJECT (entry));
7977 }
7978
7979 /**
7980  * gtk_entry_set_icon_activatable:
7981  * @entry: A #GtkEntry
7982  * @icon_pos: Icon position
7983  * @activatable: %TRUE if the icon should be activatable
7984  *
7985  * Sets whether the icon is activatable.
7986  *
7987  * Since: 2.16
7988  */
7989 void
7990 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7991                                 GtkEntryIconPosition  icon_pos,
7992                                 gboolean              activatable)
7993 {
7994   GtkEntryPrivate *priv;
7995   EntryIconInfo *icon_info;
7996
7997   g_return_if_fail (GTK_IS_ENTRY (entry));
7998   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7999
8000   priv = entry->priv;
8001
8002   if ((icon_info = priv->icons[icon_pos]) == NULL)
8003     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8004
8005   activatable = activatable != FALSE;
8006
8007   if (icon_info->nonactivatable != !activatable)
8008     {
8009       icon_info->nonactivatable = !activatable;
8010
8011       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8012         update_cursors (GTK_WIDGET (entry));
8013
8014       g_object_notify (G_OBJECT (entry),
8015                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
8016     }
8017 }
8018
8019 /**
8020  * gtk_entry_get_icon_activatable:
8021  * @entry: a #GtkEntry
8022  * @icon_pos: Icon position
8023  *
8024  * Returns whether the icon is activatable.
8025  *
8026  * Returns: %TRUE if the icon is activatable.
8027  *
8028  * Since: 2.16
8029  */
8030 gboolean
8031 gtk_entry_get_icon_activatable (GtkEntry             *entry,
8032                                 GtkEntryIconPosition  icon_pos)
8033 {
8034   GtkEntryPrivate *priv;
8035   EntryIconInfo *icon_info;
8036
8037   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
8038   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
8039
8040   priv = entry->priv;
8041   icon_info = priv->icons[icon_pos];
8042
8043   return (!icon_info || !icon_info->nonactivatable);
8044 }
8045
8046 /**
8047  * gtk_entry_get_icon_pixbuf:
8048  * @entry: A #GtkEntry
8049  * @icon_pos: Icon position
8050  *
8051  * Retrieves the image used for the icon.
8052  *
8053  * Unlike the other methods of setting and getting icon data, this
8054  * method will work regardless of whether the icon was set using a
8055  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
8056  *
8057  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
8058  *     set for this position.
8059  *
8060  * Since: 2.16
8061  */
8062 GdkPixbuf *
8063 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
8064                            GtkEntryIconPosition  icon_pos)
8065 {
8066   GtkEntryPrivate *priv;
8067   EntryIconInfo *icon_info;
8068
8069   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8070   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8071
8072   priv = entry->priv;
8073
8074   icon_info = priv->icons[icon_pos];
8075
8076   if (!icon_info)
8077     return NULL;
8078
8079   gtk_entry_ensure_pixbuf (entry, icon_pos);
8080
8081   return icon_info->pixbuf;
8082 }
8083
8084 /**
8085  * gtk_entry_get_icon_gicon:
8086  * @entry: A #GtkEntry
8087  * @icon_pos: Icon position
8088  *
8089  * Retrieves the #GIcon used for the icon, or %NULL if there is
8090  * no icon or if the icon was set by some other method (e.g., by
8091  * stock, pixbuf, or icon name).
8092  *
8093  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
8094  *     or if the icon is not a #GIcon
8095  *
8096  * Since: 2.16
8097  */
8098 GIcon *
8099 gtk_entry_get_icon_gicon (GtkEntry             *entry,
8100                           GtkEntryIconPosition  icon_pos)
8101 {
8102   GtkEntryPrivate *priv;
8103   EntryIconInfo *icon_info;
8104
8105   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8106   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8107
8108   priv = entry->priv;
8109   icon_info = priv->icons[icon_pos];
8110
8111   if (!icon_info)
8112     return NULL;
8113
8114   return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
8115 }
8116
8117 /**
8118  * gtk_entry_get_icon_stock:
8119  * @entry: A #GtkEntry
8120  * @icon_pos: Icon position
8121  *
8122  * Retrieves the stock id used for the icon, or %NULL if there is
8123  * no icon or if the icon was set by some other method (e.g., by
8124  * pixbuf, icon name or gicon).
8125  *
8126  * Returns: A stock id, or %NULL if no icon is set or if the icon
8127  *          wasn't set from a stock id
8128  *
8129  * Since: 2.16
8130  */
8131 const gchar *
8132 gtk_entry_get_icon_stock (GtkEntry             *entry,
8133                           GtkEntryIconPosition  icon_pos)
8134 {
8135   GtkEntryPrivate *priv;
8136   EntryIconInfo *icon_info;
8137
8138   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8139   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8140
8141   priv = entry->priv;
8142   icon_info = priv->icons[icon_pos];
8143
8144   if (!icon_info)
8145     return NULL;
8146
8147   return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
8148 }
8149
8150 /**
8151  * gtk_entry_get_icon_name:
8152  * @entry: A #GtkEntry
8153  * @icon_pos: Icon position
8154  *
8155  * Retrieves the icon name used for the icon, or %NULL if there is
8156  * no icon or if the icon was set by some other method (e.g., by
8157  * pixbuf, stock or gicon).
8158  *
8159  * Returns: An icon name, or %NULL if no icon is set or if the icon
8160  *          wasn't set from an icon name
8161  *
8162  * Since: 2.16
8163  */
8164 const gchar *
8165 gtk_entry_get_icon_name (GtkEntry             *entry,
8166                          GtkEntryIconPosition  icon_pos)
8167 {
8168   GtkEntryPrivate *priv;
8169   EntryIconInfo *icon_info;
8170
8171   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8172   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8173
8174   priv = entry->priv;
8175   icon_info = priv->icons[icon_pos];
8176
8177   if (!icon_info)
8178     return NULL;
8179
8180   return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
8181 }
8182
8183 /**
8184  * gtk_entry_set_icon_sensitive:
8185  * @entry: A #GtkEntry
8186  * @icon_pos: Icon position
8187  * @sensitive: Specifies whether the icon should appear
8188  *             sensitive or insensitive
8189  *
8190  * Sets the sensitivity for the specified icon.
8191  *
8192  * Since: 2.16
8193  */
8194 void
8195 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8196                               GtkEntryIconPosition  icon_pos,
8197                               gboolean              sensitive)
8198 {
8199   GtkEntryPrivate *priv;
8200   EntryIconInfo *icon_info;
8201
8202   g_return_if_fail (GTK_IS_ENTRY (entry));
8203   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8204
8205   priv = entry->priv;
8206
8207   if ((icon_info = priv->icons[icon_pos]) == NULL)
8208     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8209
8210   if (icon_info->insensitive != !sensitive)
8211     {
8212       icon_info->insensitive = !sensitive;
8213
8214       icon_info->pressed = FALSE;
8215       icon_info->prelight = FALSE;
8216
8217       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8218         update_cursors (GTK_WIDGET (entry));
8219
8220       gtk_widget_queue_draw (GTK_WIDGET (entry));
8221
8222       g_object_notify (G_OBJECT (entry),
8223                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8224     }
8225 }
8226
8227 /**
8228  * gtk_entry_get_icon_sensitive:
8229  * @entry: a #GtkEntry
8230  * @icon_pos: Icon position
8231  *
8232  * Returns whether the icon appears sensitive or insensitive.
8233  *
8234  * Returns: %TRUE if the icon is sensitive.
8235  *
8236  * Since: 2.16
8237  */
8238 gboolean
8239 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8240                               GtkEntryIconPosition  icon_pos)
8241 {
8242   GtkEntryPrivate *priv;
8243   EntryIconInfo *icon_info;
8244
8245   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8246   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8247
8248   priv = entry->priv;
8249
8250   icon_info = priv->icons[icon_pos];
8251
8252   return (!icon_info || !icon_info->insensitive);
8253
8254 }
8255
8256 /**
8257  * gtk_entry_get_icon_storage_type:
8258  * @entry: a #GtkEntry
8259  * @icon_pos: Icon position
8260  *
8261  * Gets the type of representation being used by the icon
8262  * to store image data. If the icon has no image data,
8263  * the return value will be %GTK_IMAGE_EMPTY.
8264  *
8265  * Return value: image representation being used
8266  *
8267  * Since: 2.16
8268  **/
8269 GtkImageType
8270 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8271                                  GtkEntryIconPosition  icon_pos)
8272 {
8273   GtkEntryPrivate *priv;
8274   EntryIconInfo *icon_info;
8275
8276   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8277   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8278
8279   priv = entry->priv;
8280
8281   icon_info = priv->icons[icon_pos];
8282
8283   if (!icon_info)
8284     return GTK_IMAGE_EMPTY;
8285
8286   return icon_info->storage_type;
8287 }
8288
8289 /**
8290  * gtk_entry_get_icon_at_pos:
8291  * @entry: a #GtkEntry
8292  * @x: the x coordinate of the position to find
8293  * @y: the y coordinate of the position to find
8294  *
8295  * Finds the icon at the given position and return its index. The
8296  * position's coordinates are relative to the @entry's top left corner.
8297  * If @x, @y doesn't lie inside an icon, -1 is returned.
8298  * This function is intended for use in a #GtkWidget::query-tooltip
8299  * signal handler.
8300  *
8301  * Returns: the index of the icon at the given position, or -1
8302  *
8303  * Since: 2.16
8304  */
8305 gint
8306 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8307                            gint      x,
8308                            gint      y)
8309 {
8310   GtkAllocation primary;
8311   GtkAllocation secondary;
8312   gint frame_x, frame_y;
8313
8314   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8315
8316   get_frame_size (entry, FALSE, &frame_x, &frame_y, NULL, NULL);
8317   x -= frame_x;
8318   y -= frame_y;
8319
8320   get_icon_allocations (entry, &primary, &secondary);
8321
8322   if (primary.x <= x && x < primary.x + primary.width &&
8323       primary.y <= y && y < primary.y + primary.height)
8324     return GTK_ENTRY_ICON_PRIMARY;
8325
8326   if (secondary.x <= x && x < secondary.x + secondary.width &&
8327       secondary.y <= y && y < secondary.y + secondary.height)
8328     return GTK_ENTRY_ICON_SECONDARY;
8329
8330   return -1;
8331 }
8332
8333 /**
8334  * gtk_entry_set_icon_drag_source:
8335  * @entry: a #GtkIconEntry
8336  * @icon_pos: icon position
8337  * @target_list: the targets (data formats) in which the data can be provided
8338  * @actions: a bitmask of the allowed drag actions
8339  *
8340  * Sets up the icon at the given position so that GTK+ will start a drag
8341  * operation when the user clicks and drags the icon.
8342  *
8343  * To handle the drag operation, you need to connect to the usual
8344  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8345  * signal, and use gtk_entry_get_current_icon_drag_source() in
8346  * your signal handler to find out if the drag was started from
8347  * an icon.
8348  *
8349  * By default, GTK+ uses the icon as the drag icon. You can use the 
8350  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8351  * have to use g_signal_connect_after() to ensure that your signal handler
8352  * gets executed after the default handler.
8353  *
8354  * Since: 2.16
8355  */
8356 void
8357 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8358                                 GtkEntryIconPosition  icon_pos,
8359                                 GtkTargetList        *target_list,
8360                                 GdkDragAction         actions)
8361 {
8362   GtkEntryPrivate *priv;
8363   EntryIconInfo *icon_info;
8364
8365   g_return_if_fail (GTK_IS_ENTRY (entry));
8366   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8367
8368   priv = entry->priv;
8369
8370   if ((icon_info = priv->icons[icon_pos]) == NULL)
8371     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8372
8373   if (icon_info->target_list)
8374     gtk_target_list_unref (icon_info->target_list);
8375   icon_info->target_list = target_list;
8376   if (icon_info->target_list)
8377     gtk_target_list_ref (icon_info->target_list);
8378
8379   icon_info->actions = actions;
8380 }
8381
8382 /**
8383  * gtk_entry_get_current_icon_drag_source:
8384  * @entry: a #GtkIconEntry
8385  *
8386  * Returns the index of the icon which is the source of the current
8387  * DND operation, or -1.
8388  *
8389  * This function is meant to be used in a #GtkWidget::drag-data-get
8390  * callback.
8391  *
8392  * Returns: index of the icon which is the source of the current
8393  *          DND operation, or -1.
8394  *
8395  * Since: 2.16
8396  */
8397 gint
8398 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8399 {
8400   GtkEntryPrivate *priv;
8401   EntryIconInfo *icon_info = NULL;
8402   gint i;
8403
8404   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8405
8406   priv = entry->priv;
8407
8408   for (i = 0; i < MAX_ICONS; i++)
8409     {
8410       if ((icon_info = priv->icons[i]))
8411         {
8412           if (icon_info->in_drag)
8413             return i;
8414         }
8415     }
8416
8417   return -1;
8418 }
8419
8420 /**
8421  * gtk_entry_get_icon_area:
8422  * @entry: A #GtkEntry
8423  * @icon_pos: Icon position
8424  * @icon_area: (out): Return location for the icon's area
8425  *
8426  * Gets the area where entry's icon at @icon_pos is drawn.
8427  * This function is useful when drawing something to the
8428  * entry in a draw callback.
8429  *
8430  * If the entry is not realized or has no icon at the given position,
8431  * @icon_area is filled with zeros.
8432  *
8433  * See also gtk_entry_get_text_area()
8434  *
8435  * Since: 3.0
8436  */
8437 void
8438 gtk_entry_get_icon_area (GtkEntry             *entry,
8439                          GtkEntryIconPosition  icon_pos,
8440                          GdkRectangle         *icon_area)
8441 {
8442   GtkEntryPrivate *priv;
8443   EntryIconInfo *icon_info;
8444
8445   g_return_if_fail (GTK_IS_ENTRY (entry));
8446   g_return_if_fail (icon_area != NULL);
8447
8448   priv = entry->priv;
8449
8450   icon_info = priv->icons[icon_pos];
8451
8452   if (icon_info)
8453     {
8454       GtkAllocation primary;
8455       GtkAllocation secondary;
8456
8457       get_icon_allocations (entry, &primary, &secondary);
8458
8459       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8460         *icon_area = primary;
8461       else
8462         *icon_area = secondary;
8463     }
8464   else
8465     {
8466       icon_area->x = 0;
8467       icon_area->y = 0;
8468       icon_area->width = 0;
8469       icon_area->height = 0;
8470     }
8471 }
8472
8473 static void
8474 ensure_has_tooltip (GtkEntry *entry)
8475 {
8476   GtkEntryPrivate *priv;
8477   EntryIconInfo *icon_info;
8478   int i;
8479   gboolean has_tooltip = FALSE;
8480
8481   priv = entry->priv;
8482
8483   for (i = 0; i < MAX_ICONS; i++)
8484     {
8485       if ((icon_info = priv->icons[i]) != NULL)
8486         {
8487           if (icon_info->tooltip != NULL)
8488             {
8489               has_tooltip = TRUE;
8490               break;
8491             }
8492         }
8493     }
8494
8495   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8496 }
8497
8498 /**
8499  * gtk_entry_get_icon_tooltip_text:
8500  * @entry: a #GtkEntry
8501  * @icon_pos: the icon position
8502  *
8503  * Gets the contents of the tooltip on the icon at the specified 
8504  * position in @entry.
8505  * 
8506  * Returns: the tooltip text, or %NULL. Free the returned string
8507  *     with g_free() when done.
8508  * 
8509  * Since: 2.16
8510  */
8511 gchar *
8512 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8513                                  GtkEntryIconPosition  icon_pos)
8514 {
8515   GtkEntryPrivate *priv;
8516   EntryIconInfo *icon_info;
8517   gchar *text = NULL;
8518
8519   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8520   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8521
8522   priv = entry->priv;
8523
8524   icon_info = priv->icons[icon_pos];
8525
8526   if (!icon_info)
8527     return NULL;
8528  
8529   if (icon_info->tooltip && 
8530       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8531     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8532
8533   return text;
8534 }
8535
8536 /**
8537  * gtk_entry_set_icon_tooltip_text:
8538  * @entry: a #GtkEntry
8539  * @icon_pos: the icon position
8540  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8541  *
8542  * Sets @tooltip as the contents of the tooltip for the icon
8543  * at the specified position.
8544  *
8545  * Use %NULL for @tooltip to remove an existing tooltip.
8546  *
8547  * See also gtk_widget_set_tooltip_text() and 
8548  * gtk_entry_set_icon_tooltip_markup().
8549  *
8550  * Since: 2.16
8551  */
8552 void
8553 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8554                                  GtkEntryIconPosition  icon_pos,
8555                                  const gchar          *tooltip)
8556 {
8557   GtkEntryPrivate *priv;
8558   EntryIconInfo *icon_info;
8559
8560   g_return_if_fail (GTK_IS_ENTRY (entry));
8561   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8562
8563   priv = entry->priv;
8564
8565   if ((icon_info = priv->icons[icon_pos]) == NULL)
8566     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8567
8568   if (icon_info->tooltip)
8569     g_free (icon_info->tooltip);
8570
8571   /* Treat an empty string as a NULL string,
8572    * because an empty string would be useless for a tooltip:
8573    */
8574   if (tooltip && tooltip[0] == '\0')
8575     tooltip = NULL;
8576
8577   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8578
8579   ensure_has_tooltip (entry);
8580 }
8581
8582 /**
8583  * gtk_entry_get_icon_tooltip_markup:
8584  * @entry: a #GtkEntry
8585  * @icon_pos: the icon position
8586  *
8587  * Gets the contents of the tooltip on the icon at the specified 
8588  * position in @entry.
8589  * 
8590  * Returns: the tooltip text, or %NULL. Free the returned string
8591  *     with g_free() when done.
8592  * 
8593  * Since: 2.16
8594  */
8595 gchar *
8596 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8597                                    GtkEntryIconPosition  icon_pos)
8598 {
8599   GtkEntryPrivate *priv;
8600   EntryIconInfo *icon_info;
8601
8602   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8603   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8604
8605   priv = entry->priv;
8606
8607   icon_info = priv->icons[icon_pos];
8608
8609   if (!icon_info)
8610     return NULL;
8611  
8612   return g_strdup (icon_info->tooltip);
8613 }
8614
8615 /**
8616  * gtk_entry_set_icon_tooltip_markup:
8617  * @entry: a #GtkEntry
8618  * @icon_pos: the icon position
8619  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8620  *
8621  * Sets @tooltip as the contents of the tooltip for the icon at
8622  * the specified position. @tooltip is assumed to be marked up with
8623  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8624  *
8625  * Use %NULL for @tooltip to remove an existing tooltip.
8626  *
8627  * See also gtk_widget_set_tooltip_markup() and 
8628  * gtk_enty_set_icon_tooltip_text().
8629  *
8630  * Since: 2.16
8631  */
8632 void
8633 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8634                                    GtkEntryIconPosition  icon_pos,
8635                                    const gchar          *tooltip)
8636 {
8637   GtkEntryPrivate *priv;
8638   EntryIconInfo *icon_info;
8639
8640   g_return_if_fail (GTK_IS_ENTRY (entry));
8641   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8642
8643   priv = entry->priv;
8644
8645   if ((icon_info = priv->icons[icon_pos]) == NULL)
8646     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8647
8648   if (icon_info->tooltip)
8649     g_free (icon_info->tooltip);
8650
8651   /* Treat an empty string as a NULL string,
8652    * because an empty string would be useless for a tooltip:
8653    */
8654   if (tooltip && tooltip[0] == '\0')
8655     tooltip = NULL;
8656
8657   icon_info->tooltip = g_strdup (tooltip);
8658
8659   ensure_has_tooltip (entry);
8660 }
8661
8662 static gboolean
8663 gtk_entry_query_tooltip (GtkWidget  *widget,
8664                          gint        x,
8665                          gint        y,
8666                          gboolean    keyboard_tip,
8667                          GtkTooltip *tooltip)
8668 {
8669   GtkEntry *entry;
8670   GtkEntryPrivate *priv;
8671   EntryIconInfo *icon_info;
8672   gint icon_pos;
8673
8674   entry = GTK_ENTRY (widget);
8675   priv = entry->priv;
8676
8677   if (!keyboard_tip)
8678     {
8679       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8680       if (icon_pos != -1)
8681         {
8682           if ((icon_info = priv->icons[icon_pos]) != NULL)
8683             {
8684               if (icon_info->tooltip)
8685                 {
8686                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8687                   return TRUE;
8688                 }
8689
8690               return FALSE;
8691             }
8692         }
8693     }
8694
8695   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8696                                                                    x, y,
8697                                                                    keyboard_tip,
8698                                                                    tooltip);
8699 }
8700
8701
8702 /* Quick hack of a popup menu
8703  */
8704 static void
8705 activate_cb (GtkWidget *menuitem,
8706              GtkEntry  *entry)
8707 {
8708   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8709   g_signal_emit_by_name (entry, signal);
8710 }
8711
8712
8713 static gboolean
8714 gtk_entry_mnemonic_activate (GtkWidget *widget,
8715                              gboolean   group_cycling)
8716 {
8717   gtk_widget_grab_focus (widget);
8718   return TRUE;
8719 }
8720
8721 static void
8722 append_action_signal (GtkEntry     *entry,
8723                       GtkWidget    *menu,
8724                       const gchar  *stock_id,
8725                       const gchar  *signal,
8726                       gboolean      sensitive)
8727 {
8728   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8729
8730   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8731   g_signal_connect (menuitem, "activate",
8732                     G_CALLBACK (activate_cb), entry);
8733
8734   gtk_widget_set_sensitive (menuitem, sensitive);
8735   
8736   gtk_widget_show (menuitem);
8737   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8738 }
8739         
8740 static void
8741 popup_menu_detach (GtkWidget *attach_widget,
8742                    GtkMenu   *menu)
8743 {
8744   GtkEntry *entry_attach = GTK_ENTRY (attach_widget);
8745   GtkEntryPrivate *priv_attach = entry_attach->priv;
8746
8747   priv_attach->popup_menu = NULL;
8748 }
8749
8750 static void
8751 popup_position_func (GtkMenu   *menu,
8752                      gint      *x,
8753                      gint      *y,
8754                      gboolean  *push_in,
8755                      gpointer   user_data)
8756 {
8757   GtkEntry *entry = GTK_ENTRY (user_data);
8758   GtkEntryPrivate *priv = entry->priv;
8759   GtkWidget *widget = GTK_WIDGET (entry);
8760   GdkScreen *screen;
8761   GtkRequisition menu_req;
8762   GdkRectangle monitor;
8763   GtkBorder inner_border;
8764   gint monitor_num, strong_x, height;
8765  
8766   g_return_if_fail (gtk_widget_get_realized (widget));
8767
8768   gdk_window_get_origin (priv->text_area, x, y);
8769
8770   screen = gtk_widget_get_screen (widget);
8771   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->text_area);
8772   if (monitor_num < 0)
8773     monitor_num = 0;
8774   gtk_menu_set_monitor (menu, monitor_num);
8775
8776   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8777   gtk_widget_get_preferred_size (priv->popup_menu,
8778                                  &menu_req, NULL);
8779   height = gdk_window_get_height (priv->text_area);
8780   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8781   _gtk_entry_effective_inner_border (entry, &inner_border);
8782
8783   *x += inner_border.left + strong_x - priv->scroll_offset;
8784   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8785     *x -= menu_req.width;
8786
8787   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8788     *y += height;
8789   else if ((*y - menu_req.height) >= monitor.y)
8790     *y -= menu_req.height;
8791   else if (monitor.y + monitor.height - (*y + height) > *y)
8792     *y += height;
8793   else
8794     *y -= menu_req.height;
8795
8796   *push_in = FALSE;
8797 }
8798
8799 static void
8800 unichar_chosen_func (const char *text,
8801                      gpointer    data)
8802 {
8803   GtkEntry *entry = GTK_ENTRY (data);
8804   GtkEntryPrivate *priv = entry->priv;
8805
8806   if (priv->editable)
8807     gtk_entry_enter_text (entry, text);
8808 }
8809
8810 typedef struct
8811 {
8812   GtkEntry *entry;
8813   gint button;
8814   guint time;
8815 } PopupInfo;
8816
8817 static void
8818 popup_targets_received (GtkClipboard     *clipboard,
8819                         GtkSelectionData *data,
8820                         gpointer          user_data)
8821 {
8822   PopupInfo *info = user_data;
8823   GtkEntry *entry = info->entry;
8824   GtkEntryPrivate *info_entry_priv = entry->priv;
8825
8826   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8827     {
8828       DisplayMode mode;
8829       gboolean clipboard_contains_text;
8830       GtkWidget *menuitem;
8831       GtkWidget *submenu;
8832       gboolean show_input_method_menu;
8833       gboolean show_unicode_menu;
8834       
8835       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8836       if (info_entry_priv->popup_menu)
8837         gtk_widget_destroy (info_entry_priv->popup_menu);
8838
8839       info_entry_priv->popup_menu = gtk_menu_new ();
8840
8841       gtk_menu_attach_to_widget (GTK_MENU (info_entry_priv->popup_menu),
8842                                  GTK_WIDGET (entry),
8843                                  popup_menu_detach);
8844       
8845       mode = gtk_entry_get_display_mode (entry);
8846       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8847                             info_entry_priv->editable && 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_COPY, "copy-clipboard",
8851                             mode == DISPLAY_NORMAL &&
8852                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8853
8854       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8855                             info_entry_priv->editable && clipboard_contains_text);
8856
8857       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8858       gtk_widget_set_sensitive (menuitem, info_entry_priv->editable && info_entry_priv->current_pos != info_entry_priv->selection_bound);
8859       g_signal_connect_swapped (menuitem, "activate",
8860                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8861       gtk_widget_show (menuitem);
8862       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8863
8864       menuitem = gtk_separator_menu_item_new ();
8865       gtk_widget_show (menuitem);
8866       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8867       
8868       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8869       gtk_widget_set_sensitive (menuitem, gtk_entry_buffer_get_length (info_entry_priv->buffer) > 0);
8870       g_signal_connect_swapped (menuitem, "activate",
8871                                 G_CALLBACK (gtk_entry_select_all), entry);
8872       gtk_widget_show (menuitem);
8873       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8874       
8875       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8876                     "gtk-show-input-method-menu", &show_input_method_menu,
8877                     "gtk-show-unicode-menu", &show_unicode_menu,
8878                     NULL);
8879
8880       if (show_input_method_menu || show_unicode_menu)
8881         {
8882           menuitem = gtk_separator_menu_item_new ();
8883           gtk_widget_show (menuitem);
8884           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8885         }
8886       
8887       if (show_input_method_menu)
8888         {
8889           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8890           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8891           gtk_widget_show (menuitem);
8892           submenu = gtk_menu_new ();
8893           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8894
8895           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8896
8897           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (info_entry_priv->im_context),
8898                                                 GTK_MENU_SHELL (submenu));
8899         }
8900       
8901       if (show_unicode_menu)
8902         {
8903           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8904           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8905           gtk_widget_show (menuitem);
8906           
8907           submenu = gtk_menu_new ();
8908           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8909           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8910
8911           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8912                                                         unichar_chosen_func,
8913                                                         entry);
8914         }
8915       
8916       g_signal_emit (entry,
8917                      signals[POPULATE_POPUP],
8918                      0,
8919                      info_entry_priv->popup_menu);
8920
8921
8922       if (info->button)
8923         gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8924                         NULL, NULL,
8925                         info->button, info->time);
8926       else
8927         {
8928           gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8929                           popup_position_func, entry,
8930                           info->button, info->time);
8931           gtk_menu_shell_select_first (GTK_MENU_SHELL (info_entry_priv->popup_menu), FALSE);
8932         }
8933     }
8934
8935   g_object_unref (entry);
8936   g_slice_free (PopupInfo, info);
8937 }
8938                         
8939 static void
8940 gtk_entry_do_popup (GtkEntry       *entry,
8941                     GdkEventButton *event)
8942 {
8943   PopupInfo *info = g_slice_new (PopupInfo);
8944
8945   /* In order to know what entries we should make sensitive, we
8946    * ask for the current targets of the clipboard, and when
8947    * we get them, then we actually pop up the menu.
8948    */
8949   info->entry = g_object_ref (entry);
8950   
8951   if (event)
8952     {
8953       info->button = event->button;
8954       info->time = event->time;
8955     }
8956   else
8957     {
8958       info->button = 0;
8959       info->time = gtk_get_current_event_time ();
8960     }
8961
8962   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8963                                   gdk_atom_intern_static_string ("TARGETS"),
8964                                   popup_targets_received,
8965                                   info);
8966 }
8967
8968 static gboolean
8969 gtk_entry_popup_menu (GtkWidget *widget)
8970 {
8971   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8972   return TRUE;
8973 }
8974
8975 static void
8976 gtk_entry_drag_begin (GtkWidget      *widget,
8977                       GdkDragContext *context)
8978 {
8979   GtkEntry *entry = GTK_ENTRY (widget);
8980   GtkEntryPrivate *priv = entry->priv;
8981   gint i;
8982
8983   for (i = 0; i < MAX_ICONS; i++)
8984     {
8985       EntryIconInfo *icon_info = priv->icons[i];
8986       
8987       if (icon_info != NULL)
8988         {
8989           if (icon_info->in_drag) 
8990             {
8991               switch (icon_info->storage_type)
8992                 {
8993                 case GTK_IMAGE_STOCK:
8994                   gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
8995                   break;
8996
8997                 case GTK_IMAGE_ICON_NAME:
8998                   gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
8999                   break;
9000
9001                   /* FIXME: No GIcon support for dnd icons */
9002                 case GTK_IMAGE_GICON:
9003                 case GTK_IMAGE_PIXBUF:
9004                   gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
9005                   break;
9006                 default:
9007                   g_assert_not_reached ();
9008                 }
9009             }
9010         }
9011     }
9012 }
9013
9014 static void
9015 gtk_entry_drag_end (GtkWidget      *widget,
9016                     GdkDragContext *context)
9017 {
9018   GtkEntry *entry = GTK_ENTRY (widget);
9019   GtkEntryPrivate *priv = entry->priv;
9020   gint i;
9021
9022   for (i = 0; i < MAX_ICONS; i++)
9023     {
9024       EntryIconInfo *icon_info = priv->icons[i];
9025
9026       if (icon_info != NULL)
9027         icon_info->in_drag = 0;
9028     }
9029 }
9030
9031 static void
9032 gtk_entry_drag_leave (GtkWidget        *widget,
9033                       GdkDragContext   *context,
9034                       guint             time)
9035 {
9036   GtkEntry *entry = GTK_ENTRY (widget);
9037   GtkEntryPrivate *priv = entry->priv;
9038
9039   priv->dnd_position = -1;
9040   gtk_widget_queue_draw (widget);
9041 }
9042
9043 static gboolean
9044 gtk_entry_drag_drop  (GtkWidget        *widget,
9045                       GdkDragContext   *context,
9046                       gint              x,
9047                       gint              y,
9048                       guint             time)
9049 {
9050   GtkEntry *entry = GTK_ENTRY (widget);
9051   GtkEntryPrivate *priv = entry->priv;
9052   GdkAtom target = GDK_NONE;
9053
9054   if (priv->editable)
9055     target = gtk_drag_dest_find_target (widget, context, NULL);
9056
9057   if (target != GDK_NONE)
9058     gtk_drag_get_data (widget, context, target, time);
9059   else
9060     gtk_drag_finish (context, FALSE, FALSE, time);
9061   
9062   return TRUE;
9063 }
9064
9065 static gboolean
9066 gtk_entry_drag_motion (GtkWidget        *widget,
9067                        GdkDragContext   *context,
9068                        gint              x,
9069                        gint              y,
9070                        guint             time)
9071 {
9072   GtkEntry *entry = GTK_ENTRY (widget);
9073   GtkEntryPrivate *priv = entry->priv;
9074   GtkStyleContext *style_context;
9075   GtkWidget *source_widget;
9076   GdkDragAction suggested_action;
9077   gint new_position, old_position;
9078   gint sel1, sel2;
9079   GtkBorder padding;
9080
9081   style_context = gtk_widget_get_style_context (widget);
9082   gtk_style_context_get_padding (style_context, 0, &padding);
9083   x -= padding.left;
9084   y -= padding.top;
9085
9086   old_position = priv->dnd_position;
9087   new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
9088
9089   if (priv->editable &&
9090       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
9091     {
9092       source_widget = gtk_drag_get_source_widget (context);
9093       suggested_action = gdk_drag_context_get_suggested_action (context);
9094
9095       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
9096           new_position < sel1 || new_position > sel2)
9097         {
9098           if (source_widget == widget)
9099             {
9100               /* Default to MOVE, unless the user has
9101                * pressed ctrl or alt to affect available actions
9102                */
9103               if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
9104                 suggested_action = GDK_ACTION_MOVE;
9105             }
9106
9107           priv->dnd_position = new_position;
9108         }
9109       else
9110         {
9111           if (source_widget == widget)
9112             suggested_action = 0;       /* Can't drop in selection where drag started */
9113
9114           priv->dnd_position = -1;
9115         }
9116     }
9117   else
9118     {
9119       /* Entry not editable, or no text */
9120       suggested_action = 0;
9121       priv->dnd_position = -1;
9122     }
9123   
9124   gdk_drag_status (context, suggested_action, time);
9125
9126   if (priv->dnd_position != old_position)
9127     gtk_widget_queue_draw (widget);
9128
9129   return TRUE;
9130 }
9131
9132 static void
9133 gtk_entry_drag_data_received (GtkWidget        *widget,
9134                               GdkDragContext   *context,
9135                               gint              x,
9136                               gint              y,
9137                               GtkSelectionData *selection_data,
9138                               guint             info,
9139                               guint             time)
9140 {
9141   GtkEntry *entry = GTK_ENTRY (widget);
9142   GtkEntryPrivate *priv = entry->priv;
9143   GtkEditable *editable = GTK_EDITABLE (widget);
9144   GtkStyleContext *style_context;
9145   GtkBorder padding;
9146   gchar *str;
9147
9148   str = (gchar *) gtk_selection_data_get_text (selection_data);
9149
9150   style_context = gtk_widget_get_style_context (widget);
9151   gtk_style_context_get_padding (style_context, 0, &padding);
9152   x -= padding.left;
9153   y -= padding.top;
9154
9155   if (str && priv->editable)
9156     {
9157       gint new_position;
9158       gint sel1, sel2;
9159       gint length = -1;
9160
9161       if (priv->truncate_multiline)
9162         length = truncate_multiline (str);
9163
9164       new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
9165
9166       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
9167           new_position < sel1 || new_position > sel2)
9168         {
9169           gtk_editable_insert_text (editable, str, length, &new_position);
9170         }
9171       else
9172         {
9173           /* Replacing selection */
9174           begin_change (entry);
9175           g_object_freeze_notify (G_OBJECT (entry));
9176           gtk_editable_delete_text (editable, sel1, sel2);
9177           gtk_editable_insert_text (editable, str, length, &sel1);
9178           g_object_thaw_notify (G_OBJECT (entry));
9179           end_change (entry);
9180         }
9181       
9182       gtk_drag_finish (context, TRUE, gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
9183     }
9184   else
9185     {
9186       /* Drag and drop didn't happen! */
9187       gtk_drag_finish (context, FALSE, FALSE, time);
9188     }
9189
9190   g_free (str);
9191 }
9192
9193 static void
9194 gtk_entry_drag_data_get (GtkWidget        *widget,
9195                          GdkDragContext   *context,
9196                          GtkSelectionData *selection_data,
9197                          guint             info,
9198                          guint             time)
9199 {
9200   GtkEntry *entry = GTK_ENTRY (widget);
9201   GtkEntryPrivate *priv = entry->priv;
9202   GtkEditable *editable = GTK_EDITABLE (widget);
9203   gint sel_start, sel_end;
9204   gint i;
9205
9206   /* If there is an icon drag going on, exit early. */
9207   for (i = 0; i < MAX_ICONS; i++)
9208     {
9209       EntryIconInfo *icon_info = priv->icons[i];
9210
9211       if (icon_info != NULL)
9212         {
9213           if (icon_info->in_drag)
9214             return;
9215         }
9216     }
9217
9218   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9219     {
9220       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9221
9222       gtk_selection_data_set_text (selection_data, str, -1);
9223       
9224       g_free (str);
9225     }
9226
9227 }
9228
9229 static void
9230 gtk_entry_drag_data_delete (GtkWidget      *widget,
9231                             GdkDragContext *context)
9232 {
9233   GtkEntry *entry = GTK_ENTRY (widget);
9234   GtkEntryPrivate *priv = entry->priv;
9235   GtkEditable *editable = GTK_EDITABLE (widget);
9236   gint sel_start, sel_end;
9237   gint i;
9238
9239   /* If there is an icon drag going on, exit early. */
9240   for (i = 0; i < MAX_ICONS; i++)
9241     {
9242       EntryIconInfo *icon_info = priv->icons[i];
9243
9244       if (icon_info != NULL)
9245         {
9246           if (icon_info->in_drag)
9247             return;
9248         }
9249     }
9250
9251   if (priv->editable &&
9252       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9253     gtk_editable_delete_text (editable, sel_start, sel_end);
9254 }
9255
9256 /* We display the cursor when
9257  *
9258  *  - the selection is empty, AND
9259  *  - the widget has focus
9260  */
9261
9262 #define CURSOR_ON_MULTIPLIER 2
9263 #define CURSOR_OFF_MULTIPLIER 1
9264 #define CURSOR_PEND_MULTIPLIER 3
9265 #define CURSOR_DIVIDER 3
9266
9267 static gboolean
9268 cursor_blinks (GtkEntry *entry)
9269 {
9270   GtkEntryPrivate *priv = entry->priv;
9271
9272   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9273       priv->editable &&
9274       priv->selection_bound == priv->current_pos)
9275     {
9276       GtkSettings *settings;
9277       gboolean blink;
9278
9279       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9280       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9281
9282       return blink;
9283     }
9284   else
9285     return FALSE;
9286 }
9287
9288 static gint
9289 get_cursor_time (GtkEntry *entry)
9290 {
9291   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9292   gint time;
9293
9294   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9295
9296   return time;
9297 }
9298
9299 static gint
9300 get_cursor_blink_timeout (GtkEntry *entry)
9301 {
9302   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9303   gint timeout;
9304
9305   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9306
9307   return timeout;
9308 }
9309
9310 static void
9311 show_cursor (GtkEntry *entry)
9312 {
9313   GtkEntryPrivate *priv = entry->priv;
9314   GtkWidget *widget;
9315
9316   if (!priv->cursor_visible)
9317     {
9318       priv->cursor_visible = TRUE;
9319
9320       widget = GTK_WIDGET (entry);
9321       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9322         gtk_widget_queue_draw (widget);
9323     }
9324 }
9325
9326 static void
9327 hide_cursor (GtkEntry *entry)
9328 {
9329   GtkEntryPrivate *priv = entry->priv;
9330   GtkWidget *widget;
9331
9332   if (priv->cursor_visible)
9333     {
9334       priv->cursor_visible = FALSE;
9335
9336       widget = GTK_WIDGET (entry);
9337       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9338         gtk_widget_queue_draw (widget);
9339     }
9340 }
9341
9342 /*
9343  * Blink!
9344  */
9345 static gint
9346 blink_cb (gpointer data)
9347 {
9348   GtkEntry *entry;
9349   GtkEntryPrivate *priv; 
9350   gint blink_timeout;
9351
9352   entry = GTK_ENTRY (data);
9353   priv = entry->priv;
9354  
9355   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9356     {
9357       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9358                  "connect a handler to this signal, it must return\n"
9359                  "FALSE so the entry gets the event as well");
9360
9361       gtk_entry_check_cursor_blink (entry);
9362
9363       return FALSE;
9364     }
9365   
9366   g_assert (priv->selection_bound == priv->current_pos);
9367   
9368   blink_timeout = get_cursor_blink_timeout (entry);
9369   if (priv->blink_time > 1000 * blink_timeout && 
9370       blink_timeout < G_MAXINT/1000) 
9371     {
9372       /* we've blinked enough without the user doing anything, stop blinking */
9373       show_cursor (entry);
9374       priv->blink_timeout = 0;
9375     } 
9376   else if (priv->cursor_visible)
9377     {
9378       hide_cursor (entry);
9379       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9380                                             blink_cb,
9381                                             entry);
9382     }
9383   else
9384     {
9385       show_cursor (entry);
9386       priv->blink_time += get_cursor_time (entry);
9387       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9388                                             blink_cb,
9389                                             entry);
9390     }
9391
9392   /* Remove ourselves */
9393   return FALSE;
9394 }
9395
9396 static void
9397 gtk_entry_check_cursor_blink (GtkEntry *entry)
9398 {
9399   GtkEntryPrivate *priv = entry->priv;
9400
9401   if (cursor_blinks (entry))
9402     {
9403       if (!priv->blink_timeout)
9404         {
9405           show_cursor (entry);
9406           priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9407                                                 blink_cb,
9408                                                 entry);
9409         }
9410     }
9411   else
9412     {
9413       if (priv->blink_timeout)
9414         {
9415           g_source_remove (priv->blink_timeout);
9416           priv->blink_timeout = 0;
9417         }
9418
9419       priv->cursor_visible = TRUE;
9420     }
9421 }
9422
9423 static void
9424 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9425 {
9426   GtkEntryPrivate *priv = entry->priv;
9427
9428   if (cursor_blinks (entry))
9429     {
9430       if (priv->blink_timeout != 0)
9431         g_source_remove (priv->blink_timeout);
9432
9433       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9434                                                      blink_cb,
9435                                                      entry);
9436       show_cursor (entry);
9437     }
9438 }
9439
9440 static void
9441 gtk_entry_reset_blink_time (GtkEntry *entry)
9442 {
9443   GtkEntryPrivate *priv = entry->priv;
9444
9445   priv->blink_time = 0;
9446 }
9447
9448
9449 /* completion */
9450 static gint
9451 gtk_entry_completion_timeout (gpointer data)
9452 {
9453   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9454   GtkEntryPrivate *completion_entry_priv = GTK_ENTRY (completion->priv->entry)->priv;
9455
9456   completion->priv->completion_timeout = 0;
9457
9458   if (completion->priv->filter_model &&
9459       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9460       >= completion->priv->minimum_key_length)
9461     {
9462       gint matches;
9463       gint actions;
9464       GtkTreeSelection *s;
9465       gboolean popup_single;
9466
9467       gtk_entry_completion_complete (completion);
9468       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9469
9470       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9471
9472       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9473
9474       gtk_tree_selection_unselect_all (s);
9475
9476       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9477
9478       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9479       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9480         { 
9481           if (gtk_widget_get_visible (completion->priv->popup_window))
9482             _gtk_entry_completion_resize_popup (completion);
9483           else
9484             _gtk_entry_completion_popup (completion, completion_entry_priv->completion_device);
9485         }
9486       else 
9487         _gtk_entry_completion_popdown (completion);
9488     }
9489   else if (gtk_widget_get_visible (completion->priv->popup_window))
9490     _gtk_entry_completion_popdown (completion);
9491
9492   return FALSE;
9493 }
9494
9495 static inline gboolean
9496 keyval_is_cursor_move (guint keyval)
9497 {
9498   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9499     return TRUE;
9500
9501   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9502     return TRUE;
9503
9504   if (keyval == GDK_KEY_Page_Up)
9505     return TRUE;
9506
9507   if (keyval == GDK_KEY_Page_Down)
9508     return TRUE;
9509
9510   return FALSE;
9511 }
9512
9513 static gboolean
9514 gtk_entry_completion_key_press (GtkWidget   *widget,
9515                                 GdkEventKey *event,
9516                                 gpointer     user_data)
9517 {
9518   gint matches, actions = 0;
9519   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9520
9521   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9522     return FALSE;
9523
9524   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9525
9526   if (completion->priv->actions)
9527     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9528
9529   if (keyval_is_cursor_move (event->keyval))
9530     {
9531       GtkTreePath *path = NULL;
9532       
9533       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9534         {
9535           if (completion->priv->current_selected < 0)
9536             completion->priv->current_selected = matches + actions - 1;
9537           else
9538             completion->priv->current_selected--;
9539         }
9540       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9541         {
9542           if (completion->priv->current_selected < matches + actions - 1)
9543             completion->priv->current_selected++;
9544           else
9545             completion->priv->current_selected = -1;
9546         }
9547       else if (event->keyval == GDK_KEY_Page_Up)
9548         {
9549           if (completion->priv->current_selected < 0)
9550             completion->priv->current_selected = matches + actions - 1;
9551           else if (completion->priv->current_selected == 0)
9552             completion->priv->current_selected = -1;
9553           else if (completion->priv->current_selected < matches) 
9554             {
9555               completion->priv->current_selected -= 14;
9556               if (completion->priv->current_selected < 0)
9557                 completion->priv->current_selected = 0;
9558             }
9559           else 
9560             {
9561               completion->priv->current_selected -= 14;
9562               if (completion->priv->current_selected < matches - 1)
9563                 completion->priv->current_selected = matches - 1;
9564             }
9565         }
9566       else if (event->keyval == GDK_KEY_Page_Down)
9567         {
9568           if (completion->priv->current_selected < 0)
9569             completion->priv->current_selected = 0;
9570           else if (completion->priv->current_selected < matches - 1)
9571             {
9572               completion->priv->current_selected += 14;
9573               if (completion->priv->current_selected > matches - 1)
9574                 completion->priv->current_selected = matches - 1;
9575             }
9576           else if (completion->priv->current_selected == matches + actions - 1)
9577             {
9578               completion->priv->current_selected = -1;
9579             }
9580           else
9581             {
9582               completion->priv->current_selected += 14;
9583               if (completion->priv->current_selected > matches + actions - 1)
9584                 completion->priv->current_selected = matches + actions - 1;
9585             }
9586         }
9587
9588       if (completion->priv->current_selected < 0)
9589         {
9590           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9591           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9592
9593           if (completion->priv->inline_selection &&
9594               completion->priv->completion_prefix)
9595             {
9596               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9597                                   completion->priv->completion_prefix);
9598               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9599             }
9600         }
9601       else if (completion->priv->current_selected < matches)
9602         {
9603           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9604
9605           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9606           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9607                                     path, NULL, FALSE);
9608
9609           if (completion->priv->inline_selection)
9610             {
9611
9612               GtkTreeIter iter;
9613               GtkTreeIter child_iter;
9614               GtkTreeModel *model = NULL;
9615               GtkTreeSelection *sel;
9616               gboolean entry_set;
9617
9618               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9619               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9620                 return FALSE;
9621
9622               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9623               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9624               
9625               if (completion->priv->completion_prefix == NULL)
9626                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9627
9628               g_signal_emit_by_name (completion, "cursor-on-match", model,
9629                                      &child_iter, &entry_set);
9630             }
9631         }
9632       else if (completion->priv->current_selected - matches >= 0)
9633         {
9634           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9635
9636           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9637           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9638                                     path, NULL, FALSE);
9639
9640           if (completion->priv->inline_selection &&
9641               completion->priv->completion_prefix)
9642             {
9643               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9644                                   completion->priv->completion_prefix);
9645               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9646             }
9647         }
9648
9649       gtk_tree_path_free (path);
9650
9651       return TRUE;
9652     }
9653   else if (event->keyval == GDK_KEY_Escape ||
9654            event->keyval == GDK_KEY_Left ||
9655            event->keyval == GDK_KEY_KP_Left ||
9656            event->keyval == GDK_KEY_Right ||
9657            event->keyval == GDK_KEY_KP_Right) 
9658     {
9659       gboolean retval = TRUE;
9660
9661       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9662       _gtk_entry_completion_popdown (completion);
9663
9664       if (completion->priv->current_selected < 0)
9665         {
9666           retval = FALSE;
9667           goto keypress_completion_out;
9668         }
9669       else if (completion->priv->inline_selection)
9670         {
9671           /* Escape rejects the tentative completion */
9672           if (event->keyval == GDK_KEY_Escape)
9673             {
9674               if (completion->priv->completion_prefix)
9675                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9676                                     completion->priv->completion_prefix);
9677               else 
9678                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9679             }
9680
9681           /* Move the cursor to the end for Right/Esc, to the
9682              beginning for Left */
9683           if (event->keyval == GDK_KEY_Right ||
9684               event->keyval == GDK_KEY_KP_Right ||
9685               event->keyval == GDK_KEY_Escape)
9686             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9687           else
9688             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9689         }
9690
9691 keypress_completion_out:
9692       if (completion->priv->inline_selection)
9693         {
9694           g_free (completion->priv->completion_prefix);
9695           completion->priv->completion_prefix = NULL;
9696         }
9697
9698       return retval;
9699     }
9700   else if (event->keyval == GDK_KEY_Tab || 
9701            event->keyval == GDK_KEY_KP_Tab ||
9702            event->keyval == GDK_KEY_ISO_Left_Tab) 
9703     {
9704       GtkDirectionType dir = event->keyval == GDK_KEY_ISO_Left_Tab ? 
9705         GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
9706       
9707       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9708       _gtk_entry_completion_popdown (completion);
9709
9710       g_free (completion->priv->completion_prefix);
9711       completion->priv->completion_prefix = NULL;
9712
9713       gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
9714
9715       return TRUE;
9716     }
9717   else if (event->keyval == GDK_KEY_ISO_Enter ||
9718            event->keyval == GDK_KEY_KP_Enter ||
9719            event->keyval == GDK_KEY_Return)
9720     {
9721       GtkTreeIter iter;
9722       GtkTreeModel *model = NULL;
9723       GtkTreeModel *child_model;
9724       GtkTreeIter child_iter;
9725       GtkTreeSelection *sel;
9726       gboolean retval = TRUE;
9727
9728       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9729       _gtk_entry_completion_popdown (completion);
9730
9731       if (completion->priv->current_selected < matches)
9732         {
9733           gboolean entry_set;
9734
9735           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9736           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9737             {
9738               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9739               child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9740               g_signal_handler_block (widget, completion->priv->changed_id);
9741               g_signal_emit_by_name (completion, "match-selected",
9742                                      child_model, &child_iter, &entry_set);
9743               g_signal_handler_unblock (widget, completion->priv->changed_id);
9744
9745               if (!entry_set)
9746                 {
9747                   gchar *str = NULL;
9748
9749                   gtk_tree_model_get (model, &iter,
9750                                       completion->priv->text_column, &str,
9751                                       -1);
9752
9753                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9754
9755                   /* move the cursor to the end */
9756                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9757
9758                   g_free (str);
9759                 }
9760             }
9761           else
9762             retval = FALSE;
9763         }
9764       else if (completion->priv->current_selected - matches >= 0)
9765         {
9766           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9767           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9768             {
9769               GtkTreePath *path;
9770
9771               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9772               g_signal_emit_by_name (completion, "action-activated",
9773                                      gtk_tree_path_get_indices (path)[0]);
9774               gtk_tree_path_free (path);
9775             }
9776           else
9777             retval = FALSE;
9778         }
9779
9780       g_free (completion->priv->completion_prefix);
9781       completion->priv->completion_prefix = NULL;
9782
9783       return retval;
9784     }
9785
9786   return FALSE;
9787 }
9788
9789 static void
9790 gtk_entry_completion_changed (GtkWidget *widget,
9791                               gpointer   user_data)
9792 {
9793   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9794   GtkEntry *entry = GTK_ENTRY (widget);
9795   GtkEntryPrivate *priv = entry->priv;
9796   GdkDevice *device;
9797
9798   /* (re)install completion timeout */
9799   if (completion->priv->completion_timeout)
9800     g_source_remove (completion->priv->completion_timeout);
9801
9802   if (!gtk_entry_get_text (entry))
9803     return;
9804
9805   /* no need to normalize for this test */
9806   if (completion->priv->minimum_key_length > 0 &&
9807       strcmp ("", gtk_entry_get_text (entry)) == 0)
9808     {
9809       if (gtk_widget_get_visible (completion->priv->popup_window))
9810         _gtk_entry_completion_popdown (completion);
9811       return;
9812     }
9813
9814   device = gtk_get_current_event_device ();
9815
9816   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
9817     device = gdk_device_get_associated_device (device);
9818
9819   if (device)
9820     priv->completion_device = device;
9821
9822   completion->priv->completion_timeout =
9823     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9824                    gtk_entry_completion_timeout,
9825                    completion);
9826 }
9827
9828 static gboolean
9829 check_completion_callback (GtkEntryCompletion *completion)
9830 {
9831   completion->priv->check_completion_idle = NULL;
9832   
9833   gtk_entry_completion_complete (completion);
9834   gtk_entry_completion_insert_prefix (completion);
9835
9836   return FALSE;
9837 }
9838
9839 static void
9840 clear_completion_callback (GtkEntry   *entry,
9841                            GParamSpec *pspec)
9842 {
9843   if (pspec->name == I_("cursor-position") ||
9844       pspec->name == I_("selection-bound"))
9845     {
9846       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9847       
9848       completion->priv->has_completion = FALSE;
9849     }
9850 }
9851
9852 static gboolean
9853 accept_completion_callback (GtkEntry *entry)
9854 {
9855   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9856
9857   if (completion->priv->has_completion)
9858     gtk_editable_set_position (GTK_EDITABLE (entry),
9859                                gtk_entry_buffer_get_length (get_buffer (entry)));
9860
9861   return FALSE;
9862 }
9863
9864 static void
9865 completion_insert_text_callback (GtkEntry           *entry,
9866                                  const gchar        *text,
9867                                  gint                length,
9868                                  gint                position,
9869                                  GtkEntryCompletion *completion)
9870 {
9871   /* idle to update the selection based on the file list */
9872   if (completion->priv->check_completion_idle == NULL)
9873     {
9874       completion->priv->check_completion_idle = g_idle_source_new ();
9875       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9876       g_source_set_closure (completion->priv->check_completion_idle,
9877                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9878                                                    G_OBJECT (completion)));
9879       g_source_attach (completion->priv->check_completion_idle, NULL);
9880     }
9881 }
9882
9883 static void
9884 completion_changed (GtkEntryCompletion *completion,
9885                     GParamSpec         *pspec,
9886                     gpointer            data)
9887 {
9888   GtkEntry *entry = GTK_ENTRY (data);
9889
9890   if (pspec->name == I_("popup-completion") ||
9891       pspec->name == I_("inline-completion"))
9892     {
9893       disconnect_completion_signals (entry, completion);
9894       connect_completion_signals (entry, completion);
9895     }
9896 }
9897
9898 static void
9899 disconnect_completion_signals (GtkEntry           *entry,
9900                                GtkEntryCompletion *completion)
9901 {
9902   g_signal_handlers_disconnect_by_func (completion, 
9903                                        G_CALLBACK (completion_changed), entry);
9904   if (completion->priv->changed_id > 0 &&
9905       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9906     {
9907       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9908       completion->priv->changed_id = 0;
9909     }
9910   g_signal_handlers_disconnect_by_func (entry, 
9911                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9912   if (completion->priv->insert_text_id > 0 &&
9913       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9914     {
9915       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9916       completion->priv->insert_text_id = 0;
9917     }
9918   g_signal_handlers_disconnect_by_func (entry, 
9919                                         G_CALLBACK (completion_insert_text_callback), completion);
9920   g_signal_handlers_disconnect_by_func (entry, 
9921                                         G_CALLBACK (clear_completion_callback), completion);
9922   g_signal_handlers_disconnect_by_func (entry, 
9923                                         G_CALLBACK (accept_completion_callback), completion);
9924 }
9925
9926 static void
9927 connect_completion_signals (GtkEntry           *entry,
9928                             GtkEntryCompletion *completion)
9929 {
9930   if (completion->priv->popup_completion)
9931     {
9932       completion->priv->changed_id =
9933         g_signal_connect (entry, "changed",
9934                           G_CALLBACK (gtk_entry_completion_changed), completion);
9935       g_signal_connect (entry, "key-press-event",
9936                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9937     }
9938  
9939   if (completion->priv->inline_completion)
9940     {
9941       completion->priv->insert_text_id =
9942         g_signal_connect (entry, "insert-text",
9943                           G_CALLBACK (completion_insert_text_callback), completion);
9944       g_signal_connect (entry, "notify",
9945                         G_CALLBACK (clear_completion_callback), completion);
9946       g_signal_connect (entry, "activate",
9947                         G_CALLBACK (accept_completion_callback), completion);
9948       g_signal_connect (entry, "focus-out-event",
9949                         G_CALLBACK (accept_completion_callback), completion);
9950     }
9951
9952   g_signal_connect (completion, "notify",
9953                     G_CALLBACK (completion_changed), entry);
9954 }
9955
9956 /**
9957  * gtk_entry_set_completion:
9958  * @entry: A #GtkEntry
9959  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9960  *
9961  * Sets @completion to be the auxiliary completion object to use with @entry.
9962  * All further configuration of the completion mechanism is done on
9963  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9964  * @completion is set to %NULL.
9965  *
9966  * Since: 2.4
9967  */
9968 void
9969 gtk_entry_set_completion (GtkEntry           *entry,
9970                           GtkEntryCompletion *completion)
9971 {
9972   GtkEntryCompletion *old;
9973
9974   g_return_if_fail (GTK_IS_ENTRY (entry));
9975   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9976
9977   old = gtk_entry_get_completion (entry);
9978
9979   if (old == completion)
9980     return;
9981   
9982   if (old)
9983     {
9984       if (old->priv->completion_timeout)
9985         {
9986           g_source_remove (old->priv->completion_timeout);
9987           old->priv->completion_timeout = 0;
9988         }
9989
9990       if (gtk_widget_get_mapped (old->priv->popup_window))
9991         _gtk_entry_completion_popdown (old);
9992
9993       disconnect_completion_signals (entry, old);
9994       old->priv->entry = NULL;
9995
9996       g_object_unref (old);
9997     }
9998
9999   if (!completion)
10000     {
10001       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
10002       return;
10003     }
10004
10005   /* hook into the entry */
10006   g_object_ref (completion);
10007
10008   connect_completion_signals (entry, completion);    
10009   completion->priv->entry = GTK_WIDGET (entry);
10010   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
10011
10012   g_object_notify (G_OBJECT (entry), "completion");
10013 }
10014
10015 /**
10016  * gtk_entry_get_completion:
10017  * @entry: A #GtkEntry
10018  *
10019  * Returns the auxiliary completion object currently in use by @entry.
10020  *
10021  * Return value: (transfer none): The auxiliary completion object currently
10022  *     in use by @entry.
10023  *
10024  * Since: 2.4
10025  */
10026 GtkEntryCompletion *
10027 gtk_entry_get_completion (GtkEntry *entry)
10028 {
10029   GtkEntryCompletion *completion;
10030
10031   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10032
10033   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
10034                                      GTK_ENTRY_COMPLETION_KEY));
10035
10036   return completion;
10037 }
10038
10039 /**
10040  * gtk_entry_set_cursor_hadjustment:
10041  * @entry: a #GtkEntry
10042  * @adjustment: an adjustment which should be adjusted when the cursor 
10043  *              is moved, or %NULL
10044  *
10045  * Hooks up an adjustment to the cursor position in an entry, so that when 
10046  * the cursor is moved, the adjustment is scrolled to show that position. 
10047  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
10048  * the adjustment.
10049  *
10050  * The adjustment has to be in pixel units and in the same coordinate system 
10051  * as the entry. 
10052  * 
10053  * Since: 2.12
10054  */
10055 void
10056 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
10057                                   GtkAdjustment *adjustment)
10058 {
10059   g_return_if_fail (GTK_IS_ENTRY (entry));
10060   if (adjustment)
10061     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
10062
10063   if (adjustment)
10064     g_object_ref (adjustment);
10065
10066   g_object_set_qdata_full (G_OBJECT (entry), 
10067                            quark_cursor_hadjustment,
10068                            adjustment, 
10069                            g_object_unref);
10070 }
10071
10072 /**
10073  * gtk_entry_get_cursor_hadjustment:
10074  * @entry: a #GtkEntry
10075  *
10076  * Retrieves the horizontal cursor adjustment for the entry. 
10077  * See gtk_entry_set_cursor_hadjustment().
10078  *
10079  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
10080  *   if none has been set.
10081  *
10082  * Since: 2.12
10083  */
10084 GtkAdjustment*
10085 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
10086 {
10087   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10088
10089   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
10090 }
10091
10092 /**
10093  * gtk_entry_set_progress_fraction:
10094  * @entry: a #GtkEntry
10095  * @fraction: fraction of the task that's been completed
10096  *
10097  * Causes the entry's progress indicator to "fill in" the given
10098  * fraction of the bar. The fraction should be between 0.0 and 1.0,
10099  * inclusive.
10100  *
10101  * Since: 2.16
10102  */
10103 void
10104 gtk_entry_set_progress_fraction (GtkEntry *entry,
10105                                  gdouble   fraction)
10106 {
10107   GtkWidget       *widget;
10108   GtkEntryPrivate *private;
10109   gdouble          old_fraction;
10110   gint x, y, width, height;
10111   gint old_x, old_y, old_width, old_height;
10112
10113   g_return_if_fail (GTK_IS_ENTRY (entry));
10114
10115   widget = GTK_WIDGET (entry);
10116   private = entry->priv;
10117
10118   if (private->progress_pulse_mode)
10119     old_fraction = -1;
10120   else
10121     old_fraction = private->progress_fraction;
10122
10123   if (gtk_widget_is_drawable (widget))
10124     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
10125
10126   fraction = CLAMP (fraction, 0.0, 1.0);
10127
10128   private->progress_fraction = fraction;
10129   private->progress_pulse_mode = FALSE;
10130   private->progress_pulse_current = 0.0;
10131
10132   if (gtk_widget_is_drawable (widget))
10133     {
10134       get_progress_area (widget, &x, &y, &width, &height);
10135
10136       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
10137         gtk_widget_queue_draw (widget);
10138     }
10139
10140   if (fraction != old_fraction)
10141     g_object_notify (G_OBJECT (entry), "progress-fraction");
10142 }
10143
10144 /**
10145  * gtk_entry_get_progress_fraction:
10146  * @entry: a #GtkEntry
10147  *
10148  * Returns the current fraction of the task that's been completed.
10149  * See gtk_entry_set_progress_fraction().
10150  *
10151  * Return value: a fraction from 0.0 to 1.0
10152  *
10153  * Since: 2.16
10154  */
10155 gdouble
10156 gtk_entry_get_progress_fraction (GtkEntry *entry)
10157 {
10158   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10159
10160   return entry->priv->progress_fraction;
10161 }
10162
10163 /**
10164  * gtk_entry_set_progress_pulse_step:
10165  * @entry: a #GtkEntry
10166  * @fraction: fraction between 0.0 and 1.0
10167  *
10168  * Sets the fraction of total entry width to move the progress
10169  * bouncing block for each call to gtk_entry_progress_pulse().
10170  *
10171  * Since: 2.16
10172  */
10173 void
10174 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
10175                                    gdouble   fraction)
10176 {
10177   GtkEntryPrivate *private;
10178
10179   g_return_if_fail (GTK_IS_ENTRY (entry));
10180
10181   private = entry->priv;
10182
10183   fraction = CLAMP (fraction, 0.0, 1.0);
10184
10185   if (fraction != private->progress_pulse_fraction)
10186     {
10187       private->progress_pulse_fraction = fraction;
10188
10189       gtk_widget_queue_draw (GTK_WIDGET (entry));
10190
10191       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
10192     }
10193 }
10194
10195 /**
10196  * gtk_entry_get_progress_pulse_step:
10197  * @entry: a #GtkEntry
10198  *
10199  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
10200  *
10201  * Return value: a fraction from 0.0 to 1.0
10202  *
10203  * Since: 2.16
10204  */
10205 gdouble
10206 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
10207 {
10208   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10209
10210   return entry->priv->progress_pulse_fraction;
10211 }
10212
10213 /**
10214  * gtk_entry_progress_pulse:
10215  * @entry: a #GtkEntry
10216  *
10217  * Indicates that some progress is made, but you don't know how much.
10218  * Causes the entry's progress indicator to enter "activity mode,"
10219  * where a block bounces back and forth. Each call to
10220  * gtk_entry_progress_pulse() causes the block to move by a little bit
10221  * (the amount of movement per pulse is determined by
10222  * gtk_entry_set_progress_pulse_step()).
10223  *
10224  * Since: 2.16
10225  */
10226 void
10227 gtk_entry_progress_pulse (GtkEntry *entry)
10228 {
10229   GtkEntryPrivate *private;
10230
10231   g_return_if_fail (GTK_IS_ENTRY (entry));
10232
10233   private = entry->priv;
10234
10235   if (private->progress_pulse_mode)
10236     {
10237       if (private->progress_pulse_way_back)
10238         {
10239           private->progress_pulse_current -= private->progress_pulse_fraction;
10240
10241           if (private->progress_pulse_current < 0.0)
10242             {
10243               private->progress_pulse_current = 0.0;
10244               private->progress_pulse_way_back = FALSE;
10245             }
10246         }
10247       else
10248         {
10249           private->progress_pulse_current += private->progress_pulse_fraction;
10250
10251           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10252             {
10253               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10254               private->progress_pulse_way_back = TRUE;
10255             }
10256         }
10257     }
10258   else
10259     {
10260       private->progress_fraction = 0.0;
10261       private->progress_pulse_mode = TRUE;
10262       private->progress_pulse_way_back = FALSE;
10263       private->progress_pulse_current = 0.0;
10264     }
10265
10266   gtk_widget_queue_draw (GTK_WIDGET (entry));
10267 }
10268
10269 /**
10270  * gtk_entry_set_placeholder_text:
10271  * @entry: a #GtkEntry
10272  * @text: a string to be displayed when @entry is empty an unfocused, or %NULL
10273  *
10274  * Sets text to be displayed in @entry when
10275  * it is empty and unfocused. This can be used to give a visual hint
10276  * of the expected contents of the #GtkEntry.
10277  *
10278  * Since: 3.2
10279  **/
10280 void
10281 gtk_entry_set_placeholder_text (GtkEntry    *entry,
10282                                 const gchar *text)
10283 {
10284   GtkEntryPrivate *priv;
10285
10286   g_return_if_fail (GTK_IS_ENTRY (entry));
10287
10288   priv = entry->priv;
10289
10290   if (g_strcmp0 (priv->placeholder_text, text) == 0)
10291     return;
10292
10293   g_free (priv->placeholder_text);
10294   priv->placeholder_text = g_strdup (text);
10295
10296   gtk_entry_recompute (entry);
10297
10298   g_object_notify (G_OBJECT (entry), "placeholder-text");
10299 }
10300
10301 /**
10302  * gtk_entry_get_placeholder_text:
10303  * @entry: a #GtkEntry
10304  *
10305  * Retrieves the text that will be displayed when @entry is empty and unfocused
10306  *
10307  * Returns: a pointer to the placeholder text as a string. This string points to internally allocated
10308  * storage in the widget and must not be freed, modified or stored.
10309  *
10310  * Since: 3.2
10311  **/
10312 G_CONST_RETURN gchar *
10313 gtk_entry_get_placeholder_text (GtkEntry *entry)
10314 {
10315   GtkEntryPrivate *priv;
10316
10317   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10318
10319   priv = entry->priv;
10320
10321   return priv->placeholder_text;
10322 }
10323
10324 /* Caps Lock warning for password entries */
10325
10326 static void
10327 show_capslock_feedback (GtkEntry    *entry,
10328                         const gchar *text)
10329 {
10330   GtkEntryPrivate *priv = entry->priv;
10331
10332   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10333     {
10334       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10335       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10336       priv->caps_lock_warning_shown = TRUE;
10337     }
10338
10339   if (priv->caps_lock_warning_shown)
10340     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10341   else
10342     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10343 }
10344
10345 static void
10346 remove_capslock_feedback (GtkEntry *entry)
10347 {
10348   GtkEntryPrivate *priv = entry->priv;
10349
10350   if (priv->caps_lock_warning_shown)
10351     {
10352       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10353       priv->caps_lock_warning_shown = FALSE;
10354     } 
10355 }
10356
10357 static void
10358 keymap_state_changed (GdkKeymap *keymap, 
10359                       GtkEntry  *entry)
10360 {
10361   GtkEntryPrivate *priv = entry->priv;
10362   char *text = NULL;
10363
10364   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10365     { 
10366       if (gdk_keymap_get_num_lock_state (keymap)
10367           && gdk_keymap_get_caps_lock_state (keymap))
10368         text = _("Caps Lock and Num Lock are on");
10369       else if (gdk_keymap_get_num_lock_state (keymap))
10370         text = _("Num Lock is on");
10371       else if (gdk_keymap_get_caps_lock_state (keymap))
10372         text = _("Caps Lock is on");
10373     }
10374
10375   if (text)
10376     show_capslock_feedback (entry, text);
10377   else
10378     remove_capslock_feedback (entry);
10379 }
10380
10381 /*
10382  * _gtk_entry_set_is_cell_renderer:
10383  * @entry: a #GtkEntry
10384  * @is_cell_renderer: new value
10385  *
10386  * This is a helper function for GtkComboBox. A GtkEntry in a GtkComboBox
10387  * is supposed to behave like a GtkCellEditable when placed in a combo box.
10388  *
10389  * I.e take up its allocation and get GtkEntry->is_cell_renderer = TRUE.
10390  *
10391  */
10392 void
10393 _gtk_entry_set_is_cell_renderer (GtkEntry *entry,
10394                                  gboolean  is_cell_renderer)
10395 {
10396   entry->priv->is_cell_renderer = is_cell_renderer;
10397 }