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