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