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