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