gtkentry.c 68.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* 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
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * 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
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
28 29 30

#include <pango/pango.h>

Elliot Lee's avatar
Elliot Lee committed
31
#include "gdk/gdkkeysyms.h"
Owen Taylor's avatar
Owen Taylor committed
32 33
#include "gtkbindings.h"
#include "gtkclipboard.h"
Elliot Lee's avatar
Elliot Lee committed
34
#include "gtkentry.h"
35
#include "gtkimmulticontext.h"
Owen Taylor's avatar
Owen Taylor committed
36
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
37
#include "gtkmain.h"
Owen Taylor's avatar
Owen Taylor committed
38 39
#include "gtkmenu.h"
#include "gtkmenuitem.h"
Elliot Lee's avatar
Elliot Lee committed
40 41
#include "gtkselection.h"
#include "gtksignal.h"
42

Elliot Lee's avatar
Elliot Lee committed
43 44 45 46
#define MIN_ENTRY_WIDTH  150
#define DRAW_TIMEOUT     20
#define INNER_BORDER     2

47 48 49 50 51 52
/* Initial size of buffer, in bytes */
#define MIN_SIZE 16

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

Owen Taylor's avatar
Owen Taylor committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
enum {
  INSERT_TEXT,
  DELETE_TEXT,
  CHANGED,
  ACTIVATE,
  MOVE_CURSOR,
  INSERT_AT_CURSOR,
  DELETE_FROM_CURSOR,
  CUT_CLIPBOARD,
  COPY_CLIPBOARD,
  PASTE_CLIPBOARD,
  TOGGLE_OVERWRITE,
  LAST_SIGNAL
};

68 69
enum {
  ARG_0,
Owen Taylor's avatar
Owen Taylor committed
70 71
  ARG_TEXT_POSITION,
  ARG_EDITABLE,
72
  ARG_MAX_LENGTH,
73 74
  ARG_VISIBILITY,
  ARG_INVISIBLE_CHAR
75 76
};

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

Owen Taylor's avatar
Owen Taylor committed
79 80
/* GObject, GtkObject methods
 */
81
static void   gtk_entry_class_init           (GtkEntryClass    *klass);
Owen Taylor's avatar
Owen Taylor committed
82
static void   gtk_entry_editable_init        (GtkEditableClass *iface);
83 84 85 86 87 88 89 90
static void   gtk_entry_init                 (GtkEntry         *entry);
static void   gtk_entry_set_arg              (GtkObject        *object,
					      GtkArg           *arg,
					      guint             arg_id);
static void   gtk_entry_get_arg              (GtkObject        *object,
					      GtkArg           *arg,
					      guint             arg_id);
static void   gtk_entry_finalize             (GObject          *object);
Owen Taylor's avatar
Owen Taylor committed
91 92 93

/* GtkWidget methods
 */
94 95 96 97 98 99 100 101
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);
static void   gtk_entry_draw                 (GtkWidget        *widget,
					      GdkRectangle     *area);
Owen Taylor's avatar
Owen Taylor committed
102
static void   gtk_entry_draw_focus           (GtkWidget        *widget);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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);
static gint   gtk_entry_focus_in             (GtkWidget        *widget,
					      GdkEventFocus    *event);
static gint   gtk_entry_focus_out            (GtkWidget        *widget,
					      GdkEventFocus    *event);
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);
Owen Taylor's avatar
Owen Taylor committed
123

Owen Taylor's avatar
Owen Taylor committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/* 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);

/* Default signal handlers
 */
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static void gtk_entry_real_insert_text   (GtkEntry        *entry,
					  const gchar     *new_text,
					  gint             new_text_length,
					  gint            *position);
static void gtk_entry_real_delete_text   (GtkEntry        *entry,
					  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);
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);
Owen Taylor's avatar
Owen Taylor committed
168 169 170

/* IM Context Callbacks
 */
171 172 173
static void gtk_entry_commit_cb           (GtkIMContext      *context,
					   const gchar       *str,
					   GtkEntry          *entry);
Owen Taylor's avatar
Owen Taylor committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static void gtk_entry_preedit_changed_cb  (GtkIMContext      *context,
					   GtkEntry          *entry);
/* Internal routines
 */
static void         gtk_entry_draw_text                (GtkEntry       *entry);
static void         gtk_entry_draw_cursor              (GtkEntry       *entry);
static PangoLayout *gtk_entry_get_layout               (GtkEntry       *entry,
							gboolean        include_preedit);
static void         gtk_entry_queue_draw               (GtkEntry       *entry);
static void         gtk_entry_reset_im_context         (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,
							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);
static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
							gint            start);
static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
							gint            start);
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);
static void         gtk_entry_popup_menu               (GtkEntry       *entry,
							GdkEventButton *event);
209

Elliot Lee's avatar
Elliot Lee committed
210 211
static GtkWidgetClass *parent_class = NULL;

212
GtkType
213
gtk_entry_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
214
{
215
  static GtkType entry_type = 0;
Elliot Lee's avatar
Elliot Lee committed
216 217 218

  if (!entry_type)
    {
219
      static const GtkTypeInfo entry_info =
Elliot Lee's avatar
Elliot Lee committed
220 221 222 223 224 225
      {
	"GtkEntry",
	sizeof (GtkEntry),
	sizeof (GtkEntryClass),
	(GtkClassInitFunc) gtk_entry_class_init,
	(GtkObjectInitFunc) gtk_entry_init,
226 227
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
228
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
229
      };
Owen Taylor's avatar
Owen Taylor committed
230 231 232 233 234 235 236
      
      static const GInterfaceInfo editable_info =
      {
	(GInterfaceInitFunc) gtk_entry_editable_init,	 /* interface_init */
	NULL,			                         /* interface_finalize */
	NULL			                         /* interface_data */
      };
Elliot Lee's avatar
Elliot Lee committed
237

Owen Taylor's avatar
Owen Taylor committed
238 239 240 241
      entry_type = gtk_type_unique (GTK_TYPE_WIDGET, &entry_info);
      g_type_add_interface_static (entry_type,
				   GTK_TYPE_EDITABLE,
				   &editable_info);
Elliot Lee's avatar
Elliot Lee committed
242 243 244 245 246
    }

  return entry_type;
}

Owen Taylor's avatar
Owen Taylor committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
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,
				GTK_TYPE_ENUM, step,
				G_TYPE_INT, count,
                                G_TYPE_BOOLEAN, FALSE);

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
				"move_cursor", 3,
				GTK_TYPE_ENUM, step,
				G_TYPE_INT, count,
                                G_TYPE_BOOLEAN, TRUE);
}

Elliot Lee's avatar
Elliot Lee committed
270 271 272
static void
gtk_entry_class_init (GtkEntryClass *class)
{
273
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
274 275
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
Owen Taylor's avatar
Owen Taylor committed
276 277

  GtkBindingSet *binding_set;
Elliot Lee's avatar
Elliot Lee committed
278 279 280

  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
Owen Taylor's avatar
Owen Taylor committed
281
  parent_class = gtk_type_class (GTK_TYPE_WIDGET);
Elliot Lee's avatar
Elliot Lee committed
282

283 284
  gobject_class->finalize = gtk_entry_finalize;

Owen Taylor's avatar
Owen Taylor committed
285 286 287 288
  gtk_object_add_arg_type ("GtkEntry::text_position", GTK_TYPE_INT,  GTK_ARG_READWRITE, ARG_TEXT_POSITION);
  gtk_object_add_arg_type ("GtkEntry::editable",      GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_EDITABLE);
  gtk_object_add_arg_type ("GtkEntry::max_length",    GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_MAX_LENGTH);
  gtk_object_add_arg_type ("GtkEntry::visibility",    GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_VISIBILITY);
289 290
  gtk_object_add_arg_type ("GtkEntry::invisible_char", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_INVISIBLE_CHAR);
  
Owen Taylor's avatar
Owen Taylor committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  signals[INSERT_TEXT] =
    gtk_signal_new ("insert_text",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkEntryClass, insert_text),
		    gtk_marshal_VOID__STRING_INT_POINTER,
		    GTK_TYPE_NONE,
		    3,
		    GTK_TYPE_STRING,
		    GTK_TYPE_INT,
		    GTK_TYPE_POINTER);

  signals[DELETE_TEXT] =
    gtk_signal_new ("delete_text",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkEntryClass, delete_text),
		    gtk_marshal_VOID__INT_INT,
		    GTK_TYPE_NONE,
		    2,
		    GTK_TYPE_INT,
		    GTK_TYPE_INT);		    

  signals[CHANGED] =
    gtk_signal_new ("changed",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkEntryClass, changed),
		    gtk_marshal_VOID__VOID,
		    GTK_TYPE_NONE, 0);

 /* Action signals */
  
  signals[ACTIVATE] =
    gtk_signal_new ("activate",
		    GTK_RUN_LAST | GTK_RUN_ACTION,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkEntryClass, activate),
		    gtk_marshal_VOID__VOID,
		    GTK_TYPE_NONE, 0);

  widget_class->activate_signal = signals[ACTIVATE];

  signals[MOVE_CURSOR] = 
      gtk_signal_new ("move_cursor",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
338
                      GTK_SIGNAL_OFFSET (GtkEntryClass, move_cursor),
Owen Taylor's avatar
Owen Taylor committed
339 340 341 342 343 344 345
                      gtk_marshal_VOID__ENUM_INT_BOOLEAN,
                      GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);

  signals[INSERT_AT_CURSOR] = 
      gtk_signal_new ("insert_at_cursor",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
346
                      GTK_SIGNAL_OFFSET (GtkEntryClass, insert_at_cursor),
Owen Taylor's avatar
Owen Taylor committed
347 348 349 350 351 352 353
                      gtk_marshal_VOID__STRING,
                      GTK_TYPE_NONE, 1, GTK_TYPE_STRING);

  signals[DELETE_FROM_CURSOR] = 
      gtk_signal_new ("delete_from_cursor",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
354
                      GTK_SIGNAL_OFFSET (GtkEntryClass, delete_from_cursor),
Owen Taylor's avatar
Owen Taylor committed
355 356 357 358 359 360 361 362 363 364 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                      gtk_marshal_VOID__ENUM_INT,
                      GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);

  signals[CUT_CLIPBOARD] =
    gtk_signal_new ("cut_clipboard",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkEntryClass, cut_clipboard),
                    gtk_marshal_VOID__VOID,
                    GTK_TYPE_NONE, 0);

  signals[COPY_CLIPBOARD] =
    gtk_signal_new ("copy_clipboard",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkEntryClass, copy_clipboard),
                    gtk_marshal_VOID__VOID,
                    GTK_TYPE_NONE, 0);

  signals[PASTE_CLIPBOARD] =
    gtk_signal_new ("paste_clipboard",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkEntryClass, paste_clipboard),
                    gtk_marshal_VOID__VOID,
                    GTK_TYPE_NONE, 0);

  signals[TOGGLE_OVERWRITE] =
    gtk_signal_new ("toggle_overwrite",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkEntryClass, toggle_overwrite),
                    gtk_marshal_VOID__VOID,
                    GTK_TYPE_NONE, 0);

  gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);

  /*
   * Key bindings
   */

  binding_set = gtk_binding_set_by_class (class);

  /* Moving the insertion point */
  add_move_binding (binding_set, GDK_Right, 0,
		    GTK_MOVEMENT_POSITIONS, 1);
  
  add_move_binding (binding_set, GDK_Left, 0,
		    GTK_MOVEMENT_POSITIONS, -1);

  add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_CHARS, 1);
  
  add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_CHARS, -1);
  
  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);
  
  add_move_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_PARAGRAPH_ENDS, -1);

  add_move_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_PARAGRAPH_ENDS, 1);

  add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
		    GTK_MOVEMENT_WORDS, -1);

  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);
  
  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);

  /* Deleting text */
  gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_CHARS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_CHARS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_CHARS,
				GTK_TYPE_INT, -1);

  gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_MOD1_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
				GTK_TYPE_INT, -1);

  gtk_binding_entry_add_signal (binding_set, GDK_k, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPHS,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
				GTK_TYPE_INT, 1);
  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
				"insert_at_cursor", 1,
				GTK_TYPE_STRING, " ");

  gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_MOD1_MASK,
				"delete_from_cursor", 2,
				GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
				GTK_TYPE_INT, 1);
  
  /* 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_w, 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
508

Owen Taylor's avatar
Owen Taylor committed
509 510 511 512 513 514 515
  gtk_binding_entry_add_signal (binding_set, GDK_y, GDK_CONTROL_MASK,
				"paste_clipboard", 0);

  /* Overwrite */
  gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
				"toggle_overwrite", 0);
  
516 517
  object_class->set_arg = gtk_entry_set_arg;
  object_class->get_arg = gtk_entry_get_arg;
Elliot Lee's avatar
Elliot Lee committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531

  widget_class->realize = gtk_entry_realize;
  widget_class->unrealize = gtk_entry_unrealize;
  widget_class->draw_focus = gtk_entry_draw_focus;
  widget_class->size_request = gtk_entry_size_request;
  widget_class->size_allocate = gtk_entry_size_allocate;
  widget_class->draw = gtk_entry_draw;
  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;
  widget_class->focus_in_event = gtk_entry_focus_in;
  widget_class->focus_out_event = gtk_entry_focus_out;
532
  widget_class->style_set = gtk_entry_style_set;
533
  widget_class->direction_changed = gtk_entry_direction_changed;
534
  widget_class->state_changed = gtk_entry_state_changed;
535

Owen Taylor's avatar
Owen Taylor committed
536 537
  class->insert_text = gtk_entry_real_insert_text;
  class->delete_text = gtk_entry_real_delete_text;
538 539 540
  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;
Owen Taylor's avatar
Owen Taylor committed
541 542 543 544 545
  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;
}
546

Owen Taylor's avatar
Owen Taylor committed
547 548 549 550 551 552 553 554 555 556
static void
gtk_entry_editable_init (GtkEditableClass *iface)
{
  iface->insert_text = gtk_entry_insert_text;
  iface->delete_text = gtk_entry_delete_text;
  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
557 558
}

559 560 561 562 563
static void
gtk_entry_set_arg (GtkObject      *object,
		   GtkArg         *arg,
		   guint           arg_id)
{
Owen Taylor's avatar
Owen Taylor committed
564
  GtkEntry *entry = GTK_ENTRY (object);
565 566 567

  switch (arg_id)
    {
Owen Taylor's avatar
Owen Taylor committed
568 569 570 571 572 573 574 575 576 577 578 579 580
    case ARG_TEXT_POSITION:
      gtk_editable_set_position (GTK_EDITABLE (object), GTK_VALUE_INT (*arg));
      break;
    case ARG_EDITABLE:
      {
	gboolean new_value = GTK_VALUE_BOOL (*arg) != 0;
	if (new_value != entry->editable)
	  {
	    entry->editable = new_value;
	    gtk_entry_queue_draw (entry);
	  }
      }
      break;
581 582 583 584 585 586
    case ARG_MAX_LENGTH:
      gtk_entry_set_max_length (entry, GTK_VALUE_UINT (*arg));
      break;
    case ARG_VISIBILITY:
      gtk_entry_set_visibility (entry, GTK_VALUE_BOOL (*arg));
      break;
587 588 589
    case ARG_INVISIBLE_CHAR:
      gtk_entry_set_invisible_char (entry, GTK_VALUE_INT (*arg));
      break;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    default:
      break;
    }
}

static void
gtk_entry_get_arg (GtkObject      *object,
		   GtkArg         *arg,
		   guint           arg_id)
{
  GtkEntry *entry;

  entry = GTK_ENTRY (object);

  switch (arg_id)
    {
Owen Taylor's avatar
Owen Taylor committed
606 607 608 609 610 611
    case ARG_TEXT_POSITION:
      GTK_VALUE_INT (*arg) = entry->current_pos;
      break;
    case ARG_EDITABLE:
      GTK_VALUE_BOOL (*arg) = entry->editable;
      break;
612 613 614 615
    case ARG_MAX_LENGTH:
      GTK_VALUE_UINT (*arg) = entry->text_max_length;
      break;
    case ARG_VISIBILITY:
Owen Taylor's avatar
Owen Taylor committed
616
      GTK_VALUE_BOOL (*arg) = entry->visible;
617
      break;
618 619 620
    case ARG_INVISIBLE_CHAR:
      GTK_VALUE_INT (*arg) = entry->invisible_char;
      break;
621 622 623 624 625 626
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
627 628 629 630 631
static void
gtk_entry_init (GtkEntry *entry)
{
  GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);

632 633 634
  entry->text_size = MIN_SIZE;
  entry->text = g_malloc (entry->text_size);
  entry->text[0] = '\0';
Owen Taylor's avatar
Owen Taylor committed
635 636 637

  entry->editable = TRUE;
  entry->visible = TRUE;
638 639
  entry->invisible_char = '*';
  
640 641 642 643 644 645 646
  /* 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 ();
  
  gtk_signal_connect (GTK_OBJECT (entry->im_context), "commit",
		      GTK_SIGNAL_FUNC (gtk_entry_commit_cb), entry);
Owen Taylor's avatar
Owen Taylor committed
647 648
  gtk_signal_connect (GTK_OBJECT (entry->im_context), "preedit_changed",
		      GTK_SIGNAL_FUNC (gtk_entry_preedit_changed_cb), entry);
Elliot Lee's avatar
Elliot Lee committed
649 650 651
}

static void
652
gtk_entry_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
653 654 655 656 657 658 659
{
  GtkEntry *entry;

  g_return_if_fail (GTK_IS_ENTRY (object));

  entry = GTK_ENTRY (object);

Owen Taylor's avatar
Owen Taylor committed
660 661
  if (entry->cached_layout)
    g_object_unref (G_OBJECT (entry->cached_layout));
Owen Taylor's avatar
Owen Taylor committed
662

663 664
  gtk_object_unref (GTK_OBJECT (entry->im_context));

Elliot Lee's avatar
Elliot Lee committed
665
  if (entry->timer)
Owen Taylor's avatar
Owen Taylor committed
666 667 668 669
    g_source_remove (entry->timer);

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

671
  entry->text_size = 0;
672

Elliot Lee's avatar
Elliot Lee committed
673 674 675 676
  if (entry->text)
    g_free (entry->text);
  entry->text = NULL;

677
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
678 679 680 681 682 683
}

static void
gtk_entry_realize (GtkWidget *widget)
{
  GtkEntry *entry;
684
  GtkEditable *editable;
685
  GtkRequisition requisition;
Elliot Lee's avatar
Elliot Lee committed
686 687 688 689 690 691 692 693
  GdkWindowAttr attributes;
  gint attributes_mask;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
  entry = GTK_ENTRY (widget);
694
  editable = GTK_EDITABLE (widget);
695 696

  gtk_widget_get_child_requisition (widget, &requisition);
697
  
Elliot Lee's avatar
Elliot Lee committed
698 699
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
700
  attributes.y = widget->allocation.y + (widget->allocation.height -
701
					 requisition.height) / 2;
Elliot Lee's avatar
Elliot Lee committed
702
  attributes.width = widget->allocation.width;
703
  attributes.height = requisition.height;
Elliot Lee's avatar
Elliot Lee committed
704 705 706 707 708 709 710 711 712
  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 |
Owen Taylor's avatar
Owen Taylor committed
713
			    GDK_POINTER_MOTION_HINT_MASK);
Elliot Lee's avatar
Elliot Lee committed
714 715
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

716
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
Elliot Lee's avatar
Elliot Lee committed
717 718
  gdk_window_set_user_data (widget->window, entry);

719 720
  attributes.x = widget->style->xthickness;
  attributes.y = widget->style->ythickness;
Elliot Lee's avatar
Elliot Lee committed
721
  attributes.width = widget->allocation.width - attributes.x * 2;
722
  attributes.height = requisition.height - attributes.y * 2;
Owen Taylor's avatar
Owen Taylor committed
723
  attributes.cursor = gdk_cursor_new (GDK_XTERM);
724
  attributes_mask |= GDK_WA_CURSOR;
Elliot Lee's avatar
Elliot Lee committed
725 726 727 728 729 730

  entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
  gdk_window_set_user_data (entry->text_area, entry);

  widget->style = gtk_style_attach (widget->style, widget->window);

731 732
  gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
  gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
Elliot Lee's avatar
Elliot Lee committed
733 734

  gdk_window_show (entry->text_area);
735

736
  gtk_im_context_set_client_window (entry->im_context, entry->text_area);
737

Owen Taylor's avatar
Owen Taylor committed
738
  gtk_entry_adjust_scroll (entry);
Elliot Lee's avatar
Elliot Lee committed
739 740 741 742 743 744 745 746 747 748 749 750
}

static void
gtk_entry_unrealize (GtkWidget *widget)
{
  GtkEntry *entry;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));

  entry = GTK_ENTRY (widget);

751 752
  gtk_im_context_set_client_window (entry->im_context, entry->text_area);
  
Elliot Lee's avatar
Elliot Lee committed
753 754 755 756
  if (entry->text_area)
    {
      gdk_window_set_user_data (entry->text_area, NULL);
      gdk_window_destroy (entry->text_area);
757
      entry->text_area = NULL;
Elliot Lee's avatar
Elliot Lee committed
758
    }
759

Owen Taylor's avatar
Owen Taylor committed
760 761 762
  if (entry->popup_menu)
    gtk_widget_destroy (entry->popup_menu);

763 764
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
Elliot Lee's avatar
Elliot Lee committed
765 766 767 768 769 770
}

static void
gtk_entry_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
771 772 773 774 775
  GtkEntry *entry;
  PangoFontMetrics metrics;
  PangoFont *font;
  gchar *lang;
  
Elliot Lee's avatar
Elliot Lee committed
776 777 778 779
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));
  g_return_if_fail (requisition != NULL);

780
  entry = GTK_ENTRY (widget);
781
  
782 783
  /* hackish for now, get metrics
   */
Owen Taylor's avatar
Owen Taylor committed
784
  font = pango_context_load_font (gtk_widget_get_pango_context (widget),
785
				  widget->style->font_desc);
Owen Taylor's avatar
Owen Taylor committed
786
  lang = pango_context_get_lang (gtk_widget_get_pango_context (widget));
787 788 789 790 791 792
  pango_font_get_metrics (font, lang, &metrics);
  g_free (lang);
  
  g_object_unref (G_OBJECT (font));

  entry->ascent = metrics.ascent;
793
  entry->descent = metrics.descent;
794
  
795
  requisition->width = MIN_ENTRY_WIDTH + (widget->style->xthickness + INNER_BORDER) * 2;
796
  requisition->height = ((metrics.ascent + metrics.descent) / PANGO_SCALE + 
797
			 (widget->style->ythickness + INNER_BORDER) * 2);
Elliot Lee's avatar
Elliot Lee committed
798 799 800 801 802 803 804
}

static void
gtk_entry_size_allocate (GtkWidget     *widget,
			 GtkAllocation *allocation)
{
  GtkEntry *entry;
805
  GtkEditable *editable;
Elliot Lee's avatar
Elliot Lee committed
806 807 808 809 810 811 812

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));
  g_return_if_fail (allocation != NULL);

  widget->allocation = *allocation;
  entry = GTK_ENTRY (widget);
813
  editable = GTK_EDITABLE (widget);
Elliot Lee's avatar
Elliot Lee committed
814 815 816

  if (GTK_WIDGET_REALIZED (widget))
    {
817 818 819 820 821 822 823
      /* We call gtk_widget_get_child_requisition, since we want (for
       * backwards compatibility reasons) the realization here to
       * be affected by the usize of the entry, if set
       */
      GtkRequisition requisition;
      gtk_widget_get_child_requisition (widget, &requisition);
  
Elliot Lee's avatar
Elliot Lee committed
824 825
      gdk_window_move_resize (widget->window,
			      allocation->x,
826 827
			      allocation->y + (allocation->height - requisition.height) / 2,
			      allocation->width, requisition.height);
Elliot Lee's avatar
Elliot Lee committed
828
      gdk_window_move_resize (entry->text_area,
829 830 831 832
			      widget->style->xthickness,
			      widget->style->ythickness,
			      allocation->width - widget->style->xthickness * 2,
			      requisition.height - widget->style->ythickness * 2);
Elliot Lee's avatar
Elliot Lee committed
833

Owen Taylor's avatar
Owen Taylor committed
834
      gtk_entry_recompute (entry);
Elliot Lee's avatar
Elliot Lee committed
835 836 837 838 839 840 841
    }
}

static void
gtk_entry_draw (GtkWidget    *widget,
		GdkRectangle *area)
{
842 843
  GtkEntry *entry;
  
Elliot Lee's avatar
Elliot Lee committed
844 845 846 847
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));
  g_return_if_fail (area != NULL);

848 849
  entry = GTK_ENTRY (widget);
  
Elliot Lee's avatar
Elliot Lee committed
850 851
  if (GTK_WIDGET_DRAWABLE (widget))
    {
852 853
      GdkRectangle tmp_area = *area;

854 855
      tmp_area.x -= widget->style->xthickness;
      tmp_area.y -= widget->style->xthickness;
856 857
      
      gdk_window_begin_paint_rect (entry->text_area, &tmp_area);
Elliot Lee's avatar
Elliot Lee committed
858 859
      gtk_widget_draw_focus (widget);
      gtk_entry_draw_text (GTK_ENTRY (widget));
860
      gtk_entry_draw_cursor (GTK_ENTRY (widget));
861
      gdk_window_end_paint (entry->text_area);
Elliot Lee's avatar
Elliot Lee committed
862 863 864
    }
}

Owen Taylor's avatar
Owen Taylor committed
865 866
static void
gtk_entry_draw_focus (GtkWidget *widget)
Elliot Lee's avatar
Elliot Lee committed
867
{
Owen Taylor's avatar
Owen Taylor committed
868 869
  gint width, height;
  gint x, y;
Elliot Lee's avatar
Elliot Lee committed
870

Owen Taylor's avatar
Owen Taylor committed
871 872
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));
Elliot Lee's avatar
Elliot Lee committed
873

Owen Taylor's avatar
Owen Taylor committed
874
  if (GTK_WIDGET_DRAWABLE (widget))
875
    {
Owen Taylor's avatar
Owen Taylor committed
876 877 878
      x = 0;
      y = 0;
      gdk_window_get_size (widget->window, &width, &height);
Elliot Lee's avatar
Elliot Lee committed
879

Owen Taylor's avatar
Owen Taylor committed
880 881 882 883 884 885 886
      if (GTK_WIDGET_HAS_FOCUS (widget))
	{
	  x += 1;
	  y += 1;
	  width -= 2;
	  height -= 2;
	}
Elliot Lee's avatar
Elliot Lee committed
887

Owen Taylor's avatar
Owen Taylor committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
      gtk_paint_shadow (widget->style, widget->window,
			GTK_STATE_NORMAL, GTK_SHADOW_IN,
			NULL, widget, "entry",
			x, y, width, height);

      if (GTK_WIDGET_HAS_FOCUS (widget))
	{
	   gdk_window_get_size (widget->window, &width, &height);
	   gtk_paint_focus (widget->style, widget->window, 
			    NULL, widget, "entry",
			    0, 0, width - 1, height - 1);
	}
    }
}

static gint
gtk_entry_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
Elliot Lee's avatar
Elliot Lee committed
906 907 908 909 910 911 912
{
  GtkEntry *entry;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_ENTRY (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

Owen Taylor's avatar
Owen Taylor committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  entry = GTK_ENTRY (widget);

  if (widget->window == event->window)
    gtk_widget_draw_focus (widget);
  else if (entry->text_area == event->window)
    {
      gtk_entry_draw_text (GTK_ENTRY (widget));
      gtk_entry_draw_cursor (GTK_ENTRY (widget));
    }

  return FALSE;
}

static gint
gtk_entry_button_press (GtkWidget      *widget,
			GdkEventButton *event)
{
  GtkEntry *entry = GTK_ENTRY (widget);
  GtkEditable *editable = GTK_EDITABLE (widget);
  gint tmp_pos;
933

Elliot Lee's avatar
Elliot Lee committed
934
  entry = GTK_ENTRY (widget);
935 936
  editable = GTK_EDITABLE (widget);
  
Owen Taylor's avatar
Owen Taylor committed
937 938
  if (event->window != entry->text_area ||
      (entry->button && event->button != entry->button))
939
    return FALSE;
Owen Taylor's avatar
Owen Taylor committed
940 941 942

  entry->button = event->button;
  
Elliot Lee's avatar
Elliot Lee committed
943 944
  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);
Owen Taylor's avatar
Owen Taylor committed
945 946 947
  
  tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
    
Elliot Lee's avatar
Elliot Lee committed
948 949 950 951 952
  if (event->button == 1)
    {
      switch (event->type)
	{
	case GDK_BUTTON_PRESS:
Owen Taylor's avatar
Owen Taylor committed
953 954 955 956 957 958 959
	  gtk_entry_reset_im_context (entry);
	  
	  entry->current_pos = tmp_pos;
	  entry->selection_bound = tmp_pos;

	  gtk_entry_recompute (entry);
	  
Elliot Lee's avatar
Elliot Lee committed
960 961 962
	  break;

	case GDK_2BUTTON_PRESS:
Owen Taylor's avatar
Owen Taylor committed
963
	  gtk_entry_select_word (entry);
Elliot Lee's avatar
Elliot Lee committed
964 965 966
	  break;

	case GDK_3BUTTON_PRESS:
Owen Taylor's avatar
Owen Taylor committed
967
	  gtk_entry_select_line (entry);
Elliot Lee's avatar
Elliot Lee committed
968 969 970 971 972
	  break;

	default:
	  break;
	}
Owen Taylor's avatar
Owen Taylor committed
973 974

      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
975
    }
Owen Taylor's avatar
Owen Taylor committed
976
  else if (event->button == 2 && event->type == GDK_BUTTON_PRESS && entry->editable)
Elliot Lee's avatar
Elliot Lee committed
977
    {
Owen Taylor's avatar
Owen Taylor committed
978 979
      gtk_editable_select_region (editable, tmp_pos, tmp_pos);
      gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
980

Owen Taylor's avatar
Owen Taylor committed
981 982 983 984 985 986
      return TRUE;
    }
  else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
    {
      gtk_entry_popup_menu (entry, event);
      entry->button = 0;	/* Don't wait for release, since the menu will gtk_grab_add */
Owen Taylor's avatar
Owen Taylor committed
987 988

      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
989 990 991 992 993 994 995 996 997
    }

  return FALSE;
}

static gint
gtk_entry_button_release (GtkWidget      *widget,
			  GdkEventButton *event)
{
Owen Taylor's avatar
Owen Taylor committed
998 999
  GtkEntry *entry = GTK_ENTRY (widget);
  GtkEditable *editable = GTK_EDITABLE (widget);
Owen Taylor's avatar
Owen Taylor committed
1000

Owen Taylor's avatar
Owen Taylor committed
1001
  if (event->window != entry->text_area || entry->button != event->button)
Owen Taylor's avatar
Owen Taylor committed
1002 1003 1004 1005
    return FALSE;

  entry->button = 0;
  
Elliot Lee's avatar
Elliot Lee committed
1006 1007 1008 1009 1010 1011 1012
  return FALSE;
}

static gint
gtk_entry_motion_notify (GtkWidget      *widget,
			 GdkEventMotion *event)
{
Owen Taylor's avatar
Owen Taylor committed
1013 1014
  GtkEntry *entry = GTK_ENTRY (widget);
  gint tmp_pos;
Elliot Lee's avatar
Elliot Lee committed
1015

Owen Taylor's avatar
Owen Taylor committed
1016
  if (event->window != entry->text_area || entry->button != 1)
Owen Taylor's avatar
Owen Taylor committed
1017 1018
    return FALSE;

Elliot Lee's avatar
Elliot Lee committed
1019
  if (event->is_hint || (entry->text_area != event->window))
Owen Taylor's avatar
Owen Taylor committed
1020
    gdk_window_get_pointer (entry->text_area, NULL, NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
1021

Owen Taylor's avatar
Owen Taylor committed
1022
  tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
Elliot Lee's avatar
Elliot Lee committed
1023

Owen Taylor's avatar
Owen Taylor committed
1024 1025 1026 1027 1028 1029
  if (tmp_pos != entry->current_pos)
    {
      entry->current_pos = tmp_pos;
      gtk_entry_recompute (entry);
    }
      
Owen Taylor's avatar
Owen Taylor committed
1030
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1031 1032 1033 1034 1035 1036
}

static gint
gtk_entry_key_press (GtkWidget   *widget,
		     GdkEventKey *event)
{
Owen Taylor's avatar
Owen Taylor committed
1037
  GtkEntry *entry = GTK_ENTRY (widget);
1038

Owen Taylor's avatar
Owen Taylor committed
1039 1040
  if (!entry->editable)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1041

1042 1043 1044 1045 1046 1047 1048 1049
  if (gtk_im_context_filter_keypress (entry->im_context, event))
    {
      entry->need_im_reset = TRUE;
      return TRUE;
    }
  else if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
    /* Activate key bindings
     */
Owen Taylor's avatar
Owen Taylor committed
1050
    return TRUE;
1051 1052 1053 1054 1055
  else if (event->keyval == GDK_Return)
    {
      gtk_widget_activate (widget);
      return TRUE;
    }
Owen Taylor's avatar
Owen Taylor committed
1056

1057
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
1058 1059 1060 1061 1062 1063
}

static gint
gtk_entry_focus_in (GtkWidget     *widget,
		    GdkEventFocus *event)
{
Elliot Lee's avatar
Elliot Lee committed
1064 1065 1066 1067
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_ENTRY (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

Owen Taylor's avatar
Owen Taylor committed
1068 1069 1070 1071 1072 1073
  GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
  gtk_widget_draw_focus (widget);
  gtk_entry_queue_draw (GTK_ENTRY (widget));
  
  GTK_ENTRY (widget)->need_im_reset = TRUE;
  gtk_im_context_focus_in (GTK_ENTRY (widget)->im_context);
Elliot Lee's avatar
Elliot Lee committed
1074

Owen Taylor's avatar
Owen Taylor committed
1075 1076
  return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
1077

Owen Taylor's avatar
Owen Taylor committed
1078 1079 1080 1081 1082 1083 1084
static gint
gtk_entry_focus_out (GtkWidget     *widget,
		     GdkEventFocus *event)
{
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_ENTRY (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
1085

Owen Taylor's avatar
Owen Taylor committed
1086 1087 1088
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
  gtk_widget_draw_focus (widget);
  gtk_entry_queue_draw (GTK_ENTRY (widget));
1089

Owen Taylor's avatar
Owen Taylor committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 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 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  GTK_ENTRY (widget)->need_im_reset = TRUE;
  gtk_im_context_focus_out (GTK_ENTRY (widget)->im_context);

  return FALSE;
}

static void 
gtk_entry_direction_changed (GtkWidget        *widget,
			     GtkTextDirection  previous_dir)
{
  GtkEntry *entry = GTK_ENTRY (widget);

  gtk_entry_recompute (entry);
      
  GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
}

static void
gtk_entry_state_changed (GtkWidget      *widget,
			 GtkStateType    previous_state)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_ENTRY (widget));

  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
      gdk_window_set_background (GTK_ENTRY (widget)->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
    }

  gtk_widget_queue_clear (widget);
}

/* GtkEditable method implementations
 */
static void
gtk_entry_insert_text (GtkEditable *editable,
		       const gchar *new_text,
		       gint         new_text_length,
		       gint        *position)
{
  GtkEntry *entry = GTK_ENTRY (editable);
  gchar buf[64];
  gchar *text;

  if (*position < 0 || *position > entry->text_length)
    *position = entry->text_length;
  
  g_object_ref (G_OBJECT (editable));
  
  if (new_text_length <= 63)
    text = buf;
  else
    text = g_new (gchar, new_text_length + 1);

  text[new_text_length] = '\0';
  strncpy (text, new_text, new_text_length);

  gtk_signal_emit (GTK_OBJECT (editable), signals[INSERT_TEXT], text, new_text_length, position);
  gtk_signal_emit (GTK_OBJECT (editable), signals[CHANGED]);

  if (new_text_length > 63)
    g_free (text);

  g_object_unref (G_OBJECT (editable));
}

static void
gtk_entry_delete_text (GtkEditable *editable,
		       gint         start_pos,
		       gint         end_pos)
{
  GtkEntry *entry = GTK_ENTRY (editable);

  if (end_pos < 0 || end_pos > entry->text_length)
    end_pos = entry->text_length;
  if (start_pos < 0)
    start_pos = 0;
  if (start_pos > end_pos)
    start_pos = end_pos;
  
  g_object_ref (G_OBJECT (editable));

  gtk_signal_emit (GTK_OBJECT (editable), signals[DELETE_TEXT], start_pos, end_pos);
  gtk_signal_emit (GTK_OBJECT (editable), signals[CHANGED]);

  g_object_unref (G_OBJECT (editable));
}

static gchar *    
gtk_entry_get_chars      (GtkEditable   *editable,
			  gint           start_pos,
			  gint           end_pos)
{
  GtkEntry *entry;
  gint start_index, end_index;
  
  g_return_val_if_fail (editable != NULL, NULL);
  g_return_val_if_fail (GTK_IS_ENTRY (editable), NULL);

  entry = GTK_ENTRY (editable);

  if (end_pos < 0)
    end_pos = entry->text_length;

  start_pos = MIN (entry->text_length, start_pos);
  end_pos = MIN (entry->text_length, end_pos);

  start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
  end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;

  return g_strndup (entry->text + start_index, end_index - start_index);
}

static void
gtk_entry_real_set_position (GtkEditable *editable,
			     gint         position)
{
  GtkEntry *entry = GTK_ENTRY (editable);
  
  if (position < 0 || position > entry->text_length)
    position = entry->text_length;

  if (position != entry->current_pos)
    {
      gtk_entry_reset_im_context (entry);

      entry->current_pos = entry->selection_bound = position;
      gtk_entry_recompute (entry);
    }
}

static gint
gtk_entry_get_position (GtkEditable *editable)
{
  return GTK_ENTRY (editable)->current_pos;
}

static void
gtk_entry_set_selection_bounds (GtkEditable *editable,
				gint         start,
				gint         end)
{
  GtkEntry *entry = GTK_ENTRY (editable);

  if (start < 0)
    start = entry->text_length;
  if (end < 0)
    end = entry->text_length;
  
  gtk_entry_reset_im_context (entry);

  entry->selection_bound = MIN (start, entry->text_length);
  entry->current_pos = MIN (end, entry->text_length);

  gtk_entry_update_primary_selection (entry);
  
  gtk_entry_recompute (entry);
}

static gboolean
gtk_entry_get_selection_bounds (GtkEditable *editable,
				gint        *start,
				gint        *end)
{
  GtkEntry *entry = GTK_ENTRY (editable);

  *start = entry->selection_bound;
  *end = entry->current_pos;

  return (entry->selection_bound != entry->current_pos);
}

static void 
gtk_entry_style_set	(GtkWidget      *widget,
			 GtkStyle       *previous_style)
{
  GtkEntry *entry = GTK_ENTRY (widget);

  if (previous_style && GTK_WIDGET_REALIZED (widget))
1270
    {
Owen Taylor's avatar
Owen Taylor committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
      gtk_entry_recompute (entry);

      gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
      gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
    }
}

/* Default signal handlers
 */
static void
gtk_entry_real_insert_text (GtkEntry    *entry,
			    const gchar *new_text,
			    gint         new_text_length,
			    gint        *position)
{
  gint index;
  gint n_chars;

  if (new_text_length < 0)
    new_text_length = strlen (new_text);

  n_chars = g_utf8_strlen (new_text, new_text_length);
  if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
    {
      gdk_beep ();
      n_chars = entry->text_max_length - entry->text_length;
    }

  if (new_text_length + entry->n_bytes + 1 > entry->text_size)
    {
      while (new_text_length + entry->n_bytes + 1 > entry->text_size)
1302
	{
Owen Taylor's avatar
Owen Taylor committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	  if (entry->text_size == 0)
	    entry->text_size = MIN_SIZE;
	  else
	    {
	      if (2 * (guint)entry->text_size < MAX_SIZE &&
		  2 * (guint)entry->text_size > entry->text_size)
		entry->text_size *= 2;
	      else
		{
		  entry->text_size = MAX_SIZE;
		  new_text_length = entry->text_size - new_text_length - 1;
		  break;
		}
	    }
1317
	}
Owen Taylor's avatar
Owen Taylor committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332