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