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