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