gtkentry.c 185 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
19
 */
20 21

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

28
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
29
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
30 31 32

#include <pango/pango.h>

Elliot Lee's avatar
Elliot Lee committed
33
#include "gdk/gdkkeysyms.h"
Owen Taylor's avatar
Owen Taylor committed
34
#include "gtkbindings.h"
35
#include "gtkcelleditable.h"
Owen Taylor's avatar
Owen Taylor committed
36
#include "gtkclipboard.h"
Owen Taylor's avatar
Owen Taylor committed
37
#include "gtkdnd.h"
Elliot Lee's avatar
Elliot Lee committed
38
#include "gtkentry.h"
39
#include "gtkimagemenuitem.h"
40
#include "gtkimcontextsimple.h"
41
#include "gtkimmulticontext.h"
Owen Taylor's avatar
Owen Taylor committed
42
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
43
#include "gtkmain.h"
44
#include "gtkmarshalers.h"
Owen Taylor's avatar
Owen Taylor committed
45 46
#include "gtkmenu.h"
#include "gtkmenuitem.h"
47
#include "gtkseparatormenuitem.h"
Elliot Lee's avatar
Elliot Lee committed
48
#include "gtkselection.h"
49
#include "gtksettings.h"
50
#include "gtkspinbutton.h"
51
#include "gtkstock.h"
52
#include "gtktextutil.h"
53
#include "gtkwindow.h"
54 55
#include "gtktreeview.h"
#include "gtktreeselection.h"
56
#include "gtkprivate.h"
57 58
#include "gtkentryprivate.h"
#include "gtkcelllayout.h"
59
#include "gtkalias.h"
60 61

#define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
62

Elliot Lee's avatar
Elliot Lee committed
63 64
#define MIN_ENTRY_WIDTH  150
#define DRAW_TIMEOUT     20
65
#define COMPLETION_TIMEOUT 300
66
#define PASSWORD_HINT_MAX 8
Elliot Lee's avatar
Elliot Lee committed
67

68 69 70 71 72 73
/* Initial size of buffer, in bytes */
#define MIN_SIZE 16

/* Maximum size of text buffer, in bytes */
#define MAX_SIZE G_MAXUSHORT

74 75
static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
static GQuark          quark_inner_border   = 0;
76
static GQuark          quark_password_hint  = 0;
77
static GQuark          quark_cursor_hadjustment = 0;
78

79 80 81 82 83 84 85
typedef struct _GtkEntryPrivate GtkEntryPrivate;

#define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))

struct _GtkEntryPrivate 
{
  gfloat xalign;
86
  gint insert_pos;
87
  guint blink_time;  /* time in msec the cursor has blinked since last user event */
88 89 90
  guint interior_focus : 1;
  guint real_changed   : 1;
  guint change_count   : 8;
91 92 93

  gint focus_width;
  GtkShadowType shadow_type;
94 95 96
};

typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
97

98 99 100 101 102 103
struct _GtkEntryPasswordHint
{
  gchar password_hint[PASSWORD_HINT_MAX];
  guint password_hint_timeout_id;
  gint  password_hint_length;
  gint  password_hint_position;
104 105
};

Owen Taylor's avatar
Owen Taylor committed
106 107
enum {
  ACTIVATE,
108
  POPULATE_POPUP,
Owen Taylor's avatar
Owen Taylor committed
109 110 111
  MOVE_CURSOR,
  INSERT_AT_CURSOR,
  DELETE_FROM_CURSOR,
112
  BACKSPACE,
Owen Taylor's avatar
Owen Taylor committed
113 114 115 116 117 118 119
  CUT_CLIPBOARD,
  COPY_CLIPBOARD,
  PASTE_CLIPBOARD,
  TOGGLE_OVERWRITE,
  LAST_SIGNAL
};

120
enum {
Havoc Pennington's avatar
Havoc Pennington committed
121
  PROP_0,
122 123
  PROP_CURSOR_POSITION,
  PROP_SELECTION_BOUND,
Havoc Pennington's avatar
Havoc Pennington committed
124 125 126
  PROP_EDITABLE,
  PROP_MAX_LENGTH,
  PROP_VISIBILITY,
127
  PROP_HAS_FRAME,
128
  PROP_INNER_BORDER,
129
  PROP_INVISIBLE_CHAR,
130
  PROP_ACTIVATES_DEFAULT,
131
  PROP_WIDTH_CHARS,
132
  PROP_SCROLL_OFFSET,
133
  PROP_TEXT,
134
  PROP_XALIGN,
135
  PROP_TRUNCATE_MULTILINE,
Tim Janik's avatar
Tim Janik committed
136 137 138
  PROP_SHADOW_TYPE,
  PROP_OVERWRITE_MODE,
  PROP_TEXT_LENGTH
139 140
};

Owen Taylor's avatar
Owen Taylor committed
141
static guint signals[LAST_SIGNAL] = { 0 };
142

Owen Taylor's avatar
Owen Taylor committed
143 144 145 146 147
typedef enum {
  CURSOR_STANDARD,
  CURSOR_DND
} CursorType;

Owen Taylor's avatar
Owen Taylor committed
148 149
/* GObject, GtkObject methods
 */
150 151
static void   gtk_entry_editable_init        (GtkEditableClass     *iface);
static void   gtk_entry_cell_editable_init   (GtkCellEditableIface *iface);
152 153 154 155 156 157 158 159
static void   gtk_entry_set_property         (GObject          *object,
                                              guint             prop_id,
                                              const GValue     *value,
                                              GParamSpec       *pspec);
static void   gtk_entry_get_property         (GObject          *object,
                                              guint             prop_id,
                                              GValue           *value,
                                              GParamSpec       *pspec);
160
static void   gtk_entry_finalize             (GObject          *object);
161
static void   gtk_entry_destroy              (GtkObject        *object);
Owen Taylor's avatar
Owen Taylor committed
162 163 164

/* GtkWidget methods
 */
165 166 167 168 169 170
static void   gtk_entry_realize              (GtkWidget        *widget);
static void   gtk_entry_unrealize            (GtkWidget        *widget);
static void   gtk_entry_size_request         (GtkWidget        *widget,
					      GtkRequisition   *requisition);
static void   gtk_entry_size_allocate        (GtkWidget        *widget,
					      GtkAllocation    *allocation);
171 172
static void   gtk_entry_draw_frame           (GtkWidget        *widget,
                                              GdkRectangle     *area);
173 174 175 176 177 178 179 180 181 182
static gint   gtk_entry_expose               (GtkWidget        *widget,
					      GdkEventExpose   *event);
static gint   gtk_entry_button_press         (GtkWidget        *widget,
					      GdkEventButton   *event);
static gint   gtk_entry_button_release       (GtkWidget        *widget,
					      GdkEventButton   *event);
static gint   gtk_entry_motion_notify        (GtkWidget        *widget,
					      GdkEventMotion   *event);
static gint   gtk_entry_key_press            (GtkWidget        *widget,
					      GdkEventKey      *event);
183 184
static gint   gtk_entry_key_release          (GtkWidget        *widget,
					      GdkEventKey      *event);
185 186 187 188
static gint   gtk_entry_focus_in             (GtkWidget        *widget,
					      GdkEventFocus    *event);
static gint   gtk_entry_focus_out            (GtkWidget        *widget,
					      GdkEventFocus    *event);
189
static void   gtk_entry_grab_focus           (GtkWidget        *widget);
190 191 192 193 194 195
static void   gtk_entry_style_set            (GtkWidget        *widget,
					      GtkStyle         *previous_style);
static void   gtk_entry_direction_changed    (GtkWidget        *widget,
					      GtkTextDirection  previous_dir);
static void   gtk_entry_state_changed        (GtkWidget        *widget,
					      GtkStateType      previous_state);
196 197
static void   gtk_entry_screen_changed       (GtkWidget        *widget,
					      GdkScreen        *old_screen);
Owen Taylor's avatar
Owen Taylor committed
198

199 200 201 202 203
static gboolean gtk_entry_drag_drop          (GtkWidget        *widget,
                                              GdkDragContext   *context,
                                              gint              x,
                                              gint              y,
                                              guint             time);
Owen Taylor's avatar
Owen Taylor committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static gboolean gtk_entry_drag_motion        (GtkWidget        *widget,
					      GdkDragContext   *context,
					      gint              x,
					      gint              y,
					      guint             time);
static void     gtk_entry_drag_leave         (GtkWidget        *widget,
					      GdkDragContext   *context,
					      guint             time);
static void     gtk_entry_drag_data_received (GtkWidget        *widget,
					      GdkDragContext   *context,
					      gint              x,
					      gint              y,
					      GtkSelectionData *selection_data,
					      guint             info,
					      guint             time);
static void     gtk_entry_drag_data_get      (GtkWidget        *widget,
					      GdkDragContext   *context,
					      GtkSelectionData *selection_data,
					      guint             info,
					      guint             time);
static void     gtk_entry_drag_data_delete   (GtkWidget        *widget,
					      GdkDragContext   *context);

Owen Taylor's avatar
Owen Taylor committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/* GtkEditable method implementations
 */
static void     gtk_entry_insert_text          (GtkEditable *editable,
						const gchar *new_text,
						gint         new_text_length,
						gint        *position);
static void     gtk_entry_delete_text          (GtkEditable *editable,
						gint         start_pos,
						gint         end_pos);
static gchar *  gtk_entry_get_chars            (GtkEditable *editable,
						gint         start_pos,
						gint         end_pos);
static void     gtk_entry_real_set_position    (GtkEditable *editable,
						gint         position);
static gint     gtk_entry_get_position         (GtkEditable *editable);
static void     gtk_entry_set_selection_bounds (GtkEditable *editable,
						gint         start,
						gint         end);
static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
						gint        *start,
						gint        *end);

249 250 251 252 253
/* GtkCellEditable method implementations
 */
static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
				     GdkEvent        *event);

Owen Taylor's avatar
Owen Taylor committed
254 255
/* Default signal handlers
 */
256
static void gtk_entry_real_insert_text   (GtkEditable     *editable,
257 258 259
					  const gchar     *new_text,
					  gint             new_text_length,
					  gint            *position);
260
static void gtk_entry_real_delete_text   (GtkEditable     *editable,
261 262 263 264 265 266 267 268 269 270 271
					  gint             start_pos,
					  gint             end_pos);
static void gtk_entry_move_cursor        (GtkEntry        *entry,
					  GtkMovementStep  step,
					  gint             count,
					  gboolean         extend_selection);
static void gtk_entry_insert_at_cursor   (GtkEntry        *entry,
					  const gchar     *str);
static void gtk_entry_delete_from_cursor (GtkEntry        *entry,
					  GtkDeleteType    type,
					  gint             count);
272
static void gtk_entry_backspace          (GtkEntry        *entry);
273 274 275 276
static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
277
static void gtk_entry_select_all         (GtkEntry        *entry);
278
static void gtk_entry_real_activate      (GtkEntry        *entry);
279
static gboolean gtk_entry_popup_menu     (GtkWidget      *widget);
Owen Taylor's avatar
Owen Taylor committed
280

281 282
static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
						GtkEntry  *entry);
Owen Taylor's avatar
Owen Taylor committed
283 284
/* IM Context Callbacks
 */
285 286 287 288 289 290 291 292 293 294 295 296
static void     gtk_entry_commit_cb               (GtkIMContext *context,
						   const gchar  *str,
						   GtkEntry     *entry);
static void     gtk_entry_preedit_changed_cb      (GtkIMContext *context,
						   GtkEntry     *entry);
static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
						   GtkEntry     *entry);
static gboolean gtk_entry_delete_surrounding_cb   (GtkIMContext *context,
						   gint          offset,
						   gint          n_chars,
						   GtkEntry     *entry);

Owen Taylor's avatar
Owen Taylor committed
297 298
/* Internal routines
 */
299 300
static void         gtk_entry_enter_text               (GtkEntry       *entry,
                                                        const gchar    *str);
301 302 303
static void         gtk_entry_set_positions            (GtkEntry       *entry,
							gint            current_pos,
							gint            selection_bound);
Owen Taylor's avatar
Owen Taylor committed
304
static void         gtk_entry_draw_text                (GtkEntry       *entry);
Owen Taylor's avatar
Owen Taylor committed
305 306
static void         gtk_entry_draw_cursor              (GtkEntry       *entry,
							CursorType      type);
307 308
static PangoLayout *gtk_entry_ensure_layout            (GtkEntry       *entry,
                                                        gboolean        include_preedit);
309
static void         gtk_entry_reset_layout             (GtkEntry       *entry);
Owen Taylor's avatar
Owen Taylor committed
310 311 312 313 314
static void         gtk_entry_queue_draw               (GtkEntry       *entry);
static void         gtk_entry_recompute                (GtkEntry       *entry);
static gint         gtk_entry_find_position            (GtkEntry       *entry,
							gint            x);
static void         gtk_entry_get_cursor_locations     (GtkEntry       *entry,
Owen Taylor's avatar
Owen Taylor committed
315
							CursorType      type,
Owen Taylor's avatar
Owen Taylor committed
316 317 318 319 320 321
							gint           *strong_x,
							gint           *weak_x);
static void         gtk_entry_adjust_scroll            (GtkEntry       *entry);
static gint         gtk_entry_move_visually            (GtkEntry       *editable,
							gint            start,
							gint            count);
322 323 324
static gint         gtk_entry_move_logically           (GtkEntry       *entry,
							gint            start,
							gint            count);
Owen Taylor's avatar
Owen Taylor committed
325
static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
326 327
							gint            start,
                                                        gboolean        allow_whitespace);
Owen Taylor's avatar
Owen Taylor committed
328
static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
329 330
							gint            start,
                                                        gboolean        allow_whitespace);
Owen Taylor's avatar
Owen Taylor committed
331 332 333 334 335 336 337 338 339
static void         gtk_entry_delete_whitespace        (GtkEntry       *entry);
static void         gtk_entry_select_word              (GtkEntry       *entry);
static void         gtk_entry_select_line              (GtkEntry       *entry);
static char *       gtk_entry_get_public_chars         (GtkEntry       *entry,
							gint            start,
							gint            end);
static void         gtk_entry_paste                    (GtkEntry       *entry,
							GdkAtom         selection);
static void         gtk_entry_update_primary_selection (GtkEntry       *entry);
340
static void         gtk_entry_do_popup                 (GtkEntry       *entry,
Owen Taylor's avatar
Owen Taylor committed
341
							GdkEventButton *event);
342 343
static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
							gboolean        group_cycling);
344 345
static void         gtk_entry_state_changed            (GtkWidget      *widget,
							GtkStateType    previous_state);
346 347
static void         gtk_entry_check_cursor_blink       (GtkEntry       *entry);
static void         gtk_entry_pend_cursor_blink        (GtkEntry       *entry);
348
static void         gtk_entry_reset_blink_time         (GtkEntry       *entry);
349 350 351 352 353
static void         get_text_area_size                 (GtkEntry       *entry,
							gint           *x,
							gint           *y,
							gint           *width,
							gint           *height);
354 355 356 357 358
static void         gtk_entry_get_text_area_size       (GtkEntry       *entry,
							gint           *x,
							gint           *y,
							gint           *width,
							gint           *height);
359 360 361 362 363
static void         get_widget_window_size             (GtkEntry       *entry,
							gint           *x,
							gint           *y,
							gint           *width,
							gint           *height);
364
static void         gtk_entry_move_adjustments         (GtkEntry       *entry);
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/* Completion */
static gint         gtk_entry_completion_timeout       (gpointer            data);
static gboolean     gtk_entry_completion_key_press     (GtkWidget          *widget,
							GdkEventKey        *event,
							gpointer            user_data);
static void         gtk_entry_completion_changed       (GtkWidget          *entry,
							gpointer            user_data);
static gboolean     check_completion_callback          (GtkEntryCompletion *completion);
static void         clear_completion_callback          (GtkEntry           *entry,
							GParamSpec         *pspec);
static gboolean     accept_completion_callback         (GtkEntry           *entry);
static void         completion_insert_text_callback    (GtkEntry           *entry,
							const gchar        *text,
							gint                length,
							gint                position,
							GtkEntryCompletion *completion);
static void         completion_changed                 (GtkEntryCompletion *completion,
							GParamSpec         *pspec,
							gpointer            data);
static void         disconnect_completion_signals      (GtkEntry           *entry,
							GtkEntryCompletion *completion);
static void         connect_completion_signals         (GtkEntry           *entry,
							GtkEntryCompletion *completion);

390 391 392
static void         begin_change                       (GtkEntry *entry);
static void         end_change                         (GtkEntry *entry);
static void         emit_changed                       (GtkEntry *entry);
393

Matthias Clasen's avatar
Matthias Clasen committed
394 395 396 397
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,
Matthias Clasen's avatar
Matthias Clasen committed
398
                                                gtk_entry_cell_editable_init))
Elliot Lee's avatar
Elliot Lee committed
399

Owen Taylor's avatar
Owen Taylor committed
400 401 402 403 404 405 406 407 408 409 410
static void
add_move_binding (GtkBindingSet  *binding_set,
		  guint           keyval,
		  guint           modmask,
		  GtkMovementStep step,
		  gint            count)
{
  g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
  
  gtk_binding_entry_add_signal (binding_set, keyval, modmask,
				"move_cursor", 3,
Manish Singh's avatar
Manish Singh committed
411
				G_TYPE_ENUM, step,
Owen Taylor's avatar
Owen Taylor committed
412
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
413
				G_TYPE_BOOLEAN, FALSE);
Owen Taylor's avatar
Owen Taylor committed
414 415 416 417

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
				"move_cursor", 3,
Manish Singh's avatar
Manish Singh committed
418
				G_TYPE_ENUM, step,
Owen Taylor's avatar
Owen Taylor committed
419
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
420
				G_TYPE_BOOLEAN, TRUE);
Owen Taylor's avatar
Owen Taylor committed
421 422
}

Elliot Lee's avatar
Elliot Lee committed
423 424 425
static void
gtk_entry_class_init (GtkEntryClass *class)
{
426
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
427
  GtkWidgetClass *widget_class;
428
  GtkObjectClass *gtk_object_class;
Owen Taylor's avatar
Owen Taylor committed
429
  GtkBindingSet *binding_set;
Elliot Lee's avatar
Elliot Lee committed
430 431

  widget_class = (GtkWidgetClass*) class;
432
  gtk_object_class = (GtkObjectClass *)class;
Elliot Lee's avatar
Elliot Lee committed
433

434
  gobject_class->finalize = gtk_entry_finalize;
Havoc Pennington's avatar
Havoc Pennington committed
435 436
  gobject_class->set_property = gtk_entry_set_property;
  gobject_class->get_property = gtk_entry_get_property;
437 438 439 440 441 442 443 444 445 446

  widget_class->realize = gtk_entry_realize;
  widget_class->unrealize = gtk_entry_unrealize;
  widget_class->size_request = gtk_entry_size_request;
  widget_class->size_allocate = gtk_entry_size_allocate;
  widget_class->expose_event = gtk_entry_expose;
  widget_class->button_press_event = gtk_entry_button_press;
  widget_class->button_release_event = gtk_entry_button_release;
  widget_class->motion_notify_event = gtk_entry_motion_notify;
  widget_class->key_press_event = gtk_entry_key_press;
447
  widget_class->key_release_event = gtk_entry_key_release;
448 449
  widget_class->focus_in_event = gtk_entry_focus_in;
  widget_class->focus_out_event = gtk_entry_focus_out;
450
  widget_class->grab_focus = gtk_entry_grab_focus;
451 452 453
  widget_class->style_set = gtk_entry_style_set;
  widget_class->direction_changed = gtk_entry_direction_changed;
  widget_class->state_changed = gtk_entry_state_changed;
454
  widget_class->screen_changed = gtk_entry_screen_changed;
455
  widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
456

457
  widget_class->drag_drop = gtk_entry_drag_drop;
Owen Taylor's avatar
Owen Taylor committed
458 459 460 461 462 463
  widget_class->drag_motion = gtk_entry_drag_motion;
  widget_class->drag_leave = gtk_entry_drag_leave;
  widget_class->drag_data_received = gtk_entry_drag_data_received;
  widget_class->drag_data_get = gtk_entry_drag_data_get;
  widget_class->drag_data_delete = gtk_entry_drag_data_delete;

464
  widget_class->popup_menu = gtk_entry_popup_menu;
465

466 467
  gtk_object_class->destroy = gtk_entry_destroy;

468 469 470
  class->move_cursor = gtk_entry_move_cursor;
  class->insert_at_cursor = gtk_entry_insert_at_cursor;
  class->delete_from_cursor = gtk_entry_delete_from_cursor;
471
  class->backspace = gtk_entry_backspace;
472 473 474 475
  class->cut_clipboard = gtk_entry_cut_clipboard;
  class->copy_clipboard = gtk_entry_copy_clipboard;
  class->paste_clipboard = gtk_entry_paste_clipboard;
  class->toggle_overwrite = gtk_entry_toggle_overwrite;
476
  class->activate = gtk_entry_real_activate;
477
  class->get_text_area_size = gtk_entry_get_text_area_size;
478
  
479
  quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
480
  quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
481
  quark_cursor_hadjustment = g_quark_from_static_string ("gtk-hadjustment");
482

Havoc Pennington's avatar
Havoc Pennington committed
483
  g_object_class_install_property (gobject_class,
484
                                   PROP_CURSOR_POSITION,
Matthias Clasen's avatar
x  
Matthias Clasen committed
485
                                   g_param_spec_int ("cursor-position",
486 487
                                                     P_("Cursor Position"),
                                                     P_("The current position of the insertion cursor in chars"),
Havoc Pennington's avatar
Havoc Pennington committed
488
                                                     0,
489
                                                     MAX_SIZE,
Havoc Pennington's avatar
Havoc Pennington committed
490
                                                     0,
491
                                                     GTK_PARAM_READABLE));
492 493 494
  
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTION_BOUND,
Matthias Clasen's avatar
x  
Matthias Clasen committed
495
                                   g_param_spec_int ("selection-bound",
496 497
                                                     P_("Selection Bound"),
                                                     P_("The position of the opposite end of the selection from the cursor in chars"),
498
                                                     0,
499
                                                     MAX_SIZE,
500
                                                     0,
501
                                                     GTK_PARAM_READABLE));
Havoc Pennington's avatar
Havoc Pennington committed
502 503 504 505
  
  g_object_class_install_property (gobject_class,
                                   PROP_EDITABLE,
                                   g_param_spec_boolean ("editable",
506 507
							 P_("Editable"),
							 P_("Whether the entry contents can be edited"),
Havoc Pennington's avatar
Havoc Pennington committed
508
                                                         TRUE,
509
							 GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
510 511 512
  
  g_object_class_install_property (gobject_class,
                                   PROP_MAX_LENGTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
513
                                   g_param_spec_int ("max-length",
514 515
                                                     P_("Maximum length"),
                                                     P_("Maximum number of characters for this entry. Zero if no maximum"),
516 517 518
                                                     0,
                                                     MAX_SIZE,
                                                     0,
519
                                                     GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
520 521 522
  g_object_class_install_property (gobject_class,
                                   PROP_VISIBILITY,
                                   g_param_spec_boolean ("visibility",
523 524
							 P_("Visibility"),
							 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
Havoc Pennington's avatar
Havoc Pennington committed
525
                                                         TRUE,
526
							 GTK_PARAM_READWRITE));
527

Havoc Pennington's avatar
Havoc Pennington committed
528
  g_object_class_install_property (gobject_class,
529
                                   PROP_HAS_FRAME,
Matthias Clasen's avatar
x  
Matthias Clasen committed
530
                                   g_param_spec_boolean ("has-frame",
531 532
							 P_("Has Frame"),
							 P_("FALSE removes outside bevel from entry"),
533
                                                         TRUE,
534
							 GTK_PARAM_READWRITE));
535

536 537 538 539
  g_object_class_install_property (gobject_class,
                                   PROP_INNER_BORDER,
                                   g_param_spec_boxed ("inner-border",
                                                       P_("Inner Border"),
540
                                                       P_("Border between text and frame. Overrides the inner-border style property"),
541 542 543
                                                       GTK_TYPE_BORDER,
                                                       GTK_PARAM_READWRITE));

Morten Welinder's avatar
Morten Welinder committed
544
  g_object_class_install_property (gobject_class,
Havoc Pennington's avatar
Havoc Pennington committed
545
                                   PROP_INVISIBLE_CHAR,
Matthias Clasen's avatar
x  
Matthias Clasen committed
546
                                   g_param_spec_unichar ("invisible-char",
547 548
							 P_("Invisible character"),
							 P_("The character to use when masking entry contents (in \"password mode\")"),
549
							 '*',
550
							 GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
551

552 553
  g_object_class_install_property (gobject_class,
                                   PROP_ACTIVATES_DEFAULT,
Matthias Clasen's avatar
x  
Matthias Clasen committed
554
                                   g_param_spec_boolean ("activates-default",
555 556
							 P_("Activates default"),
							 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
557
                                                         FALSE,
558
							 GTK_PARAM_READWRITE));
559 560
  g_object_class_install_property (gobject_class,
                                   PROP_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
561
                                   g_param_spec_int ("width-chars",
562 563
                                                     P_("Width in chars"),
                                                     P_("Number of characters to leave space for in the entry"),
564 565 566
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
567
                                                     GTK_PARAM_READWRITE));
568

569 570
  g_object_class_install_property (gobject_class,
                                   PROP_SCROLL_OFFSET,
Matthias Clasen's avatar
x  
Matthias Clasen committed
571
                                   g_param_spec_int ("scroll-offset",
572 573
                                                     P_("Scroll offset"),
                                                     P_("Number of pixels of the entry scrolled off the screen to the left"),
574 575 576
                                                     0,
                                                     G_MAXINT,
                                                     0,
577
                                                     GTK_PARAM_READABLE));
578 579 580 581

  g_object_class_install_property (gobject_class,
                                   PROP_TEXT,
                                   g_param_spec_string ("text",
582 583
							P_("Text"),
							P_("The contents of the entry"),
584
							"",
585
							GTK_PARAM_READWRITE));
586

587 588 589 590 591 592 593 594
  /**
   * GtkEntry:xalign:
   *
   * The horizontal alignment, from 0 (left) to 1 (right). 
   * Reversed for RTL layouts.
   * 
   * Since: 2.4
   */
595 596 597 598
  g_object_class_install_property (gobject_class,
                                   PROP_XALIGN,
                                   g_param_spec_float ("xalign",
						       P_("X align"),
599
						       P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
600 601 602
						       0.0,
						       1.0,
						       0.0,
603
						       GTK_PARAM_READWRITE));
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

  /**
   * GtkEntry:truncate-multiline:
   *
   * When %TRUE, pasted multi-line text is truncated to the first line.
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TRUNCATE_MULTILINE,
                                   g_param_spec_boolean ("truncate-multiline",
                                                         P_("Truncate multiline"),
                                                         P_("Whether to truncate multiline pastes to one line."),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));

620 621 622
  /**
   * GtkEntry:shadow-type:
   *
623 624
   * Which kind of shadow to draw around the entry when 
   * #GtkEntry:has-frame is set to %TRUE.
625 626 627 628 629 630 631 632 633 634 635 636
   *
   * Since: 2.12
   */
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
                                   g_param_spec_enum ("shadow-type",
                                                      P_("Shadow type"),
                                                      P_("Which kind of shadow to draw around the entry when has-frame is set"),
                                                      GTK_TYPE_SHADOW_TYPE,
                                                      GTK_SHADOW_IN,
                                                      GTK_PARAM_READWRITE));

Tim Janik's avatar
Tim Janik committed
637 638 639 640 641
  /**
   * GtkEntry:overwrite-mode:
   *
   * If text is overwritten when typing in the #GtkEntry.
   *
642
   * Since: 2.14
Tim Janik's avatar
Tim Janik committed
643 644 645 646 647 648 649 650 651 652 653 654 655
   */
  g_object_class_install_property (gobject_class,
                                   PROP_OVERWRITE_MODE,
                                   g_param_spec_boolean ("overwrite-mode",
                                                         P_("Overwrite mode"),
                                                         P_("Whether new text overwrites existing text"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
  /**
   * GtkEntry:text-length:
   *
   * The length of the text in the #GtkEntry.
   *
656
   * Since: 2.14
Tim Janik's avatar
Tim Janik committed
657 658 659 660 661 662 663 664 665 666 667
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TEXT_LENGTH,
                                   g_param_spec_uint ("text-length",
                                                      P_("Text length"),
                                                      P_("Length of the text currently in the entry"),
                                                      0, 
                                                      G_MAXUINT16,
                                                      0,
                                                      GTK_PARAM_READABLE));

668
  signals[POPULATE_POPUP] =
669
    g_signal_new (I_("populate_popup"),
Manish Singh's avatar
Manish Singh committed
670 671 672 673 674 675 676
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_MENU);
677
  
Owen Taylor's avatar
Owen Taylor committed
678 679 680
 /* Action signals */
  
  signals[ACTIVATE] =
681
    g_signal_new (I_("activate"),
Manish Singh's avatar
Manish Singh committed
682 683 684 685 686 687
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, activate),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
688 689 690
  widget_class->activate_signal = signals[ACTIVATE];

  signals[MOVE_CURSOR] = 
691
    g_signal_new (I_("move_cursor"),
Manish Singh's avatar
Manish Singh committed
692 693 694 695 696 697 698 699 700
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
		  G_TYPE_NONE, 3,
		  GTK_TYPE_MOVEMENT_STEP,
		  G_TYPE_INT,
		  G_TYPE_BOOLEAN);
Owen Taylor's avatar
Owen Taylor committed
701 702

  signals[INSERT_AT_CURSOR] = 
703
    g_signal_new (I_("insert_at_cursor"),
Manish Singh's avatar
Manish Singh committed
704 705 706 707 708 709 710
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
		  NULL, NULL,
		  _gtk_marshal_VOID__STRING,
		  G_TYPE_NONE, 1,
		  G_TYPE_STRING);
Owen Taylor's avatar
Owen Taylor committed
711 712

  signals[DELETE_FROM_CURSOR] = 
713
    g_signal_new (I_("delete_from_cursor"),
Manish Singh's avatar
Manish Singh committed
714 715 716 717 718 719 720 721
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM_INT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_DELETE_TYPE,
		  G_TYPE_INT);
Owen Taylor's avatar
Owen Taylor committed
722

723
  signals[BACKSPACE] =
724
    g_signal_new (I_("backspace"),
725 726 727 728 729 730 731
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, backspace),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

Owen Taylor's avatar
Owen Taylor committed
732
  signals[CUT_CLIPBOARD] =
733
    g_signal_new (I_("cut_clipboard"),
Manish Singh's avatar
Manish Singh committed
734 735 736 737 738 739
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
740 741

  signals[COPY_CLIPBOARD] =
742
    g_signal_new (I_("copy_clipboard"),
Manish Singh's avatar
Manish Singh committed
743 744 745 746 747 748
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
749 750

  signals[PASTE_CLIPBOARD] =
751
    g_signal_new (I_("paste_clipboard"),
Manish Singh's avatar
Manish Singh committed
752 753 754 755 756 757
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
758 759

  signals[TOGGLE_OVERWRITE] =
760
    g_signal_new (I_("toggle_overwrite"),
Manish Singh's avatar
Manish Singh committed
761 762 763 764 765 766
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
767 768 769 770 771 772 773 774 775

  /*
   * Key bindings
   */

  binding_set = gtk_binding_set_by_class (class);

  /* Moving the insertion point */
  add_move_binding (binding_set, GDK_Right, 0,
776
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
Owen Taylor's avatar
Owen Taylor committed
777 778
  
  add_move_binding (binding_set, GDK_Left, 0,
779
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);
Owen Taylor's avatar
Owen Taylor committed
780

781 782 783 784 785 786
  add_move_binding (binding_set, GDK_KP_Right, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
  
  add_move_binding (binding_set, GDK_KP_Left, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);
  
Owen Taylor's avatar
Owen Taylor committed
787 788 789 790 791
  add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, -1);
792 793 794 795 796 797

  add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, -1);
Owen Taylor's avatar
Owen Taylor committed
798 799 800 801 802 803
  
  add_move_binding (binding_set, GDK_Home, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

  add_move_binding (binding_set, GDK_End, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
804 805 806 807 808 809

  add_move_binding (binding_set, GDK_KP_Home, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

  add_move_binding (binding_set, GDK_KP_End, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
Owen Taylor's avatar
Owen Taylor committed
810 811 812 813 814 815 816
  
  add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

  add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

817 818 819 820 821 822
  add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

  add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

823 824 825 826 827
  /* Select all
   */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
                                "move_cursor", 3,
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
Manish Singh's avatar
Manish Singh committed
828 829
                                G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);
830 831 832
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
                                "move_cursor", 3,
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
Manish Singh's avatar
Manish Singh committed
833
                                G_TYPE_INT, 1,
834
				G_TYPE_BOOLEAN, TRUE);  
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849
  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
                                "move_cursor", 3,
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
                                G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);
  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
                                "move_cursor", 3,
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
                                G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);  
  /* Unselect all 
   */
  gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
                                "move_cursor", 3,
850 851 852 853 854
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
                                G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
                                "move_cursor", 3,
855 856 857
                                GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
                                G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);
858 859 860

  /* Activate
   */
861 862
  gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
				"activate", 0);
863 864
  gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
				"activate", 0);
865 866 867
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
				"activate", 0);
  
Owen Taylor's avatar
Owen Taylor committed
868 869 870
  /* Deleting text */
  gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
				"delete_from_cursor", 2,
Manish Singh's avatar
Manish Singh committed
871 872
				G_TYPE_ENUM, GTK_DELETE_CHARS,
				G_TYPE_INT, 1);
Owen Taylor's avatar
Owen Taylor committed
873

874 875
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
				"delete_from_cursor", 2,
Manish Singh's avatar
Manish Singh committed
876 877
				G_TYPE_ENUM, GTK_DELETE_CHARS,
				G_TYPE_INT, 1);
878
  
Owen Taylor's avatar
Owen Taylor committed
879
  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
880
				"backspace", 0);
Owen Taylor's avatar
Owen Taylor committed
881

882 883
  /* Make this do the same as Backspace, to help with mis-typing */
  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
884
				"backspace", 0);
885

Owen Taylor's avatar
Owen Taylor committed
886 887
  gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
Manish Singh's avatar
Manish Singh committed
888 889
				G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				G_TYPE_INT, 1);
Owen Taylor's avatar
Owen Taylor committed
890

891 892
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
Manish Singh's avatar
Manish Singh committed
893 894
				G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				G_TYPE_INT, 1);
895
  
Owen Taylor's avatar
Owen Taylor committed
896 897
  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
Manish Singh's avatar
Manish Singh committed
898 899
				G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				G_TYPE_INT, -1);
Owen Taylor's avatar
Owen Taylor committed
900 901 902 903 904 905 906 907 908

  /* Cut/copy/paste */

  gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
				"cut_clipboard", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
				"copy_clipboard", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
				"paste_clipboard", 0);
Elliot Lee's avatar
Elliot Lee committed
909

910 911 912 913 914 915 916
  gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
				"cut_clipboard", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
				"copy_clipboard", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
				"paste_clipboard", 0);

Owen Taylor's avatar
Owen Taylor committed
917 918 919
  /* Overwrite */
  gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
				"toggle_overwrite", 0);
920 921
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
				"toggle_overwrite", 0);
922

923 924 925
  /**
   * GtkEntry:inner-border:
   *
926
   * Sets the text area's border between the text and the frame.
927 928 929 930 931 932 933 934 935 936 937
   *
   * Since: 2.10
   */
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boxed ("inner-border",
                                                               P_("Inner Border"),
                                                               P_("Border between text and frame."),
                                                               GTK_TYPE_BORDER,
                                                               GTK_PARAM_READABLE));

   gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
938 939
						       P_("Select on focus"),
						       P_("Whether to select the contents of an entry when it is focused"),
940
						       TRUE,
941
						       GTK_PARAM_READWRITE));
942

943
  /**
944
   * GtkSettings:gtk-entry-password-hint-timeout:
945
   *
946
   * How long to show the last input character in hidden
947 948 949 950 951
   * entries. This value is in milliseconds. 0 disables showing the
   * last char. 600 is a good value for enabling it.
   *
   * Since: 2.10
   */
952 953
  gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
                                                    P_("Password Hint Timeout"),
954
                                                    P_("How long to show the last input character in hidden entries"),
955 956 957
                                                    0, G_MAXUINT, 0,
                                                    GTK_PARAM_READWRITE));

958
  g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
Owen Taylor's avatar
Owen Taylor committed
959
}
960

Owen Taylor's avatar
Owen Taylor committed
961 962 963
static void
gtk_entry_editable_init (GtkEditableClass *iface)
{
964 965 966 967
  iface->do_insert_text = gtk_entry_insert_text;
  iface->do_delete_text = gtk_entry_delete_text;
  iface->insert_text = gtk_entry_real_insert_text;
  iface->delete_text = gtk_entry_real_delete_text;
Owen Taylor's avatar
Owen Taylor committed
968 969 970 971 972
  iface->get_chars = gtk_entry_get_chars;
  iface->set_selection_bounds = gtk_entry_set_selection_bounds;
  iface->get_selection_bounds = gtk_entry_get_selection_bounds;
  iface->set_position = gtk_entry_real_set_position;
  iface->get_position = gtk_entry_get_position;
Elliot Lee's avatar
Elliot Lee committed
973 974
}

975 976 977 978 979 980
static void
gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_entry_start_editing;
}

981 982 983 984 985
static void
gtk_entry_set_property (GObject         *object,
                        guint            prop_id,
                        const GValue    *value,
                        GParamSpec      *pspec)
986
{
987
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
Owen Taylor's avatar
Owen Taylor committed
988
  GtkEntry *entry = GTK_ENTRY (object);
989

Havoc Pennington's avatar
Havoc Pennington committed
990
  switch (prop_id)
991
    {
Havoc Pennington's avatar
Havoc Pennington committed
992
    case PROP_EDITABLE:
Owen Taylor's avatar
Owen Taylor committed
993
      {
Havoc Pennington's avatar
Havoc Pennington committed
994 995 996
        gboolean new_value = g_value_get_boolean (value);

      	if (new_value != entry->editable)
Owen Taylor's avatar
Owen Taylor committed
997
	  {
998 999
	    if (!new_value)
	      {
1000
		_gtk_entry_reset_im_context (entry);
1001 1002 1003 1004 1005 1006 1007
		if (GTK_WIDGET_HAS_FOCUS (entry))
		  gtk_im_context_focus_out (entry->im_context);

		entry->preedit_length = 0;
		entry->preedit_cursor = 0;
	      }

Owen Taylor's avatar
Owen Taylor committed
1008
	    entry->editable = new_value;
1009

1010 1011 1012 1013
	    if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
	      gtk_im_context_focus_in (entry->im_context);
	    
	    gtk_entry_queue_draw (entry);
Owen Taylor's avatar
Owen Taylor committed
1014 1015 1016
	  }
      }
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1017 1018 1019

    case PROP_MAX_LENGTH:
      gtk_entry_set_max_length (entry, g_value_get_int (value));
1020
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1021 1022 1023
      
    case PROP_VISIBILITY:
      gtk_entry_set_visibility (entry, g_value_get_boolean (value));
1024
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1025

1026 1027 1028 1029
    case PROP_HAS_FRAME:
      gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
      break;

1030 1031 1032 1033
    case PROP_INNER_BORDER:
      gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
      break;

Havoc Pennington's avatar
Havoc Pennington committed
1034
    case PROP_INVISIBLE_CHAR:
1035
      gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
1036
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1037

1038 1039 1040
    case PROP_ACTIVATES_DEFAULT:
      gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
      break;
1041 1042 1043 1044

    case PROP_WIDTH_CHARS:
      gtk_entry_set_width_chars (entry, g_value_get_int (value));
      break;
1045

1046 1047 1048 1049
    case PROP_TEXT:
      gtk_entry_set_text (entry, g_value_get_string (value));
      break;

1050 1051 1052 1053
    case PROP_XALIGN:
      gtk_entry_set_alignment (entry, g_value_get_float (value));
      break;

1054
    case PROP_TRUNCATE_MULTILINE:
1055
      entry->truncate_multiline = g_value_get_boolean (value);
1056 1057
      break;

1058 1059 1060 1061
    case PROP_SHADOW_TYPE:
      priv->shadow_type = g_value_get_enum (value);
      break;

Tim Janik's avatar
Tim Janik committed
1062 1063 1064 1065
    case PROP_OVERWRITE_MODE:
      gtk_entry_set_overwrite_mode (entry, g_value_get_boolean (value));
      break;

1066
    case PROP_SCROLL_OFFSET:
1067
    case PROP_CURSOR_POSITION:
1068
    default:
Havoc Pennington's avatar
Havoc Pennington committed
1069
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1070 1071 1072 1073
      break;
    }
}

1074 1075 1076 1077 1078
static void
gtk_entry_get_property (GObject         *object,
                        guint            prop_id,
                        GValue          *value,
                        GParamSpec      *pspec)
1079
{
1080
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1081
  GtkEntry *entry = GTK_ENTRY (object);
1082

Havoc Pennington's avatar
Havoc Pennington committed
1083
  switch (prop_id)
1084
    {
1085
    case PROP_CURSOR_POSITION:
Havoc Pennington's avatar
Havoc Pennington committed
1086
      g_value_set_int (value, entry->current_pos);
Owen Taylor's avatar
Owen Taylor committed
1087
      break;
1088 1089 1090
    case PROP_SELECTION_BOUND:
      g_value_set_int (value, entry->selection_bound);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1091 1092
    case PROP_EDITABLE:
      g_value_set_boolean (value, entry->editable);
Owen Taylor's avatar
Owen Taylor committed
1093
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1094 1095
    case PROP_MAX_LENGTH:
      g_value_set_int (value, entry->text_max_length); 
1096
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1097 1098
    case PROP_VISIBILITY:
      g_value_set_boolean (value, entry->visible);
1099
      break;
1100 1101 1102
    case PROP_HAS_FRAME:
      g_value_set_boolean (value, entry->has_frame);
      break;
1103 1104 1105
    case PROP_INNER_BORDER:
      g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
      break;
Havoc Pennington's avatar
Havoc Pennington committed
1106
    case PROP_INVISIBLE_CHAR:
1107
      g_value_set_uint (value, entry->invisible_char);
1108
      break;
1109 1110 1111
    case PROP_ACTIVATES_DEFAULT:
      g_value_set_boolean (value, entry->activates_default);
      break;
1112 1113 1114
    case PROP_WIDTH_CHARS:
      g_value_set_int (value, entry->width_chars);
      break;
1115 1116 1117
    case PROP_SCROLL_OFFSET:
      g_value_set_int (value, entry->scroll_offset);
      break;
1118 1119 1120
    case PROP_TEXT:
      g_value_set_string (value, gtk_entry_get_text (entry));
      break;
1121 1122 1123
    case PROP_XALIGN:
      g_value_set_float (value, gtk_entry_get_alignment (entry));
      break;
1124
    case PROP_TRUNCATE_MULTILINE:
1125
      g_value_set_boolean (value, entry->truncate_multiline);
1126
      break;
1127 1128 1129
    case PROP_SHADOW_TYPE:
      g_value_set_enum (value, priv->shadow_type);
      break;
Tim Janik's avatar
Tim Janik committed
1130 1131 1132 1133 1134 1135
    case PROP_OVERWRITE_MODE:
      g_value_set_boolean (value, entry->overwrite_mode);
      break;
    case PROP_TEXT_LENGTH:
      g_value_set_boolean (value, entry->text_length);
      break;
1136

1137
    default:
Havoc Pennington's avatar
Havoc Pennington committed
1138
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1139 1140 1141 1142
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
1143 1144 1145
static void
gtk_entry_init (GtkEntry *entry)
{
1146 1147
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
  
Elliot Lee's avatar
Elliot Lee committed
1148 1149
  GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);

1150 1151 1152
  entry->text_size = MIN_SIZE;
  entry->text = g_malloc (entry->text_size);
  entry->text[0] = '\0';
Owen Taylor's avatar
Owen Taylor committed
1153 1154 1155

  entry->editable = TRUE;
  entry->visible = TRUE;
1156
  entry->invisible_char = '*';
Owen Taylor's avatar
Owen Taylor committed
1157
  entry->dnd_position = -1;
1158
  entry->width_chars = -1;
1159
  entry->is_cell_renderer = FALSE;
1160
  entry->editing_canceled = FALSE;
1161
  entry->has_frame = TRUE;
1162
  entry->truncate_multiline = FALSE;
1163
  priv->shadow_type = GTK_SHADOW_IN;
1164
  priv->xalign = 0.0;
1165

Owen Taylor's avatar
Owen Taylor committed
1166
  gtk_drag_dest_set (GTK_WIDGET (entry),
1167
                     GTK_DEST_DEFAULT_HIGHLIGHT,
1168
                     NULL, 0,
Owen Taylor's avatar
Owen Taylor committed
1169
                     GDK_ACTION_COPY | GDK_ACTION_MOVE);
1170
  gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
Owen Taylor's avatar
Owen Taylor committed
1171

1172 1173 1174 1175 1176
  /* This object is completely private. No external entity can gain a reference
   * to it; so we create it here and destroy it in finalize().
   */
  entry->im_context = gtk_im_multicontext_new ();
  
Manish Singh's avatar
Manish Singh committed
1177
  g_signal_connect (entry->im_context, "commit",
1178
		    G_CALLBACK (gtk_entry_commit_cb), entry);
Manish Singh's avatar
Manish Singh committed
1179
  g_signal_connect (entry->im_context, "preedit_changed",
1180
		    G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
Manish Singh's avatar
Manish Singh committed
1181
  g_signal_connect (entry->im_context, "retrieve_surrounding",
1182
		    G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
Manish Singh's avatar
Manish Singh committed
1183
  g_signal_connect (entry->im_context, "delete_surrounding",
1184
		    G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
Elliot Lee's avatar
Elliot Lee committed
1185 1186
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
static void
begin_change (GtkEntry *entry)
{
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);

  priv->change_count++;
}

static void
end_change (GtkEntry *entry)
{
  GtkEditable *editable = GTK_EDITABLE (entry);
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
 
  g_return_if_fail (priv->change_count > 0);

  priv->change_count--;

  if (priv->change_count == 0)
    {
       if (priv->real_changed) 
         {
           g_signal_emit_by_name (editable, "changed");
           priv->real_changed = FALSE;
         }
    } 
}

static void
emit_changed (GtkEntry *entry)
{
  GtkEditable *editable = GTK_EDITABLE (entry);
  GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);

  if (priv->change_count == 0)
    g_signal_emit_by_name (editable, "changed");
  else 
    priv->real_changed = TRUE;
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
/*
 * Overwrite a memory that might contain sensitive information.
 */
static void
trash_area (gchar *area, gsize len)
{
  volatile gchar *varea = (volatile gchar *)area;
  while (len-- > 0)
    *varea++ = 0;
}

static void
gtk_entry_destroy (GtkObject *object)
{
  GtkEntry *entry = GTK_ENTRY (object);

  entry->n_bytes = 0;
  entry->current_pos = entry->selection_bound = entry->text_length = 0;
1245
  _gtk_entry_reset_im_context (entry);
1246 1247
  gtk_entry_reset_layout (entry);

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
  if (entry->blink_timeout)
    {
      g_source_remove (entry->blink_timeout);
      entry->blink_timeout = 0;
    }

  if (entry->recompute_idle)
    {
      g_source_remove (entry->recompute_idle);
      entry->recompute_idle = 0;
    }

1260 1261 1262 1263 1264 1265
  if (!entry->visible)
    {
      /* We want to trash the text here because the entry might be leaked.  */
      trash_area (entry->text, strlen (entry->text));
    }

Matthias Clasen's avatar
Matthias Clasen committed
1266
  GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1267 1268
}

Elliot Lee's avatar
Elliot Lee committed
1269
static void
1270
gtk_entry_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
1271
{
1272
  GtkEntry *entry = GTK_ENTRY (object);
Elliot Lee's avatar
Elliot Lee committed
1273

1274 1275
  gtk_entry_set_completion (entry, NULL);

Owen Taylor's avatar
Owen Taylor committed
1276
  if (entry->cached_layout)
Manish Singh's avatar
Manish Singh committed
1277
    g_object_unref (entry->cached_layout);
Owen Taylor's avatar
Owen Taylor committed
1278

Manish Singh's avatar
Manish Singh committed
1279
  g_object_unref (entry->im_context);
1280

1281 1282
  if (entry->blink_timeout)
    g_source_remove (entry->blink_timeout);
Owen Taylor's avatar
Owen Taylor committed
1283 1284 1285

  if (entry->recompute_idle)
    g_source_remove (entry->recompute_idle);
Elliot Lee's avatar
Elliot Lee committed
1286

1287
  entry->text_size = 0;
1288

Elliot Lee's avatar
Elliot Lee committed
1289
  if (entry->text)
1290 1291 1292 1293 1294 1295
    {
      if (!entry->visible)
	trash_area (entry->text, strlen (entry->text));
      g_free (entry->text);
      entry->text = NULL;
    }
Elliot Lee's avatar
Elliot Lee committed
1296

Matthias Clasen's avatar
Matthias Clasen committed
1297
  G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
}

static void
gtk_entry_realize (GtkWidget *widget)
{
  GtkEntry *entry;
  GdkWindowAttr attributes;
  gint attributes_mask;

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
  entry = GTK_ENTRY (widget);
1309

Elliot Lee's avatar
Elliot Lee committed
1310
  attributes.window_type = GDK_WINDOW_CHILD;
1311 1312 1313
  
  get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);

Elliot Lee's avatar
Elliot Lee committed
1314 1315 1316 1317 1318 1319 1320 1321 1322
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
			    GDK_BUTTON_PRESS_MASK |
			    GDK_BUTTON_RELEASE_MASK |
			    GDK_BUTTON1_MOTION_MASK |
			    GDK_BUTTON3_MOTION_MASK |
1323
			    GDK_POINTER_MOTION_HINT_MASK |
1324
			    GDK_POINTER_MOTION_MASK |
1325 1326
                            GDK_ENTER_NOTIFY_MASK |
			    GDK_LEAVE_NOTIFY_MASK);
Elliot Lee's avatar
Elliot Lee committed
1327 1328
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

1329
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);