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