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