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