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