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