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