gtktextview.c 90.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/* gtktext.c - A "view" widget for the GtkTextBuffer object
 * 
 * Copyright (c) 1992-1994 The Regents of the University of California.
 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
 * Copyright (c) 1999 by Scriptics Corporation.
 * Copyright (c) 2000      Red Hat, Inc.
 * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
 *
 * This software is copyrighted by the Regents of the University of
 * California, Sun Microsystems, Inc., and other parties.  The
 * following terms apply to all files associated with the software
 * unless explicitly disclaimed in individual files.
 * 
 * The authors hereby grant permission to use, copy, modify,
 * distribute, and license this software and its documentation for any
 * purpose, provided that existing copyright notices are retained in
 * all copies and that this notice is included verbatim in any
 * distributions. No written agreement, license, or royalty fee is
 * required for any of the authorized uses.  Modifications to this
 * software may be copyrighted by their authors and need not follow
 * the licensing terms described here, provided that the new terms are
 * clearly indicated on the first page of each file where they apply.
 * 
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
 * U.S. government, the Government shall have only "Restricted Rights"
 * in the software and related documentation as defined in the Federal
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
 * are acquiring the software on behalf of the Department of Defense,
 * the software shall be classified as "Commercial Computer Software"
 * and the Government shall have only "Restricted Rights" as defined
 * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
 * foregoing, the authors grant the U.S. Government and others acting
 * in its behalf permission to use and distribute the software in
 * accordance with the terms specified in this license.
 * 
 */

51 52
#include <string.h>

53 54 55 56 57 58 59 60 61
#include "gtkbindings.h"
#include "gtkdnd.h"
#include "gtkmain.h"
#include "gtksignal.h"
#include "gtktext.h"
#include "gtktextdisplay.h"
#include "gtktextview.h"
#include "gtkimmulticontext.h"
#include "gdk/gdkkeysyms.h"
62 63
#include "gtktexttypes.h"
#include <string.h>
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

enum {
  MOVE_INSERT,
  SET_ANCHOR,
  SCROLL_TEXT,
  DELETE_TEXT,
  CUT_TEXT,
  COPY_TEXT,
  PASTE_TEXT,
  TOGGLE_OVERWRITE,
  SET_SCROLL_ADJUSTMENTS,
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_HEIGHT_LINES,
  ARG_WIDTH_COLUMNS,
  ARG_PIXELS_ABOVE_LINES,
  ARG_PIXELS_BELOW_LINES,
  ARG_PIXELS_INSIDE_WRAP,
  ARG_EDITABLE,
  ARG_WRAP_MODE,
  LAST_ARG
};

static void gtk_text_view_init                 (GtkTextView      *text_view);
static void gtk_text_view_class_init           (GtkTextViewClass *klass);
static void gtk_text_view_destroy              (GtkObject        *object);
static void gtk_text_view_finalize             (GObject          *object);
static void gtk_text_view_set_arg              (GtkObject        *object,
						GtkArg           *arg,
						guint             arg_id);
static void gtk_text_view_get_arg              (GtkObject        *object,
						GtkArg           *arg,
						guint             arg_id);
static void gtk_text_view_size_request         (GtkWidget        *widget,
						GtkRequisition   *requisition);
static void gtk_text_view_size_allocate        (GtkWidget        *widget,
						GtkAllocation    *allocation);
static void gtk_text_view_realize              (GtkWidget        *widget);
static void gtk_text_view_unrealize            (GtkWidget        *widget);
static void gtk_text_view_style_set            (GtkWidget        *widget,
						GtkStyle         *previous_style);
108 109
static void gtk_text_view_direction_changed    (GtkWidget        *widget,
						GtkTextDirection  previous_direction);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static gint gtk_text_view_event                (GtkWidget        *widget,
						GdkEvent         *event);
static gint gtk_text_view_key_press_event      (GtkWidget        *widget,
						GdkEventKey      *event);
static gint gtk_text_view_key_release_event    (GtkWidget        *widget,
						GdkEventKey      *event);
static gint gtk_text_view_button_press_event   (GtkWidget        *widget,
						GdkEventButton   *event);
static gint gtk_text_view_button_release_event (GtkWidget        *widget,
						GdkEventButton   *event);
static gint gtk_text_view_focus_in_event       (GtkWidget        *widget,
						GdkEventFocus    *event);
static gint gtk_text_view_focus_out_event      (GtkWidget        *widget,
						GdkEventFocus    *event);
static gint gtk_text_view_motion_event         (GtkWidget        *widget,
						GdkEventMotion   *event);
static void gtk_text_view_draw                 (GtkWidget        *widget,
						GdkRectangle     *area);
static gint gtk_text_view_expose_event         (GtkWidget        *widget,
						GdkEventExpose   *expose);


/* Source side drag signals */
static void gtk_text_view_drag_begin       (GtkWidget        *widget,
					    GdkDragContext   *context);
static void gtk_text_view_drag_end         (GtkWidget        *widget,
					    GdkDragContext   *context);
static void gtk_text_view_drag_data_get    (GtkWidget        *widget,
					    GdkDragContext   *context,
					    GtkSelectionData *selection_data,
					    guint             info,
					    guint             time);
static void gtk_text_view_drag_data_delete (GtkWidget        *widget,
					    GdkDragContext   *context);

/* Target side drag signals */
static void     gtk_text_view_drag_leave         (GtkWidget        *widget,
						  GdkDragContext   *context,
						  guint             time);
static gboolean gtk_text_view_drag_motion        (GtkWidget        *widget,
						  GdkDragContext   *context,
						  gint              x,
						  gint              y,
						  guint             time);
static gboolean gtk_text_view_drag_drop          (GtkWidget        *widget,
						  GdkDragContext   *context,
						  gint              x,
						  gint              y,
						  guint             time);
static void     gtk_text_view_drag_data_received (GtkWidget        *widget,
						  GdkDragContext   *context,
						  gint              x,
						  gint              y,
						  GtkSelectionData *selection_data,
						  guint             info,
						  guint             time);

static void gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
						  GtkAdjustment *hadj,
						  GtkAdjustment *vadj);

static void gtk_text_view_move_insert      (GtkTextView             *text_view,
					    GtkTextViewMovementStep  step,
					    gint                     count,
					    gboolean                 extend_selection);
static void gtk_text_view_set_anchor       (GtkTextView             *text_view);
static void gtk_text_view_scroll_text      (GtkTextView             *text_view,
					    GtkTextViewScrollType    type);
static void gtk_text_view_delete_text      (GtkTextView             *text_view,
					    GtkTextViewDeleteType    type,
					    gint                     count);
static void gtk_text_view_cut_text         (GtkTextView             *text_view);
static void gtk_text_view_copy_text        (GtkTextView             *text_view);
static void gtk_text_view_paste_text       (GtkTextView             *text_view);
static void gtk_text_view_toggle_overwrite (GtkTextView             *text_view);


static void     gtk_text_view_validate_onscreen     (GtkTextView        *text_view);
static void     gtk_text_view_get_first_para_iter   (GtkTextView        *text_view,
						     GtkTextIter        *iter);
static void     gtk_text_view_scroll_calc_now       (GtkTextView        *text_view);
static void     gtk_text_view_set_values_from_style (GtkTextView        *text_view,
192
						     GtkTextAttributes *values,
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
						     GtkStyle           *style);
static void     gtk_text_view_ensure_layout         (GtkTextView        *text_view);
static void     gtk_text_view_destroy_layout        (GtkTextView        *text_view);
static void     gtk_text_view_start_selection_drag  (GtkTextView        *text_view,
						     const GtkTextIter  *iter,
						     GdkEventButton     *event);
static gboolean gtk_text_view_end_selection_drag    (GtkTextView        *text_view,
						     GdkEventButton     *event);
static void     gtk_text_view_start_selection_dnd   (GtkTextView        *text_view,
						     const GtkTextIter  *iter,
						     GdkEventButton     *event);
static void     gtk_text_view_start_cursor_blink    (GtkTextView        *text_view);
static void     gtk_text_view_stop_cursor_blink     (GtkTextView        *text_view);

static void gtk_text_view_value_changed (GtkAdjustment *adj,
					 GtkTextView   *view);
static void gtk_text_view_commit_handler            (GtkIMContext  *context,
						     const gchar   *str,
						     GtkTextView   *text_view);

static void gtk_text_view_mark_set_handler       (GtkTextBuffer     *buffer,
						  const GtkTextIter *location,
						  const char        *mark_name,
						  gpointer           data);
static void gtk_text_view_get_virtual_cursor_pos (GtkTextView       *text_view,
						  gint              *x,
						  gint              *y);
static void gtk_text_view_set_virtual_cursor_pos (GtkTextView       *text_view,
						  gint               x,
						  gint               y);

224 225 226
static GtkAdjustment* get_hadjustment            (GtkTextView       *text_view);
static GtkAdjustment* get_vadjustment            (GtkTextView       *text_view);

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 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
enum {
  TARGET_STRING,
  TARGET_TEXT,
  TARGET_COMPOUND_TEXT,
  TARGET_UTF8_STRING
};

static GdkAtom clipboard_atom = GDK_NONE;
static GdkAtom text_atom = GDK_NONE;
static GdkAtom ctext_atom = GDK_NONE;
static GdkAtom utf8_atom = GDK_NONE;


static GtkTargetEntry target_table[] = {
  { "UTF8_STRING", 0, TARGET_UTF8_STRING },
  { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
  { "TEXT", 0, TARGET_TEXT },
  { "text/plain", 0, TARGET_STRING },
  { "STRING",     0, TARGET_STRING }
};

static guint n_targets = sizeof (target_table) / sizeof (target_table[0]);

static GtkContainerClass *parent_class = NULL;
static guint signals[LAST_SIGNAL] = { 0 };

GtkType
gtk_text_view_get_type (void)
{
  static GtkType our_type = 0;

  if (our_type == 0)
    {
      static const GtkTypeInfo our_info =
      {
        "GtkTextView",
        sizeof (GtkTextView),
        sizeof (GtkTextViewClass),
        (GtkClassInitFunc) gtk_text_view_class_init,
        (GtkObjectInitFunc) gtk_text_view_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL
      };

    our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
  }

  return our_type;
}

static void
add_move_insert_binding (GtkBindingSet *binding_set,
			 guint keyval,
			 guint modmask,
			 GtkTextViewMovementStep step,
			 gint count)
{
  g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
  
  gtk_binding_entry_add_signal (binding_set, keyval, modmask,
				"move_insert", 3,
				GTK_TYPE_ENUM, step,
				GTK_TYPE_INT, count,
                                GTK_TYPE_BOOL, FALSE);

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
				"move_insert", 3,
				GTK_TYPE_ENUM, step,
				GTK_TYPE_INT, count,
                                GTK_TYPE_BOOL, TRUE);
}

static void
gtk_text_view_class_init (GtkTextViewClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkBindingSet *binding_set;
  
  parent_class = gtk_type_class (GTK_TYPE_CONTAINER);

  /*
   * Arguments
   */
  gtk_object_add_arg_type ("GtkTextView::height_lines", GTK_TYPE_INT,
                           GTK_ARG_READWRITE, ARG_HEIGHT_LINES);
  gtk_object_add_arg_type ("GtkTextView::width_columns", GTK_TYPE_INT,
                           GTK_ARG_READWRITE, ARG_WIDTH_COLUMNS);
  gtk_object_add_arg_type ("GtkTextView::pixels_above_lines", GTK_TYPE_INT,
                           GTK_ARG_READWRITE, ARG_PIXELS_ABOVE_LINES);
  gtk_object_add_arg_type ("GtkTextView::pixels_below_lines", GTK_TYPE_INT,
                           GTK_ARG_READWRITE, ARG_PIXELS_BELOW_LINES);
  gtk_object_add_arg_type ("GtkTextView::pixels_inside_wrap", GTK_TYPE_INT,
                           GTK_ARG_READWRITE, ARG_PIXELS_INSIDE_WRAP);
  gtk_object_add_arg_type ("GtkTextView::editable", GTK_TYPE_BOOL,
                           GTK_ARG_READWRITE, ARG_EDITABLE);
  gtk_object_add_arg_type ("GtkTextView::wrap_mode", GTK_TYPE_ENUM,
                           GTK_ARG_READWRITE, ARG_WRAP_MODE);


  /*
   * Signals
   */

  signals[MOVE_INSERT] = 
      gtk_signal_new ("move_insert",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
                      GTK_SIGNAL_OFFSET (GtkTextViewClass, move_insert),
                      gtk_marshal_NONE__INT_INT_INT,
                      GTK_TYPE_NONE, 3, GTK_TYPE_ENUM, GTK_TYPE_INT, GTK_TYPE_BOOL);

  signals[SET_ANCHOR] = 
      gtk_signal_new ("set_anchor",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
                      GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
                      gtk_marshal_NONE__NONE,
                      GTK_TYPE_NONE, 0);

  signals[SCROLL_TEXT] = 
      gtk_signal_new ("scroll_text",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
                      GTK_SIGNAL_OFFSET (GtkTextViewClass, scroll_text),
                      gtk_marshal_NONE__INT,
                      GTK_TYPE_NONE, 1, GTK_TYPE_ENUM);

  signals[DELETE_TEXT] = 
      gtk_signal_new ("delete_text",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
                      GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_text),
                      gtk_marshal_NONE__INT_INT,
                      GTK_TYPE_NONE, 2, GTK_TYPE_ENUM, GTK_TYPE_INT);

  signals[CUT_TEXT] =
    gtk_signal_new ("cut_text",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_text),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);

  signals[COPY_TEXT] =
    gtk_signal_new ("copy_text",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_text),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);

  signals[PASTE_TEXT] =
    gtk_signal_new ("paste_text",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_text),
                    gtk_marshal_NONE__NONE,
                    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 (GtkTextViewClass, toggle_overwrite),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);

  signals[SET_SCROLL_ADJUSTMENTS] = widget_class->set_scroll_adjustments_signal =
    gtk_signal_new ("set_scroll_adjustments",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
		    gtk_marshal_NONE__POINTER_POINTER,
		    GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
  
  gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);

  /*
   * Key bindings
   */

  binding_set = gtk_binding_set_by_class (klass);

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

  add_move_insert_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
                          GTK_TEXT_MOVEMENT_CHAR, 1);
  
  add_move_insert_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
                          GTK_TEXT_MOVEMENT_CHAR, -1);
  
  add_move_insert_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
                          GTK_TEXT_MOVEMENT_WORD, 1);

  add_move_insert_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
                          GTK_TEXT_MOVEMENT_WORD, -1);
  
  /* Eventually we want to move by display lines, not paragraphs */
  add_move_insert_binding (binding_set, GDK_Up, 0,
435
                          GTK_TEXT_MOVEMENT_WRAPPED_LINE, -1);
436 437
  
  add_move_insert_binding (binding_set, GDK_Down, 0,
438
                          GTK_TEXT_MOVEMENT_WRAPPED_LINE, 1);
439 440

  add_move_insert_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
441
                          GTK_TEXT_MOVEMENT_WRAPPED_LINE, -1);
442 443
  
  add_move_insert_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
444
                          GTK_TEXT_MOVEMENT_WRAPPED_LINE, 1);
445 446
  
  add_move_insert_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
447
                          GTK_TEXT_MOVEMENT_LINE_ENDS, -1);
448 449

  add_move_insert_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
450
                          GTK_TEXT_MOVEMENT_LINE_ENDS, 1);
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

  add_move_insert_binding (binding_set, GDK_f, GDK_MOD1_MASK,
                          GTK_TEXT_MOVEMENT_WORD, 1);

  add_move_insert_binding (binding_set, GDK_b, GDK_MOD1_MASK,
                          GTK_TEXT_MOVEMENT_WORD, -1);

  add_move_insert_binding (binding_set, GDK_Home, 0,
                          GTK_TEXT_MOVEMENT_BUFFER_ENDS, -1);

  add_move_insert_binding (binding_set, GDK_End, 0,
                          GTK_TEXT_MOVEMENT_BUFFER_ENDS, 1);
  
  /* Setting the cut/paste/copy anchor */
  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK,
				"set_anchor", 0);

  
  /* Scrolling around */
  gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
				"scroll_text", 1,
				GTK_TYPE_ENUM, GTK_TEXT_SCROLL_PAGE_UP);

  gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
				"scroll_text", 1,
				GTK_TYPE_ENUM, GTK_TEXT_SCROLL_PAGE_DOWN);

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

  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
				"delete_text", 2,
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_CHAR,
				GTK_TYPE_INT, -1);

  gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
				"delete_text", 2,
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_HALF_WORD,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
				"delete_text", 2,
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_HALF_WORD,
				GTK_TYPE_INT, -1);

  gtk_binding_entry_add_signal (binding_set, GDK_k, GDK_CONTROL_MASK,
				"delete_text", 2,
501
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_HALF_LINE,
502 503 504 505
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK,
				"delete_text", 2,
506
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_WHOLE_LINE,
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
				"delete_text", 2,
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_WHITESPACE_LEAVE_ONE,
				GTK_TYPE_INT, 1);

  gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_MOD1_MASK,
				"delete_text", 2,
				GTK_TYPE_ENUM, GTK_TEXT_DELETE_WHITESPACE,
				GTK_TYPE_INT, 1);
  
  /* Cut/copy/paste */

  gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
				"cut_text", 0);

  gtk_binding_entry_add_signal (binding_set, GDK_w, GDK_CONTROL_MASK,
				"cut_text", 0);
  
  gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
				"copy_text", 0);
  
  gtk_binding_entry_add_signal (binding_set, GDK_y, GDK_CONTROL_MASK,
				"paste_text", 0);

  /* Overwrite */
  gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
				"toggle_overwrite", 0);
  
  /*
   * Default handlers and virtual methods
   */
  object_class->set_arg = gtk_text_view_set_arg;
  object_class->get_arg = gtk_text_view_get_arg;

  object_class->destroy = gtk_text_view_destroy;
  gobject_class->finalize = gtk_text_view_finalize;

  widget_class->realize = gtk_text_view_realize;
  widget_class->unrealize = gtk_text_view_unrealize;
  widget_class->style_set = gtk_text_view_style_set;
549
  widget_class->direction_changed = gtk_text_view_direction_changed;
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  widget_class->size_request = gtk_text_view_size_request;
  widget_class->size_allocate = gtk_text_view_size_allocate;
  widget_class->event = gtk_text_view_event;
  widget_class->key_press_event = gtk_text_view_key_press_event;
  widget_class->key_release_event = gtk_text_view_key_release_event;
  widget_class->button_press_event = gtk_text_view_button_press_event;
  widget_class->button_release_event = gtk_text_view_button_release_event;
  widget_class->focus_in_event = gtk_text_view_focus_in_event;
  widget_class->focus_out_event = gtk_text_view_focus_out_event;
  widget_class->motion_notify_event = gtk_text_view_motion_event;
  widget_class->expose_event = gtk_text_view_expose_event;
  widget_class->draw = gtk_text_view_draw;

  widget_class->drag_begin = gtk_text_view_drag_begin;
  widget_class->drag_end = gtk_text_view_drag_end;
  widget_class->drag_data_get = gtk_text_view_drag_data_get;
  widget_class->drag_data_delete = gtk_text_view_drag_data_delete;

  widget_class->drag_leave = gtk_text_view_drag_leave;
  widget_class->drag_motion = gtk_text_view_drag_motion;
  widget_class->drag_drop = gtk_text_view_drag_drop;
  widget_class->drag_data_received = gtk_text_view_drag_data_received;

  klass->move_insert = gtk_text_view_move_insert;
  klass->set_anchor = gtk_text_view_set_anchor;
  klass->scroll_text = gtk_text_view_scroll_text;
  klass->delete_text = gtk_text_view_delete_text;
  klass->cut_text = gtk_text_view_cut_text;
  klass->copy_text = gtk_text_view_copy_text;
  klass->paste_text = gtk_text_view_paste_text;
  klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
  klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
}

void
gtk_text_view_init (GtkTextView *text_view)
{
  GtkWidget *widget;
  
  widget = GTK_WIDGET (text_view);

  GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);

  text_view->wrap_mode = GTK_WRAPMODE_NONE;

  if (!clipboard_atom)
    clipboard_atom = gdk_atom_intern ("CLIPBOARD", FALSE);

  if (!text_atom)
    text_atom = gdk_atom_intern ("TEXT", FALSE);

  if (!ctext_atom)
    ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);

  if (!utf8_atom)
    utf8_atom = gdk_atom_intern ("UTF8_STRING", FALSE);
  
  gtk_drag_dest_set (widget,
                    GTK_DEST_DEFAULT_DROP | GTK_DEST_DEFAULT_MOTION,
                    target_table, n_targets,
                    GDK_ACTION_COPY | GDK_ACTION_MOVE);

  text_view->virtual_cursor_x = -1;
  text_view->virtual_cursor_y = -1;

  /* This object is completely private. No external entity can gain a reference
   * to it; so we create it here and destroy it in finalize().
   */
  text_view->im_context = gtk_im_multicontext_new ();
  
  gtk_signal_connect (GTK_OBJECT (text_view->im_context), "commit",
		      GTK_SIGNAL_FUNC (gtk_text_view_commit_handler), text_view);
622 623 624

  text_view->editable = TRUE;
  text_view->cursor_visible = TRUE;
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
}

GtkWidget*
gtk_text_view_new (void)
{
  return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
}

GtkWidget*
gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
{
  GtkTextView *text_view;

  text_view = (GtkTextView*)gtk_text_view_new ();

  gtk_text_view_set_buffer (text_view, buffer);

  return GTK_WIDGET (text_view);
}

void
gtk_text_view_set_buffer (GtkTextView *text_view,
			  GtkTextBuffer *buffer)
{
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
  g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
  
  if (text_view->buffer == buffer)
    return;

  if (text_view->buffer != NULL)
    {
      gtk_signal_disconnect_by_func (GTK_OBJECT (text_view->buffer),
				     gtk_text_view_mark_set_handler, text_view);
      gtk_object_unref (GTK_OBJECT (text_view->buffer));
      text_view->dnd_mark = NULL;
    }

  text_view->buffer = buffer;
  
  if (buffer != NULL)
    {
      GtkTextIter start;
      
      gtk_object_ref (GTK_OBJECT (buffer));
      gtk_object_sink (GTK_OBJECT (buffer));

      if (text_view->layout)
        gtk_text_layout_set_buffer (text_view->layout, buffer);

675
      gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
676 677 678 679 680 681
      
      text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
							 "__drag_target",
							 &start, FALSE);

      text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
682
                                                                NULL,
683
								&start, TRUE);
684
      
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
      text_view->first_para_pixels = 0;
      
      gtk_signal_connect (GTK_OBJECT (text_view->buffer), "mark_set",
			  gtk_text_view_mark_set_handler, text_view);
    }

  if (GTK_WIDGET_VISIBLE (text_view))    
    gtk_widget_queue_draw (GTK_WIDGET (text_view));
}

GtkTextBuffer*
gtk_text_view_get_buffer (GtkTextView *text_view)
{
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
  
  return text_view->buffer;
}

void
gtk_text_view_get_iter_at_pixel (GtkTextView *text_view,
				 GtkTextIter *iter,
				 gint x, gint y)
{
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
  g_return_if_fail (iter != NULL);
  g_return_if_fail (text_view->layout != NULL);

  gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                     iter,
                                     x + text_view->xoffset,
                                     y + text_view->yoffset);
}

718 719 720 721 722 723 724 725 726 727
void
gtk_text_view_get_iter_location (GtkTextView       *text_view,
                                 const GtkTextIter *iter,
                                 GdkRectangle      *location)
{
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
  g_return_if_fail (gtk_text_iter_get_buffer (iter) == text_view->buffer);

  gtk_text_layout_get_iter_location (text_view->layout, iter, location);
}
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

static void
set_adjustment_clamped (GtkAdjustment *adj, gfloat val)
{
  /* We don't really want to clamp to upper; we want to clamp to
     upper - page_size which is the highest value the scrollbar
     will let us reach. */
  if (val > (adj->upper - adj->page_size))
    val = adj->upper - adj->page_size;

  if (val < adj->lower)
    val = adj->lower;
  
  gtk_adjustment_set_value (adj, val);
}

gboolean
gtk_text_view_scroll_to_mark_adjusted (GtkTextView *text_view,
746
                                       GtkTextMark *mark,
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
				       gint margin,
				       gfloat percentage)
{
  GtkTextIter iter;
  GdkRectangle rect;
  GdkRectangle screen;
  gint screen_bottom;
  gint screen_right;
  GtkWidget *widget;
  gboolean retval = FALSE;
  gint scroll_inc;

  gint current_x_scroll, current_y_scroll;
  
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
762
  g_return_val_if_fail (mark != NULL, FALSE);
763 764 765 766 767 768 769 770

  widget = GTK_WIDGET (text_view);
  
  if (!GTK_WIDGET_MAPPED (widget))
    {
      g_warning ("FIXME need to implement scroll_to_mark for unmapped GtkTextView?");
      return FALSE;
    }
771 772

  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter, mark);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  
  gtk_text_layout_get_iter_location (text_view->layout,
                                     &iter,
                                     &rect);
  
  /* Be sure the scroll region is up-to-date */
  gtk_text_view_scroll_calc_now (text_view);
  
  current_x_scroll = text_view->xoffset;
  current_y_scroll = text_view->yoffset;

  screen.x = current_x_scroll;
  screen.y = current_y_scroll;
  screen.width = widget->allocation.width;
  screen.height = widget->allocation.height;

  {
    /* Clamp margin so it's not too large. */
    gint small_dimension = MIN (screen.width, screen.height);
    gint max_rect_dim;
    
    if (margin > (small_dimension/2 - 5)) /* 5 is arbitrary */
      margin = (small_dimension/2 - 5);

    if (margin < 0)
      margin = 0;
    
    /* make sure rectangle fits in the leftover space */

    max_rect_dim = MAX (rect.width, rect.height);
    
    if (max_rect_dim > (small_dimension - margin*2))
      margin -= max_rect_dim - (small_dimension - margin*2);
                 
    if (margin < 0)
      margin = 0;
  }

  g_assert (margin >= 0);
  
  screen.x += margin;
  screen.y += margin;

  screen.width -= margin*2;
  screen.height -= margin*2;

  screen_bottom = screen.y + screen.height;
  screen_right = screen.x + screen.width;
  
  /* Vertical scroll (only vertical gets adjusted) */

  scroll_inc = 0;
  if (rect.y < screen.y)
    {
      gint scroll_dest = rect.y;
      scroll_inc = (scroll_dest - screen.y) * percentage;
    }
  else if ((rect.y + rect.height) > screen_bottom)
    {
      gint scroll_dest = rect.y + rect.height;
      scroll_inc = (scroll_dest - screen_bottom) * percentage;
    }

  if (scroll_inc != 0)
    {
838
      set_adjustment_clamped (get_vadjustment (text_view),
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
			      current_y_scroll + scroll_inc);
      retval = TRUE;
    }
  
  /* Horizontal scroll */

  scroll_inc = 0;
  if (rect.x < screen.x)
    {
      gint scroll_dest = rect.x;
      scroll_inc = scroll_dest - screen.x;
    }
  else if ((rect.x + rect.width) > screen_right)
    {
      gint scroll_dest = rect.x + rect.width;
      scroll_inc = scroll_dest - screen_right;
    }

  if (scroll_inc != 0)
    {
859
      set_adjustment_clamped (get_hadjustment (text_view),
860 861 862 863 864 865 866 867 868
			      current_x_scroll + scroll_inc);
      retval = TRUE;
    }

  return retval;
}

gboolean
gtk_text_view_scroll_to_mark (GtkTextView *text_view,
869 870
                              GtkTextMark *mark,
                              gint mark_within_margin)
871 872 873
{
  g_return_val_if_fail (mark_within_margin >= 0, FALSE);
  
874
  return gtk_text_view_scroll_to_mark_adjusted (text_view, mark,
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
						mark_within_margin, 1.0);
}

static gboolean
clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
{
  GdkRectangle visible_rect;
  gtk_text_view_get_visible_rect (text_view, &visible_rect);

  return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
					       visible_rect.y,
					       visible_rect.y + visible_rect.height);
}

gboolean
gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
891
                                  GtkTextMark *mark)
892
{
893
  GtkTextIter iter;
894 895
  
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
896
  g_return_val_if_fail (mark != NULL, FALSE);
897
  
898
  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter, mark);
899
  
900
  if (clamp_iter_onscreen (text_view, &iter))
901
    {
902
      gtk_text_buffer_move_mark (text_view->buffer, mark, &iter);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
      return TRUE;
    }
  else
    return FALSE;
}

void
gtk_text_view_get_visible_rect (GtkTextView  *text_view,
				GdkRectangle *visible_rect)
{
  GtkWidget *widget;

  g_return_if_fail (text_view != NULL);
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));

  widget = GTK_WIDGET (text_view);
  
  if (visible_rect)
    {
      visible_rect->x = text_view->xoffset;
      visible_rect->y = text_view->yoffset;
      visible_rect->width = widget->allocation.width;
      visible_rect->height = widget->allocation.height;
    }
}

void
gtk_text_view_set_wrap_mode (GtkTextView *text_view,
			     GtkWrapMode  wrap_mode)
{
  g_return_if_fail (text_view != NULL);
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));

  if (text_view->wrap_mode != wrap_mode)
    {
      text_view->wrap_mode = wrap_mode;

      if (text_view->layout)
	{
	  text_view->layout->default_style->wrap_mode = wrap_mode;
	  gtk_text_layout_default_style_changed (text_view->layout);
	}
    }
}

GtkWrapMode
gtk_text_view_get_wrap_mode (GtkTextView *text_view)
{
  g_return_val_if_fail (text_view != NULL, GTK_WRAPMODE_NONE);
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAPMODE_NONE);

  return text_view->wrap_mode;
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
void
gtk_text_view_set_editable (GtkTextView *text_view,
                            gboolean     setting)
{
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));

  if (text_view->editable != setting)
    {
      text_view->editable = setting;

      if (text_view->layout)
	{
	  text_view->layout->default_style->editable = text_view->editable;
	  gtk_text_layout_default_style_changed (text_view->layout);
	}
    }
}

gboolean
gtk_text_view_get_editable (GtkTextView *text_view)
{
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
  
  return text_view->editable;
}

void
gtk_text_view_set_cursor_visible    (GtkTextView   *text_view,
                                     gboolean       setting)
{
  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));

989 990
  setting = (setting != FALSE);

991 992 993 994 995
  if (text_view->cursor_visible != setting)
    {
      text_view->cursor_visible = setting;

      if (GTK_WIDGET_HAS_FOCUS (text_view))
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	{
	  if (text_view->layout)
	    {
	      gtk_text_layout_set_cursor_visible (text_view->layout, setting);
	  
	      if (setting)
		gtk_text_view_start_cursor_blink (text_view);
	      else
		gtk_text_view_stop_cursor_blink (text_view);
	    }
	}
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    }
}

gboolean
gtk_text_view_get_cursor_visible    (GtkTextView   *text_view)
{
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);

  return text_view->cursor_visible;
}


1019 1020 1021 1022 1023 1024 1025
gboolean
gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
{
  GtkTextIter insert;
  
  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
  
1026 1027 1028
  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &insert,
                                    gtk_text_buffer_get_mark (text_view->buffer,
                                                              "insert"));
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  
  if (clamp_iter_onscreen (text_view, &insert))
    {
      gtk_text_buffer_place_cursor (text_view->buffer, &insert);
      return TRUE;
    }
  else
    return FALSE;
}

static void
gtk_text_view_destroy (GtkObject *object)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (object);

  gtk_text_view_destroy_layout (text_view);
  gtk_text_view_set_buffer (text_view, NULL);
1048

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}

static void
gtk_text_view_finalize (GObject *object)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (object);  

1059 1060 1061 1062 1063
  if (text_view->hadjustment)
    gtk_object_unref (GTK_OBJECT (text_view->hadjustment));
  if (text_view->vadjustment)
    gtk_object_unref (GTK_OBJECT (text_view->vadjustment));

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 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
  gtk_object_unref (GTK_OBJECT (text_view->im_context));

  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

static void
gtk_text_view_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (object);

  switch (arg_id)
    {
    case ARG_HEIGHT_LINES:
      break;

    case ARG_WIDTH_COLUMNS:
      break;

    case ARG_PIXELS_ABOVE_LINES:
      break;

    case ARG_PIXELS_BELOW_LINES:
      break;

    case ARG_PIXELS_INSIDE_WRAP:
      break;

    case ARG_EDITABLE:
      break;

    case ARG_WRAP_MODE:
      break;

    default:
      g_assert_not_reached ();
      break;
    }
}

static void
gtk_text_view_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (object);

  switch (arg_id)
    {
    case ARG_HEIGHT_LINES:
      break;

    case ARG_WIDTH_COLUMNS:
      break;

    case ARG_PIXELS_ABOVE_LINES:
      break;

    case ARG_PIXELS_BELOW_LINES:
      break;

    case ARG_PIXELS_INSIDE_WRAP:
      break;

    case ARG_EDITABLE:
      break;

    case ARG_WRAP_MODE:
      break;

    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

static void
gtk_text_view_size_request (GtkWidget      *widget,
			    GtkRequisition *requisition)
{
  /* Hrm */
1146 1147 1148

  requisition->width = 200;
  requisition->height = 200;
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
}

static void
gtk_text_view_size_allocate (GtkWidget *widget,
                             GtkAllocation *allocation)
{
  GtkTextView *text_view;
  GtkTextIter first_para;
  gint y;
  GtkAdjustment *vadj;
  gboolean yoffset_changed = FALSE;

  text_view = GTK_TEXT_VIEW (widget);
  
  widget->allocation = *allocation;
  
  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);

      gdk_window_resize (text_view->bin_window,
			 allocation->width, allocation->height);
    }

  gtk_text_view_ensure_layout (text_view);
  gtk_text_layout_set_screen_width (text_view->layout,
                                    GTK_WIDGET (text_view)->allocation.width);
      
  gtk_text_view_validate_onscreen (text_view);
  gtk_text_view_scroll_calc_now (text_view);

  /* Now adjust the value of the adjustment to keep the cursor at the same place in
   * the buffer 
  */
  gtk_text_view_get_first_para_iter (text_view, &first_para);
1186 1187 1188
  gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);

  y += text_view->first_para_pixels;
1189

1190 1191 1192 1193
  /* Ensure h/v adj exist */
  get_hadjustment (text_view);
  get_vadjustment (text_view);
  
1194 1195 1196 1197 1198 1199 1200 1201 1202
  vadj = text_view->vadjustment;
  if (y > vadj->upper - vadj->page_size)
    y = MAX (0, vadj->upper - vadj->page_size);

  if (y != text_view->yoffset)
    {
      vadj->value = text_view->yoffset = y;
      yoffset_changed = TRUE;
    }
1203
  
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  text_view->hadjustment->page_size = allocation->width;
  text_view->hadjustment->page_increment = allocation->width / 2;
  text_view->hadjustment->lower = 0;
  text_view->hadjustment->upper = MAX (allocation->width, text_view->width);
  gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");

  text_view->vadjustment->page_size = allocation->height;
  text_view->vadjustment->page_increment = allocation->height / 2;
  text_view->vadjustment->lower = 0;
  text_view->vadjustment->upper = MAX (allocation->height, text_view->height);
  gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");

  if (yoffset_changed)
    gtk_adjustment_value_changed (vadj);
}

static void
gtk_text_view_get_first_para_iter (GtkTextView *text_view,
				   GtkTextIter *iter)
{
1224 1225
  gtk_text_buffer_get_iter_at_mark (text_view->buffer, iter,
                                    text_view->first_para_mark);
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 1270 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
}

static void
gtk_text_view_validate_onscreen (GtkTextView *text_view)
{
  GtkWidget *widget = GTK_WIDGET (text_view);
  
  if (widget->allocation.height > 0)
    {
      GtkTextIter first_para;
      gtk_text_view_get_first_para_iter (text_view, &first_para);
      gtk_text_layout_validate_yrange (text_view->layout,
				       &first_para,
				       0, text_view->first_para_pixels + widget->allocation.height);
    }
}

static gboolean
first_validate_callback (gpointer data)
{
  GtkTextView *text_view = data;

  gtk_text_view_validate_onscreen (text_view);
  
  text_view->first_validate_idle = 0;
  return FALSE;
}

static gboolean
incremental_validate_callback (gpointer data)
{
  GtkTextView *text_view = data;

  gtk_text_layout_validate (text_view->layout, 2000);
  if (gtk_text_layout_is_valid (text_view->layout))
    {
      text_view->incremental_validate_idle = 0;
      return FALSE;
    }
  else
    return TRUE;
}

static void
invalidated_handler (GtkTextLayout *layout,
		     gpointer       data)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (data);

  if (!text_view->first_validate_idle)
    text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 1, first_validate_callback, text_view, NULL);

  if (!text_view->incremental_validate_idle)
    text_view->incremental_validate_idle = g_idle_add_full (GDK_PRIORITY_REDRAW + 1, incremental_validate_callback, text_view, NULL);
}

static void
changed_handler (GtkTextLayout *layout,
		 gint           start_y,
		 gint           old_height,
		 gint           new_height,
		 gpointer       data)
{
  GtkTextView *text_view;
  GtkWidget *widget;
  GdkRectangle visible_rect;
  GdkRectangle redraw_rect;
  
  text_view = GTK_TEXT_VIEW (data);
  widget = GTK_WIDGET (data);

  if (GTK_WIDGET_REALIZED (text_view))
    {
      gtk_text_view_get_visible_rect (text_view, &visible_rect);
1302

1303 1304 1305
      redraw_rect.x = visible_rect.x;
      redraw_rect.width = visible_rect.width;
      redraw_rect.y = start_y;
1306 1307 1308 1309 1310

      if (old_height == new_height)
	redraw_rect.height = old_height;
      else
	redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
      
      if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
	{
	  redraw_rect.y -= text_view->yoffset;
	  gdk_window_invalidate_rect (text_view->bin_window, &redraw_rect, FALSE);
	}
    }

  if (old_height != new_height)
    {
      gboolean yoffset_changed = FALSE;

      if (start_y + old_height <= text_view->yoffset - text_view->first_para_pixels)
	{
	  text_view->yoffset += new_height - old_height;
1326
	  get_vadjustment (text_view)->value = text_view->yoffset;
1327 1328 1329 1330 1331 1332
	  yoffset_changed = TRUE;
	}

      gtk_text_view_scroll_calc_now (text_view);

      if (yoffset_changed)
1333
	gtk_adjustment_value_changed (get_vadjustment (text_view));
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    }
}

static void
gtk_text_view_realize (GtkWidget *widget)
{
  GtkTextView *text_view;
  GdkCursor *cursor;
  GdkWindowAttr attributes;
  gint attributes_mask;
    
  text_view = GTK_TEXT_VIEW (widget);
  GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
  
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

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

  attributes.x = 0;
  attributes.y = 0;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.event_mask = (GDK_EXPOSURE_MASK            |
			   GDK_SCROLL_MASK              |
			   GDK_KEY_PRESS_MASK           |
			   GDK_BUTTON_PRESS_MASK        |
			   GDK_BUTTON_RELEASE_MASK      |
			   GDK_POINTER_MOTION_MASK      |
			   GDK_POINTER_MOTION_HINT_MASK |
			   gtk_widget_get_events (widget));

  text_view->bin_window = gdk_window_new (widget->window,
					&attributes, attributes_mask);
  gdk_window_show (text_view->bin_window);
  gdk_window_set_user_data (text_view->bin_window, widget);

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

  gdk_window_set_background (text_view->bin_window, &widget->style->base[GTK_WIDGET_STATE (widget)]);

  gtk_text_view_ensure_layout (text_view);

  /* I-beam cursor */
  cursor = gdk_cursor_new (GDK_XTERM);
  gdk_window_set_cursor (text_view->bin_window, cursor);
  gdk_cursor_destroy (cursor);

  gtk_im_context_set_client_window (text_view->im_context, widget->window);
}

static void
gtk_text_view_unrealize (GtkWidget *widget)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (widget);

  if (text_view->first_validate_idle)
    {
      g_source_remove (text_view->first_validate_idle);
      text_view->first_validate_idle = 0;
    }
    
  if (text_view->incremental_validate_idle)
    {
      g_source_remove (text_view->incremental_validate_idle);
      text_view->incremental_validate_idle = 0;
    }
    
  gtk_text_view_destroy_layout (text_view);
  
  gtk_im_context_set_client_window (text_view->im_context, NULL);

  gdk_window_set_user_data (text_view->bin_window, NULL);
  gdk_window_destroy (text_view->bin_window);
  text_view->bin_window = NULL;

  (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

static void 
gtk_text_view_style_set (GtkWidget *widget,
			 GtkStyle  *previous_style)
{
  GtkTextView *text_view = GTK_TEXT_VIEW (widget);

  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_set_background (text_view->bin_window,
				 &widget->style->base[GTK_WIDGET_STATE (widget)]);

      gtk_text_view_set_values_from_style (text_view, text_view->layout->default_style, widget->style);
      gtk_text_layout_default_style_changed (text_view->layout);
    }
}

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
static void 
gtk_text_view_direction_changed (GtkWidget        *widget,
				 GtkTextDirection  previous_direction)
{
  GtkTextView *text_view = GTK_TEXT_VIEW (widget);

  if (text_view->layout)
    {
      text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
      gtk_text_layout_default_style_changed (text_view->layout);
    }
}

1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
/*
 * Events
 */

static gboolean
get_event_coordinates (GdkEvent *event, gint *x, gint *y)
{
  if (event)
    switch (event->type)
      {
      case GDK_MOTION_NOTIFY:
        *x = event->motion.x;
        *y = event->motion.y;
        return TRUE;
        break;
        
      case GDK_BUTTON_PRESS:
      case GDK_2BUTTON_PRESS:
      case GDK_3BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
        *x = event->button.x;
        *y = event->button.y;
        return TRUE;
        break;
        
      case GDK_KEY_PRESS:
      case GDK_KEY_RELEASE:
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
      case GDK_PROPERTY_NOTIFY:
      case GDK_SELECTION_CLEAR:
      case GDK_SELECTION_REQUEST:
      case GDK_SELECTION_NOTIFY:
      case GDK_PROXIMITY_IN:
      case GDK_PROXIMITY_OUT:
      case GDK_DRAG_ENTER:
      case GDK_DRAG_LEAVE:
      case GDK_DRAG_MOTION:
      case GDK_DRAG_STATUS:
      case GDK_DROP_START:
      case GDK_DROP_FINISHED:
      default:
        return FALSE;
	break;
      }

  return FALSE;
}

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static gint
emit_event_on_tags (GtkWidget   *widget,
                    GdkEvent    *event,
                    GtkTextIter *iter)
{
  GSList *tags;
  GSList *tmp;
  gint retval = FALSE;
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (widget);
  
  tags = gtk_text_buffer_get_tags (text_view->buffer, iter);
          
  tmp = tags;
  while (tmp != NULL)
    {
      GtkTextTag *tag = tmp->data;

      if (gtk_text_tag_event (tag, GTK_OBJECT (widget), event, iter))
        {
          retval = TRUE;
          break;
        }

      tmp = g_slist_next (tmp);
    }

  g_slist_free (tags);

  return retval;
}
     
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
static gint
gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
{
  GtkTextView *text_view;
  gint x = 0, y = 0;
  
  text_view = GTK_TEXT_VIEW (widget);
  
  if (text_view->layout == NULL ||
      text_view->buffer == NULL)
    return FALSE;
  
  if (get_event_coordinates (event, &x, &y))
    {
      GtkTextIter iter;

      x += text_view->xoffset;
      y += text_view->yoffset;

      /* FIXME this is slow and we do it twice per event.
         My favorite solution is to have GtkTextLayout cache
         the last couple lookups. */
      gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                         &iter,
                                         x, y);

1563 1564 1565 1566 1567 1568 1569
      return emit_event_on_tags (widget, event, &iter);
    }
  else if (event->type == GDK_KEY_PRESS ||
           event->type == GDK_KEY_RELEASE)
    {
      GtkTextMark *insert;
      GtkTextIter iter;
1570

1571 1572
      insert = gtk_text_buffer_get_mark (text_view->buffer,
                                         "insert");      
1573

1574
      gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter, insert);
1575

1576
      return emit_event_on_tags (widget, event, &iter);
1577
    }
1578 1579
  else
    return FALSE;
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
}

static gint
gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (widget);

  if (text_view->layout == NULL ||
      text_view->buffer == NULL)
    return FALSE;

  if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
      GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
    return TRUE;

  if (gtk_im_context_filter_keypress (text_view->im_context, event))
    return TRUE;
  else if (event->keyval == GDK_Return)
    {
1601 1602
      gtk_text_buffer_insert_interactive_at_cursor (text_view->buffer, "\n", 1,
                                                    text_view->editable);
1603 1604 1605 1606
      gtk_text_view_scroll_to_mark (text_view,
                                    gtk_text_buffer_get_mark (text_view->buffer,
                                                              "insert"),
                                    0);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
      return TRUE;
    }
  else
    return FALSE;
}

static gint
gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
{
  return FALSE;
}

static gint
gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
{
  GtkTextView *text_view;

  text_view = GTK_TEXT_VIEW (widget);
  
  gtk_widget_grab_focus (widget);

  /* debug hack */
  if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
    gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
  else if (event->button == 3)
    gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);

  if (event->type == GDK_BUTTON_PRESS)
    {
      if (event->button == 1)
        {
          /* If we're in the selection, start a drag copy/move of the
             selection; otherwise, start creating a new selection. */
          GtkTextIter iter;
          GtkTextIter start, end;

          gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                             &iter,
                                             event->x + text_view->xoffset,
                                             event->y + text_view->yoffset);
          
          if (gtk_text_buffer_get_selection_bounds (text_view->buffer,
                                                    &start, &end) &&
              gtk_text_iter_in_region (&iter, &start, &end))
            {
              gtk_text_view_start_selection_dnd (text_view, &iter, event);
            }
          else
            {
              gtk_text_view_start_selection_drag (text_view, &iter, event);
            }
          
          return TRUE;
        }
      else if (event->button == 2)
        {
          GtkTextIter iter;

          gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                             &iter,
                                             event->x + text_view->xoffset,
                                             event->y + text_view->yoffset);
          
          gtk_text_buffer_paste_primary_selection (text_view->buffer,
                                                   &iter,
1672 1673 1674
                                                   event->time,
                                                   TRUE,
                                                   text_view->editable);
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
          return TRUE;
        }
      else if (event->button == 3)
        {
          if (gtk_text_view_end_selection_drag (text_view, event))
            return TRUE;
          else
            return FALSE;
        }
    }
  
  return FALSE;
}

static gint
gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
{
  if (event->button == 1)
    {
      gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event);
      return TRUE;
    }

  return FALSE;
}

static gint
gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
{
1704
  GtkTextView *text_view = GTK_TEXT_VIEW (widget);
1705 1706 1707
  
  GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);

1708 1709 1710 1711 1712
  if (text_view->cursor_visible && text_view->layout)
    {
      gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
      gtk_text_view_start_cursor_blink (text_view);
    }
1713 1714 1715 1716 1717 1718 1719 1720 1721

  gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
  
  return FALSE;
}

static gint
gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
{
1722
  GtkTextView *text_view = GTK_TEXT_VIEW (widget);
1723 1724 1725
  
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);

1726 1727 1728 1729 1730
  if (text_view->cursor_visible && text_view->layout)
    {
      gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
      gtk_text_view_stop_cursor_blink (text_view);
    }
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

  gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
  
  return FALSE;
}

static gint
gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
{
  
  return FALSE;
}

static void
gtk_text_view_paint (GtkWidget *widget, GdkRectangle *area)
{
  GtkTextView *text_view;
  
  text_view = GTK_TEXT_VIEW (widget);

  g_return_if_fail (text_view->layout != NULL