]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextview.c
Deprecate widget flag: GTK_WIDGET_VISIBLE
[~andy/gtk] / gtk / gtktextview.c
index 0354f14de28884239f2e3ba207d5b9e8ef92d9ff..589e8af8592697bfefe988b63afeeae626211f97 100644 (file)
@@ -1,3 +1,4 @@
+/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
 /* GTK - The GIMP Toolkit
  * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
  *
@@ -24,7 +25,7 @@
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#include <config.h>
+#include "config.h"
 #include <string.h>
 
 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
@@ -45,7 +46,6 @@
 #include "gtkimmulticontext.h"
 #include "gdk/gdkkeysyms.h"
 #include "gtkprivate.h"
-#include "gtksizegroup.h"          /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
 #include "gtktextutil.h"
 #include "gtkwindow.h"
 #include "gtkalias.h"
 
 #define SPACE_FOR_CURSOR 1
 
+typedef struct _GtkTextViewPrivate GtkTextViewPrivate;
+
+#define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
+
+struct _GtkTextViewPrivate 
+{
+  guint blink_time;  /* time in msec the cursor has blinked since last user event */
+  guint im_spot_idle;
+  gchar *im_module;
+};
+
+
 struct _GtkTextPendingScroll
 {
   GtkTextMark   *mark;
@@ -123,9 +135,10 @@ enum
   COPY_CLIPBOARD,
   PASTE_CLIPBOARD,
   TOGGLE_OVERWRITE,
-  MOVE_FOCUS,
   MOVE_VIEWPORT,
   SELECT_ALL,
+  TOGGLE_CURSOR_VISIBLE,
+  PREEDIT_CHANGED,
   LAST_SIGNAL
 };
 
@@ -146,7 +159,7 @@ enum
   PROP_BUFFER,
   PROP_OVERWRITE,
   PROP_ACCEPTS_TAB,
-  LAST_PROP
+  PROP_IM_MODULE
 };
 
 static void gtk_text_view_destroy              (GtkObject        *object);
@@ -195,6 +208,8 @@ static gint gtk_text_view_expose_event         (GtkWidget        *widget,
 static void gtk_text_view_draw_focus           (GtkWidget        *widget);
 static gboolean gtk_text_view_focus            (GtkWidget        *widget,
                                                 GtkDirectionType  direction);
+static void gtk_text_view_move_focus           (GtkWidget        *widget,
+                                                GtkDirectionType  direction_type);
 static void gtk_text_view_select_all           (GtkWidget        *widget,
                                                 gboolean          select);
 
@@ -246,14 +261,14 @@ static void gtk_text_view_move_cursor       (GtkTextView           *text_view,
 static void gtk_text_view_page_horizontally (GtkTextView          *text_view,
                                              gint                  count,
                                              gboolean              extend_selection);
-static void gtk_text_view_move_viewport     (GtkTextView           *text_view,
+static gboolean gtk_text_view_move_viewport (GtkTextView           *text_view,
                                              GtkScrollStep          step,
                                              gint                   count);
 static void gtk_text_view_set_anchor       (GtkTextView           *text_view);
-static void gtk_text_view_scroll_pages     (GtkTextView           *text_view,
+static gboolean gtk_text_view_scroll_pages (GtkTextView           *text_view,
                                             gint                   count,
                                             gboolean               extend_selection);
-static void gtk_text_view_scroll_hpages    (GtkTextView           *text_view,
+static gboolean gtk_text_view_scroll_hpages(GtkTextView           *text_view,
                                             gint                   count,
                                             gboolean               extend_selection);
 static void gtk_text_view_insert_at_cursor (GtkTextView           *text_view,
@@ -266,7 +281,8 @@ static void gtk_text_view_cut_clipboard    (GtkTextView           *text_view);
 static void gtk_text_view_copy_clipboard   (GtkTextView           *text_view);
 static void gtk_text_view_paste_clipboard  (GtkTextView           *text_view);
 static void gtk_text_view_toggle_overwrite (GtkTextView           *text_view);
-static void gtk_text_view_move_focus       (GtkTextView           *text_view,
+static void gtk_text_view_toggle_cursor_visible (GtkTextView      *text_view);
+static void gtk_text_view_compat_move_focus(GtkTextView           *text_view,
                                             GtkDirectionType       direction_type);
 static void gtk_text_view_unselect         (GtkTextView           *text_view);
 
@@ -284,14 +300,14 @@ static void     gtk_text_view_reset_im_context       (GtkTextView        *text_v
 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 gboolean gtk_text_view_end_selection_drag     (GtkTextView        *text_view);
 static void     gtk_text_view_start_selection_dnd    (GtkTextView        *text_view,
                                                       const GtkTextIter  *iter,
                                                       GdkEventMotion     *event);
 static void     gtk_text_view_check_cursor_blink     (GtkTextView        *text_view);
 static void     gtk_text_view_pend_cursor_blink      (GtkTextView        *text_view);
 static void     gtk_text_view_stop_cursor_blink      (GtkTextView        *text_view);
+static void     gtk_text_view_reset_blink_time       (GtkTextView        *text_view);
 
 static void     gtk_text_view_value_changed                (GtkAdjustment *adj,
                                                            GtkTextView   *view);
@@ -316,9 +332,13 @@ static void gtk_text_view_mark_set_handler       (GtkTextBuffer     *buffer,
 static void gtk_text_view_target_list_notify     (GtkTextBuffer     *buffer,
                                                   const GParamSpec  *pspec,
                                                   gpointer           data);
+static void gtk_text_view_paste_done_handler     (GtkTextBuffer     *buffer,
+                                                  GtkClipboard      *clipboard,
+                                                  gpointer           data);
 static void gtk_text_view_get_cursor_location    (GtkTextView       *text_view,
                                                  GdkRectangle      *pos);
 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView       *text_view,
+                                                  GtkTextIter       *cursor,
                                                   gint              *x,
                                                   gint              *y);
 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView       *text_view,
@@ -331,6 +351,7 @@ static GtkAdjustment* get_vadjustment            (GtkTextView       *text_view);
 static void gtk_text_view_do_popup               (GtkTextView       *text_view,
                                                  GdkEventButton    *event);
 
+static void cancel_pending_scroll                (GtkTextView   *text_view);
 static void gtk_text_view_queue_scroll           (GtkTextView   *text_view,
                                                   GtkTextMark   *mark,
                                                   gdouble        within_margin,
@@ -401,7 +422,7 @@ static GtkTextWindow *text_window_new             (GtkTextWindowType  type,
                                                    gint               height_request);
 static void           text_window_free            (GtkTextWindow     *win);
 static void           text_window_realize         (GtkTextWindow     *win,
-                                                   GdkWindow         *parent);
+                                                   GtkWidget         *widget);
 static void           text_window_unrealize       (GtkTextWindow     *win);
 static void           text_window_size_allocate   (GtkTextWindow     *win,
                                                    GdkRectangle      *rect);
@@ -430,14 +451,14 @@ add_move_binding (GtkBindingSet  *binding_set,
   g_assert ((modmask & GDK_SHIFT_MASK) == 0);
 
   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
-                                "move_cursor", 3,
+                                "move-cursor", 3,
                                 G_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,
+                                "move-cursor", 3,
                                 G_TYPE_ENUM, step,
                                 G_TYPE_INT, count,
                                 G_TYPE_BOOLEAN, TRUE);
@@ -478,7 +499,14 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
   widget_class->motion_notify_event = gtk_text_view_motion_event;
   widget_class->expose_event = gtk_text_view_expose_event;
   widget_class->focus = gtk_text_view_focus;
-  
+
+  /* need to override the base class function via override_class_handler,
+   * because the signal slot is not available in GtkWidgetCLass
+   */
+  g_signal_override_class_handler ("move-focus",
+                                   GTK_TYPE_TEXT_VIEW,
+                                   G_CALLBACK (gtk_text_view_move_focus));
+
   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;
@@ -505,7 +533,7 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
   klass->copy_clipboard = gtk_text_view_copy_clipboard;
   klass->paste_clipboard = gtk_text_view_paste_clipboard;
   klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
-  klass->move_focus = gtk_text_view_move_focus;
+  klass->move_focus = gtk_text_view_compat_move_focus;
   klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
 
   /*
@@ -593,7 +621,7 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                                    g_param_spec_int ("indent",
                                                     P_("Indent"),
                                                     P_("Amount to indent the paragraph, in pixels"),
-                                                    0,
+                                                    G_MININT,
                                                     G_MAXINT,
                                                     0,
                                                     GTK_PARAM_READWRITE));
@@ -638,6 +666,26 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                                                         TRUE,
                                                         GTK_PARAM_READWRITE));
 
+   /**
+    * GtkTextView:im-module:
+    *
+    * Which IM (input method) module should be used for this entry. 
+    * See #GtkIMContext.
+    *
+    * Setting this to a non-%NULL value overrides the
+    * system-wide IM module setting. See the GtkSettings 
+    * #GtkSettings:gtk-im-module property.
+    *
+    * Since: 2.16
+    */
+   g_object_class_install_property (gobject_class,
+                                    PROP_IM_MODULE,
+                                    g_param_spec_string ("im-module",
+                                                         P_("IM module"),
+                                                         P_("Which IM module should be used"),
+                                                         NULL,
+                                                         GTK_PARAM_READWRITE));
+
   /*
    * Style properties
    */
@@ -654,21 +702,35 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
 
   /**
    * GtkTextView::move-cursor: 
-   * @widget: the object which received the signal
+   * @text_view: the object which received the signal
    * @step: the granularity of the move, as a #GtkMovementStep
    * @count: the number of @step units to move
    * @extend_selection: %TRUE if the move should extend the selection
    *  
-   * The ::move-cursor signal is a keybinding signal which gets emitted
-   * when the user initiates a cursor movement. 
+   * The ::move-cursor signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted when the user initiates a cursor movement. 
+   * If the cursor is not visible in @text_view, this signal causes
+   * the viewport to be moved instead.
    *
    * Applications should not connect to it, but may emit it with 
-   * g_signal_emit_by_name() if they need to control scrolling 
+   * g_signal_emit_by_name() if they need to control the cursor
    * programmatically.
    *
+   * The default bindings for this signal come in two variants,
+   * the variant with the Shift modifier extends the selection,
+   * the variant without the Shift modifer does not.
+   * There are too many key combinations to list them all here.
+   * <itemizedlist>
+   * <listitem>Arrow keys move by individual characters/lines</listitem>
+   * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
+   * <listitem>Home/End keys move to the ends of the buffer</listitem>
+   * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
+   * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
+   * </itemizedlist>
    */
   signals[MOVE_CURSOR] = 
-    g_signal_new (I_("move_cursor"),
+    g_signal_new (I_("move-cursor"),
                  G_OBJECT_CLASS_TYPE (gobject_class), 
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 
                  G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
@@ -679,8 +741,23 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  G_TYPE_INT, 
                  G_TYPE_BOOLEAN);
 
+  /**
+   * GtkTextView::page-horizontally:
+   * @text_view: the object which received the signal
+   * @count: the number of @step units to move
+   * @extend_selection: %TRUE if the move should extend the selection
+   *
+   * The ::page-horizontally signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which can be bound to key combinations to allow the user
+   * to initiate horizontal cursor movement by pages.  
+   * 
+   * This signal should not be used anymore, instead use the
+   * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
+   * granularity.
+   */
   signals[PAGE_HORIZONTALLY] =
-    g_signal_new (I_("page_horizontally"),
+    g_signal_new (I_("page-horizontally"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
@@ -690,19 +767,45 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  G_TYPE_INT,
                  G_TYPE_BOOLEAN);
   
+  /**
+   * GtkTextView::move-viewport:
+   * @text_view: the object which received the signal
+   * @step: the granularity of the move, as a #GtkMovementStep
+   * @count: the number of @step units to move
+   *
+   * The ::move-viewport signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which can be bound to key combinations to allow the user
+   * to move the viewport, i.e. change what part of the text view
+   * is visible in a containing scrolled window.
+   *
+   * There are no default bindings for this signal.
+   */
   signals[MOVE_VIEWPORT] =
-    _gtk_binding_signal_new (I_("move_viewport"),
-                            G_OBJECT_CLASS_TYPE (gobject_class),
-                            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
-                            G_CALLBACK (gtk_text_view_move_viewport),
-                            NULL, NULL,
-                            _gtk_marshal_VOID__ENUM_INT,
-                            G_TYPE_NONE, 2,
-                            GTK_TYPE_SCROLL_STEP,
-                            G_TYPE_INT);
+    g_signal_new_class_handler (I_("move-viewport"),
+                                G_OBJECT_CLASS_TYPE (gobject_class),
+                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                G_CALLBACK (gtk_text_view_move_viewport),
+                                NULL, NULL,
+                                _gtk_marshal_VOID__ENUM_INT,
+                                G_TYPE_NONE, 2,
+                                GTK_TYPE_SCROLL_STEP,
+                                G_TYPE_INT);
 
+  /**
+   * GtkTextView::set-anchor:
+   * @text_view: the object which received the signal
+   *
+   * The ::set-anchor signal is a
+   * <link linkend="keybinding-signals">keybinding signal</link>
+   * which gets emitted when the user initiates setting the "anchor" 
+   * mark. The "anchor" mark gets placed at the same position as the
+   * "insert" mark.
+   *
+   * This signal has no default bindings.
+   */   
   signals[SET_ANCHOR] =
-    g_signal_new (I_("set_anchor"),
+    g_signal_new (I_("set-anchor"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
@@ -710,8 +813,20 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkTextView::insert-at-cursor:
+   * @text_view: the object which received the signal
+   * @string: the string to insert
+   *
+   * The ::insert-at-cursor signal is a
+   * <link linkend="keybinding-signals">keybinding signal</link>
+   * which gets emitted when the user initiates the insertion of a 
+   * fixed string at the cursor.
+   *
+   * This signal has no default bindings.
+   */
   signals[INSERT_AT_CURSOR] =
-    g_signal_new (I_("insert_at_cursor"),
+    g_signal_new (I_("insert-at-cursor"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
@@ -720,8 +835,27 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  G_TYPE_NONE, 1,
                  G_TYPE_STRING);
 
+  /**
+   * GtkTextView::delete-from-cursor:
+   * @text_view: the object which received the signal
+   * @type: the granularity of the deletion, as a #GtkDeleteType
+   * @count: the number of @type units to delete
+   *
+   * The ::delete-from-cursor signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted when the user initiates a text deletion.
+   *
+   * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
+   * if there is one, otherwise it deletes the requested number
+   * of characters.
+   *
+   * The default bindings for this signal are
+   * Delete for deleting a character, Ctrl-Delete for 
+   * deleting a word and Ctrl-Backspace for deleting a word 
+   * backwords.
+   */
   signals[DELETE_FROM_CURSOR] =
-    g_signal_new (I_("delete_from_cursor"),
+    g_signal_new (I_("delete-from-cursor"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
@@ -731,6 +865,17 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  GTK_TYPE_DELETE_TYPE,
                  G_TYPE_INT);
 
+  /**
+   * GtkTextView::backspace:
+   * @text_view: the object which received the signal
+   *
+   * The ::backspace signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted when the user asks for it.
+   * 
+   * The default bindings for this signal are
+   * Backspace and Shift-Backspace.
+   */
   signals[BACKSPACE] =
     g_signal_new (I_("backspace"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
@@ -740,8 +885,19 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkTextView::cut-clipboard:
+   * @text_view: the object which received the signal
+   *
+   * The ::cut-clipboard signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to cut the selection to the clipboard.
+   * 
+   * The default bindings for this signal are
+   * Ctrl-x and Shift-Delete.
+   */
   signals[CUT_CLIPBOARD] =
-    g_signal_new (I_("cut_clipboard"),
+    g_signal_new (I_("cut-clipboard"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
@@ -749,8 +905,19 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkTextView::copy-clipboard:
+   * @text_view: the object which received the signal
+   *
+   * The ::copy-clipboard signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to copy the selection to the clipboard.
+   * 
+   * The default bindings for this signal are
+   * Ctrl-c and Ctrl-Insert.
+   */
   signals[COPY_CLIPBOARD] =
-    g_signal_new (I_("copy_clipboard"),
+    g_signal_new (I_("copy-clipboard"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
@@ -758,8 +925,20 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkTextView::paste-clipboard:
+   * @text_view: the object which received the signal
+   *
+   * The ::paste-clipboard signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to paste the contents of the clipboard 
+   * into the text view.
+   * 
+   * The default bindings for this signal are
+   * Ctrl-v and Shift-Insert.
+   */
   signals[PASTE_CLIPBOARD] =
-    g_signal_new (I_("paste_clipboard"),
+    g_signal_new (I_("paste-clipboard"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
@@ -767,8 +946,18 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkTextView::toggle-overwrite:
+   * @text_view: the object which received the signal
+   *
+   * The ::toggle-overwrite signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to toggle the overwrite mode of the text view.
+   * 
+   * The default bindings for this signal is Insert.
+   */ 
   signals[TOGGLE_OVERWRITE] =
-    g_signal_new (I_("toggle_overwrite"),
+    g_signal_new (I_("toggle-overwrite"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
@@ -776,18 +965,17 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
-  signals[MOVE_FOCUS] =
-    g_signal_new (I_("move_focus"),
-                 G_OBJECT_CLASS_TYPE (gobject_class),
-                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
-                 G_STRUCT_OFFSET (GtkTextViewClass, move_focus),
-                 NULL, NULL,
-                 _gtk_marshal_VOID__ENUM,
-                 G_TYPE_NONE, 1,
-                 GTK_TYPE_DIRECTION_TYPE);
-  
+  /**
+   * GtkTextView::set-scroll-adjustments
+   * @horizontal: the horizontal #GtkAdjustment
+   * @vertical: the vertical #GtkAdjustment
+   *
+   * Set the scroll adjustments for the text view. Usually scrolled containers
+   * like #GtkScrolledWindow will emit this signal to connect two instances
+   * of #GtkScrollbar to the scroll directions of the #GtkTextView.
+   */
   signals[SET_SCROLL_ADJUSTMENTS] =
-    g_signal_new (I_("set_scroll_adjustments"),
+    g_signal_new (I_("set-scroll-adjustments"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
@@ -798,8 +986,19 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  GTK_TYPE_ADJUSTMENT);
   widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
 
+  /**
+   * GtkTextView::populate-popup:
+   * @entry: The text view on which the signal is emitted
+   * @menu: the menu that is being populated
+   *
+   * The ::populate-popup signal gets emitted before showing the 
+   * context menu of the text view.
+   *
+   * If you need to add items to the context menu, connect
+   * to this signal and append your menuitems to the @menu.
+   */
   signals[POPULATE_POPUP] =
-    g_signal_new (I_("populate_popup"),
+    g_signal_new (I_("populate-popup"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
@@ -808,17 +1007,71 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                  G_TYPE_NONE, 1,
                  GTK_TYPE_MENU);
   
+  /**
+   * GtkTextView::select-all:
+   * @text_view: the object which received the signal
+   * @select: %TRUE to select, %FALSE to unselect
+   *
+   * The ::select-all signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to select or unselect the complete
+   * contents of the text view.
+   *
+   * The default bindings for this signal are Ctrl-a and Ctrl-/ 
+   * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
+   */
   signals[SELECT_ALL] =
-    _gtk_binding_signal_new (I_("select_all"),
-                            G_OBJECT_CLASS_TYPE (object_class),
-                            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
-                            G_CALLBACK (gtk_text_view_select_all),
-                            NULL, NULL,
-                            _gtk_marshal_VOID__BOOLEAN,
-                            G_TYPE_NONE, 1,
-                            G_TYPE_BOOLEAN, TRUE);
+    g_signal_new_class_handler (I_("select-all"),
+                                G_OBJECT_CLASS_TYPE (object_class),
+                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                G_CALLBACK (gtk_text_view_select_all),
+                                NULL, NULL,
+                                _gtk_marshal_VOID__BOOLEAN,
+                                G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
+
+  /**
+   * GtkTextView::toggle-cursor-visible:
+   * @text_view: the object which received the signal
+   *
+   * The ::toggle-cursor-visible signal is a 
+   * <link linkend="keybinding-signals">keybinding signal</link> 
+   * which gets emitted to toggle the visibility of the cursor.
+   *
+   * The default binding for this signal is F7.
+   */ 
+  signals[TOGGLE_CURSOR_VISIBLE] =
+    g_signal_new_class_handler (I_("toggle-cursor-visible"),
+                                G_OBJECT_CLASS_TYPE (object_class),
+                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                G_CALLBACK (gtk_text_view_toggle_cursor_visible),
+                                NULL, NULL,
+                                _gtk_marshal_VOID__VOID,
+                                G_TYPE_NONE, 0);
+
+  /**
+   * GtkTextView::preedit-changed:
+   * @text_view: the object which received the signal
+   * @preedit: the current preedit string
+   *
+   * If an input method is used, the typed text will not immediately
+   * be committed to the buffer. So if you are interested in the text,
+   * connect to this signal.
+   *
+   * This signal is only emitted if the text at the given position
+   * is actually editable.
+   *
+   * Since: 2.20
+   */
+  signals[PREEDIT_CHANGED] =
+    g_signal_new_class_handler (I_("preedit-changed"),
+                                G_OBJECT_CLASS_TYPE (object_class),
+                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                NULL,
+                                NULL, NULL,
+                                _gtk_marshal_VOID__STRING,
+                                G_TYPE_NONE, 1,
+                                G_TYPE_STRING);
 
-  
   /*
    * Key bindings
    */
@@ -848,7 +1101,7 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                     GTK_MOVEMENT_WORDS, -1);
 
   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
-                    GTK_MOVEMENT_WORDS, 1);
+                    GTK_MOVEMENT_WORDS, -1);
   
   add_move_binding (binding_set, GDK_Up, 0,
                     GTK_MOVEMENT_DISPLAY_LINES, -1);
@@ -924,30 +1177,30 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
 
   /* Select all */
   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
-                               "select_all", 1,
+                               "select-all", 1,
                                G_TYPE_BOOLEAN, TRUE);
 
   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
-                               "select_all", 1,
+                               "select-all", 1,
                                G_TYPE_BOOLEAN, TRUE);
   
   /* Unselect all */
   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
-                                "select_all", 1,
+                                "select-all", 1,
                                 G_TYPE_BOOLEAN, FALSE);
 
   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
-                                "select_all", 1,
+                                "select-all", 1,
                                 G_TYPE_BOOLEAN, FALSE);
 
   /* Deleting text */
   gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
-                               "delete_from_cursor", 2,
+                               "delete-from-cursor", 2,
                                G_TYPE_ENUM, GTK_DELETE_CHARS,
                                G_TYPE_INT, 1);
 
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
-                               "delete_from_cursor", 2,
+                               "delete-from-cursor", 2,
                                G_TYPE_ENUM, GTK_DELETE_CHARS,
                                G_TYPE_INT, 1);
   
@@ -959,56 +1212,77 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                                "backspace", 0);
 
   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
-                               "delete_from_cursor", 2,
+                               "delete-from-cursor", 2,
                                G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
                                G_TYPE_INT, 1);
 
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
-                               "delete_from_cursor", 2,
+                               "delete-from-cursor", 2,
                                G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
                                G_TYPE_INT, 1);
   
   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
-                               "delete_from_cursor", 2,
+                               "delete-from-cursor", 2,
                                G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
                                G_TYPE_INT, -1);
 
+  gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
+                               "delete-from-cursor", 2,
+                               G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
+                               G_TYPE_INT, 1);
+
+  gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
+                               "delete-from-cursor", 2,
+                               G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
+                               G_TYPE_INT, 1);
+
+  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
+                               "delete-from-cursor", 2,
+                               G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
+                               G_TYPE_INT, -1);
+
   /* Cut/copy/paste */
 
   gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
-                               "cut_clipboard", 0);
+                               "cut-clipboard", 0);
   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
-                               "copy_clipboard", 0);
+                               "copy-clipboard", 0);
   gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
-                               "paste_clipboard", 0);
+                               "paste-clipboard", 0);
 
   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
-                               "cut_clipboard", 0);
+                               "cut-clipboard", 0);
   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
-                               "copy_clipboard", 0);
+                               "copy-clipboard", 0);
   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
-                               "paste_clipboard", 0);
+                               "paste-clipboard", 0);
 
   /* Overwrite */
   gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
-                               "toggle_overwrite", 0);
+                               "toggle-overwrite", 0);
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
-                               "toggle_overwrite", 0);
+                               "toggle-overwrite", 0);
+
+  /* Caret mode */
+  gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
+                               "toggle-cursor-visible", 0);
 
   /* Control-tab focus motion */
   gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
-                               "move_focus", 1,
+                               "move-focus", 1,
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
-                               "move_focus", 1,
+                               "move-focus", 1,
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
   
   gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
-                               "move_focus", 1,
+                               "move-focus", 1,
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
-                               "move_focus", 1,
+                               "move-focus", 1,
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
+
+  g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
 }
 
 static void
@@ -1048,11 +1322,11 @@ gtk_text_view_init (GtkTextView *text_view)
 
   g_signal_connect (text_view->im_context, "commit",
                     G_CALLBACK (gtk_text_view_commit_handler), text_view);
-  g_signal_connect (text_view->im_context, "preedit_changed",
+  g_signal_connect (text_view->im_context, "preedit-changed",
                    G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
-  g_signal_connect (text_view->im_context, "retrieve_surrounding",
+  g_signal_connect (text_view->im_context, "retrieve-surrounding",
                    G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
-  g_signal_connect (text_view->im_context, "delete_surrounding",
+  g_signal_connect (text_view->im_context, "delete-surrounding",
                    G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
 
   text_view->cursor_visible = TRUE;
@@ -1093,7 +1367,7 @@ gtk_text_view_new (void)
  *
  * Creates a new #GtkTextView widget displaying the buffer
  * @buffer. One buffer can be shared among many widgets.
- * @buffer may be NULL to create a default buffer, in which case
+ * @buffer may be %NULL to create a default buffer, in which case
  * this function is equivalent to gtk_text_view_new(). The
  * text view adds its own reference count to the buffer; it does not
  * take over an existing reference.
@@ -1115,14 +1389,13 @@ gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
 /**
  * gtk_text_view_set_buffer:
  * @text_view: a #GtkTextView
- * @buffer: a #GtkTextBuffer
+ * @buffer: (allow-none): a #GtkTextBuffer
  *
  * Sets @buffer as the buffer being displayed by @text_view. The previous
  * buffer displayed by the text view is unreferenced, and a reference is
  * added to @buffer. If you owned a reference to @buffer before passing it
  * to this function, you must remove that reference yourself; #GtkTextView
  * will not "adopt" it.
- *
  **/
 void
 gtk_text_view_set_buffer (GtkTextView   *text_view,
@@ -1163,28 +1436,37 @@ gtk_text_view_set_buffer (GtkTextView   *text_view,
       g_signal_handlers_disconnect_by_func (text_view->buffer,
                                             gtk_text_view_target_list_notify,
                                             text_view);
-      g_object_unref (text_view->buffer);
-      text_view->dnd_mark = NULL;
+      g_signal_handlers_disconnect_by_func (text_view->buffer,
+                                            gtk_text_view_paste_done_handler,
+                                            text_view);
 
       if (GTK_WIDGET_REALIZED (text_view))
        {
          GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
                                                              GDK_SELECTION_PRIMARY);
          gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
-       }
+        }
+
+      if (text_view->layout)
+        gtk_text_layout_set_buffer (text_view->layout, NULL);
+
+      g_object_unref (text_view->buffer);
+      text_view->dnd_mark = NULL;
+      text_view->first_para_mark = NULL;
+      cancel_pending_scroll (text_view);
     }
 
   text_view->buffer = buffer;
 
+  if (text_view->layout)
+    gtk_text_layout_set_buffer (text_view->layout, buffer);
+
   if (buffer != NULL)
     {
       GtkTextIter start;
 
       g_object_ref (buffer);
 
-      if (text_view->layout)
-        gtk_text_layout_set_buffer (text_view->layout, buffer);
-
       gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
 
       text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
@@ -1197,12 +1479,15 @@ gtk_text_view_set_buffer (GtkTextView   *text_view,
 
       text_view->first_para_pixels = 0;
 
-      g_signal_connect (text_view->buffer, "mark_set",
+      g_signal_connect (text_view->buffer, "mark-set",
                        G_CALLBACK (gtk_text_view_mark_set_handler),
                         text_view);
       g_signal_connect (text_view->buffer, "notify::paste-target-list",
                        G_CALLBACK (gtk_text_view_target_list_notify),
                         text_view);
+      g_signal_connect (text_view->buffer, "paste-done",
+                       G_CALLBACK (gtk_text_view_paste_done_handler),
+                        text_view);
 
       gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
 
@@ -1216,7 +1501,7 @@ gtk_text_view_set_buffer (GtkTextView   *text_view,
 
   g_object_notify (G_OBJECT (text_view), "buffer");
   
-  if (GTK_WIDGET_VISIBLE (text_view))
+  if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
     gtk_widget_queue_draw (GTK_WIDGET (text_view));
 
   DV(g_print ("Invalidating due to set_buffer\n"));
@@ -1245,7 +1530,7 @@ get_buffer (GtkTextView *text_view)
  * The reference count on the buffer is not incremented; the caller
  * of this function won't own a new reference.
  *
- * Return value: a #GtkTextBuffer
+ * Return value: (transfer none): a #GtkTextBuffer
  **/
 GtkTextBuffer*
 gtk_text_view_get_buffer (GtkTextView *text_view)
@@ -1267,7 +1552,6 @@ gtk_text_view_get_buffer (GtkTextView *text_view)
  * currently-displayed portion.  If you have coordinates from an
  * event, you have to convert those to buffer coordinates with
  * gtk_text_view_window_to_buffer_coords().
- *
  **/
 void
 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
@@ -1288,7 +1572,7 @@ gtk_text_view_get_iter_at_location (GtkTextView *text_view,
  * gtk_text_view_get_iter_at_position:
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
- * @trailing: location to store an integer indicating where
+ * @trailing: if non-%NULL, location to store an integer indicating where
  *    in the grapheme the user clicked. It will either be
  *    zero, or the number of characters in the grapheme. 
  *    0 represents the trailing edge of the grapheme.
@@ -1334,7 +1618,6 @@ gtk_text_view_get_iter_at_position (GtkTextView *text_view,
  * The rectangle position is in buffer coordinates; use
  * gtk_text_view_buffer_to_window_coords() to convert these
  * coordinates to coordinates for one of the windows in the text view.
- *
  **/
 void
 gtk_text_view_get_iter_location (GtkTextView       *text_view,
@@ -1359,7 +1642,6 @@ gtk_text_view_get_iter_location (GtkTextView       *text_view,
  * Gets the y coordinate of the top of the line containing @iter,
  * and the height of the line. The coordinate is a buffer coordinate;
  * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
- *
  **/
 void
 gtk_text_view_get_line_yrange (GtkTextView       *text_view,
@@ -1436,22 +1718,25 @@ set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
  * @within_margin: margin as a [0.0,0.5) fraction of screen size
- * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
- * @xalign: horizontal alignment of mark within visible area.
+ * @use_align: whether to use alignment arguments (if %FALSE, 
+ *    just get the mark onscreen)
+ * @xalign: horizontal alignment of mark within visible area
  * @yalign: vertical alignment of mark within visible area
  *
  * Scrolls @text_view so that @iter is on the screen in the position
  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
- * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
- * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
- * possibly not scrolling at all. The effective screen for purposes
- * of this function is reduced by a margin of size @within_margin.
- * NOTE: This function uses the currently-computed height of the
- * lines in the text buffer. Note that line heights are computed
- * in an idle handler; so this function may not have the desired effect
- * if it's called before the height computations. To avoid oddness,
- * consider using gtk_text_view_scroll_to_mark() which saves a point
- * to be scrolled to after line validation.
+ * left or top, 1.0 indicates right or bottom, 0.5 means center. 
+ * If @use_align is %FALSE, the text scrolls the minimal distance to 
+ * get the mark onscreen, possibly not scrolling at all. The effective 
+ * screen for purposes of this function is reduced by a margin of size 
+ * @within_margin.
+ *
+ * Note that this function uses the currently-computed height of the
+ * lines in the text buffer. Line heights are computed in an idle 
+ * handler; so this function may not have the desired effect if it's 
+ * called before the height computations. To avoid oddness, consider 
+ * using gtk_text_view_scroll_to_mark() which saves a point to be 
+ * scrolled to after line validation.
  *
  * Return value: %TRUE if scrolling occurred
  **/
@@ -1606,9 +1891,13 @@ gtk_text_view_scroll_to_iter (GtkTextView   *text_view,
     }
   
   if (retval)
-    DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
+    {
+      DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
+    }
   else
-    DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
+    {
+      DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
+    }
   
   return retval;
 }
@@ -1826,22 +2115,66 @@ gtk_text_view_update_im_spot_location (GtkTextView *text_view)
   gtk_im_context_set_cursor_location (text_view->im_context, &area);
 }
 
+static gboolean
+do_update_im_spot_location (gpointer text_view)
+{
+  GtkTextViewPrivate *priv;
+
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
+  priv->im_spot_idle = 0;
+
+  gtk_text_view_update_im_spot_location (text_view);
+  return FALSE;
+}
+
+static void
+queue_update_im_spot_location (GtkTextView *text_view)
+{
+  GtkTextViewPrivate *priv;
+
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
+
+  /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
+   * so we don't wait until the entire buffer has been validated. */
+  if (!priv->im_spot_idle)
+    priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
+                                                   do_update_im_spot_location,
+                                                   text_view,
+                                                   NULL);
+}
+
+static void
+flush_update_im_spot_location (GtkTextView *text_view)
+{
+  GtkTextViewPrivate *priv;
+
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
+
+  if (priv->im_spot_idle)
+    {
+      g_source_remove (priv->im_spot_idle);
+      priv->im_spot_idle = 0;
+      gtk_text_view_update_im_spot_location (text_view);
+    }
+}
+
 /**
  * gtk_text_view_scroll_to_mark:
  * @text_view: a #GtkTextView
  * @mark: a #GtkTextMark
  * @within_margin: margin as a [0.0,0.5) fraction of screen size
- * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
- * @xalign: horizontal alignment of mark within visible area.
+ * @use_align: whether to use alignment arguments (if %FALSE, just 
+ *    get the mark onscreen)
+ * @xalign: horizontal alignment of mark within visible area
  * @yalign: vertical alignment of mark within visible area
  *
  * Scrolls @text_view so that @mark is on the screen in the position
  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
- * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
- * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
- * possibly not scrolling at all. The effective screen for purposes
- * of this function is reduced by a margin of size @within_margin.
- *
+ * left or top, 1.0 indicates right or bottom, 0.5 means center. 
+ * If @use_align is %FALSE, the text scrolls the minimal distance to 
+ * get the mark onscreen, possibly not scrolling at all. The effective 
+ * screen for purposes of this function is reduced by a margin of size 
+ * @within_margin.
  **/
 void
 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
@@ -1857,6 +2190,11 @@ gtk_text_view_scroll_to_mark (GtkTextView *text_view,
   g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
   g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
 
+  /* We need to verify that the buffer contains the mark, otherwise this
+   * can lead to data structure corruption later on.
+   */
+  g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
+
   gtk_text_view_queue_scroll (text_view, mark,
                               within_margin,
                               use_align,
@@ -1878,7 +2216,6 @@ gtk_text_view_scroll_to_mark (GtkTextView *text_view,
  * 
  * Scrolls @text_view the minimum distance such that @mark is contained
  * within the visible area of the widget.
- * 
  **/
 void
 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
@@ -1887,6 +2224,11 @@ gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
 
+  /* We need to verify that the buffer contains the mark, otherwise this
+   * can lead to data structure corruption later on.
+   */
+  g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
+
   gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
 }
 
@@ -2026,10 +2368,22 @@ gtk_text_view_set_editable (GtkTextView *text_view,
 
   if (text_view->editable != setting)
     {
+      if (!setting)
+       {
+         gtk_text_view_reset_im_context(text_view);
+         if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
+           gtk_im_context_focus_out (text_view->im_context);
+       }
+
       text_view->editable = setting;
 
+      if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
+       gtk_im_context_focus_in (text_view->im_context);
+
       if (text_view->layout)
         {
+         gtk_text_layout_set_overwrite_mode (text_view->layout,
+                                             text_view->overwrite_mode && text_view->editable);
           text_view->layout->default_style->editable = text_view->editable;
           gtk_text_layout_default_style_changed (text_view->layout);
         }
@@ -2062,7 +2416,6 @@ gtk_text_view_get_editable (GtkTextView *text_view)
  * 
  * Sets the default number of blank pixels above paragraphs in @text_view.
  * Tags in the buffer for @text_view may override the defaults.
- * 
  **/
 void
 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
@@ -2108,7 +2461,6 @@ gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
  * Sets the default number of pixels of blank space
  * to put below paragraphs in @text_view. May be overridden
  * by tags applied to @text_view's buffer. 
- * 
  **/
 void
 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
@@ -2154,7 +2506,6 @@ gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
  * Sets the default number of pixels of blank space to leave between
  * display/wrapped lines within a paragraph. May be overridden by
  * tags in @text_view's buffer.
- * 
  **/
 void
 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
@@ -2245,7 +2596,6 @@ gtk_text_view_get_justification (GtkTextView *text_view)
  * 
  * Sets the default left margin for text in @text_view.
  * Tags in the buffer may override the default.
- * 
  **/
 void
 gtk_text_view_set_left_margin (GtkTextView *text_view,
@@ -2291,7 +2641,6 @@ gtk_text_view_get_left_margin (GtkTextView *text_view)
  *
  * Sets the default right margin for text in the text view.
  * Tags in the buffer may override the default.
- * 
  **/
 void
 gtk_text_view_set_right_margin (GtkTextView *text_view,
@@ -2337,7 +2686,6 @@ gtk_text_view_get_right_margin (GtkTextView *text_view)
  *
  * Sets the default indentation for paragraphs in @text_view.
  * Tags in the buffer may override the default.
- * 
  **/
 void
 gtk_text_view_set_indent (GtkTextView *text_view,
@@ -2384,7 +2732,6 @@ gtk_text_view_get_indent (GtkTextView *text_view)
  *
  * Sets the default tab stops for paragraphs in @text_view.
  * Tags in the buffer may override the default.
- * 
  **/
 void
 gtk_text_view_set_tabs (GtkTextView   *text_view,
@@ -2421,7 +2768,8 @@ gtk_text_view_set_tabs (GtkTextView   *text_view,
  * "standard" (8-space) tabs are used. Free the return value
  * with pango_tab_array_free().
  * 
- * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
+ * Return value: copy of default tab array, or %NULL if "standard" 
+ *    tabs are used; must be freed with pango_tab_array_free().
  **/
 PangoTabArray*
 gtk_text_view_get_tabs (GtkTextView *text_view)
@@ -2431,6 +2779,12 @@ gtk_text_view_get_tabs (GtkTextView *text_view)
   return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
 }
 
+static void
+gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
+{
+  gtk_text_view_set_cursor_visible (text_view, !text_view->cursor_visible);
+}
+
 /**
  * gtk_text_view_set_cursor_visible:
  * @text_view: a #GtkTextView
@@ -2452,7 +2806,7 @@ gtk_text_view_set_cursor_visible (GtkTextView *text_view,
     {
       text_view->cursor_visible = setting;
 
-      if (GTK_WIDGET_HAS_FOCUS (text_view))
+      if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
         {
           if (text_view->layout)
             {
@@ -2489,7 +2843,7 @@ gtk_text_view_get_cursor_visible (GtkTextView *text_view)
  * Moves the cursor to the currently visible region of the
  * buffer, it it isn't there already.
  *
- * Return value: TRUE if the cursor had to be moved.
+ * Return value: %TRUE if the cursor had to be moved.
  **/
 gboolean
 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
@@ -2499,8 +2853,7 @@ gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
 
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   if (clamp_iter_onscreen (text_view, &insert))
     {
@@ -2532,22 +2885,38 @@ static void
 gtk_text_view_destroy (GtkObject *object)
 {
   GtkTextView *text_view;
-  
+  GtkTextViewPrivate *priv;
+
   text_view = GTK_TEXT_VIEW (object);
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
 
   gtk_text_view_remove_validate_idles (text_view);
   gtk_text_view_set_buffer (text_view, NULL);
   gtk_text_view_destroy_layout (text_view);
 
-  (* GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy) (object);
+  if (text_view->scroll_timeout)
+    {
+      g_source_remove (text_view->scroll_timeout);
+      text_view->scroll_timeout = 0;
+    }
+
+  if (priv->im_spot_idle)
+    {
+      g_source_remove (priv->im_spot_idle);
+      priv->im_spot_idle = 0;
+    }
+
+  GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
 }
 
 static void
 gtk_text_view_finalize (GObject *object)
 {
   GtkTextView *text_view;
+  GtkTextViewPrivate *priv;
 
   text_view = GTK_TEXT_VIEW (object);
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
 
   g_assert (text_view->buffer == NULL);
 
@@ -2579,8 +2948,10 @@ gtk_text_view_finalize (GObject *object)
     text_window_free (text_view->bottom_window);
 
   g_object_unref (text_view->im_context);
-  
-  (* G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize) (object);
+
+  g_free (priv->im_module);
+
+  G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
 }
 
 static void
@@ -2590,8 +2961,10 @@ gtk_text_view_set_property (GObject         *object,
                            GParamSpec      *pspec)
 {
   GtkTextView *text_view;
+  GtkTextViewPrivate *priv;
 
   text_view = GTK_TEXT_VIEW (object);
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
 
   switch (prop_id)
     {
@@ -2651,8 +3024,15 @@ gtk_text_view_set_property (GObject         *object,
       gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
       break;
       
+    case PROP_IM_MODULE:
+      g_free (priv->im_module);
+      priv->im_module = g_value_dup_string (value);
+      if (GTK_IS_IM_MULTICONTEXT (text_view->im_context))
+        gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (text_view->im_context), priv->im_module);
+      break;
+
     default:
-      g_assert_not_reached ();
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
     }
 }
@@ -2664,8 +3044,10 @@ gtk_text_view_get_property (GObject         *object,
                            GParamSpec      *pspec)
 {
   GtkTextView *text_view;
+  GtkTextViewPrivate *priv;
 
   text_view = GTK_TEXT_VIEW (object);
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
 
   switch (prop_id)
     {
@@ -2725,6 +3107,10 @@ gtk_text_view_get_property (GObject         *object,
       g_value_set_boolean (value, text_view->accepts_tab);
       break;
       
+    case PROP_IM_MODULE:
+      g_value_set_string (value, priv->im_module);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -3228,8 +3614,6 @@ first_validate_callback (gpointer data)
 {
   GtkTextView *text_view = data;
 
-  GDK_THREADS_ENTER ();
-  
   /* Note that some of this code is duplicated at the end of size_allocate,
    * keep in sync with that.
    */
@@ -3238,8 +3622,6 @@ first_validate_callback (gpointer data)
 
   gtk_text_view_flush_first_validate (text_view);
   
-  GDK_THREADS_LEAVE ();
-  
   return FALSE;
 }
 
@@ -3249,8 +3631,6 @@ incremental_validate_callback (gpointer data)
   GtkTextView *text_view = data;
   gboolean result = TRUE;
 
-  GDK_THREADS_ENTER ();
-  
   DV(g_print(G_STRLOC"\n"));
   
   gtk_text_layout_validate (text_view->layout, 2000);
@@ -3263,8 +3643,6 @@ incremental_validate_callback (gpointer data)
       result = FALSE;
     }
 
-  GDK_THREADS_LEAVE ();
-
   return result;
 }
 
@@ -3282,14 +3660,14 @@ gtk_text_view_invalidate (GtkTextView *text_view)
   
   if (!text_view->first_validate_idle)
     {
-      text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
+      text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
       DV (g_print (G_STRLOC": adding first validate idle %d\n",
                    text_view->first_validate_idle));
     }
       
   if (!text_view->incremental_validate_idle)
     {
-      text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
+      text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
       DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
                    text_view->incremental_validate_idle));
     }
@@ -3364,7 +3742,7 @@ changed_handler (GtkTextLayout     *layout,
             text_window_invalidate_rect (text_view->bottom_window,
                                          &redraw_rect);
 
-          gtk_text_view_update_im_spot_location (text_view);
+          queue_update_im_spot_location (text_view);
         }
     }
   
@@ -3473,23 +3851,19 @@ gtk_text_view_realize (GtkWidget *widget)
   gdk_window_set_background (widget->window,
                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
 
-  text_window_realize (text_view->text_window, widget->window);
+  text_window_realize (text_view->text_window, widget);
 
   if (text_view->left_window)
-    text_window_realize (text_view->left_window,
-                         widget->window);
+    text_window_realize (text_view->left_window, widget);
 
   if (text_view->top_window)
-    text_window_realize (text_view->top_window,
-                         widget->window);
+    text_window_realize (text_view->top_window, widget);
 
   if (text_view->right_window)
-    text_window_realize (text_view->right_window,
-                         widget->window);
+    text_window_realize (text_view->right_window, widget);
 
   if (text_view->bottom_window)
-    text_window_realize (text_view->bottom_window,
-                         widget->window);
+    text_window_realize (text_view->bottom_window, widget);
 
   gtk_text_view_ensure_layout (text_view);
 
@@ -3509,6 +3883,9 @@ gtk_text_view_realize (GtkWidget *widget)
       
       tmp_list = tmp_list->next;
     }
+
+  /* Ensure updating the spot location. */
+  gtk_text_view_update_im_spot_location (text_view);
 }
 
 static void
@@ -3548,11 +3925,11 @@ gtk_text_view_unrealize (GtkWidget *widget)
     text_window_unrealize (text_view->bottom_window);
 
   gtk_text_view_destroy_layout (text_view);
-  
-  (* GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize) (widget);
+
+  GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
 }
 
-static void 
+static void
 gtk_text_view_set_background (GtkTextView *text_view)
 {
   GtkWidget *widget = GTK_WIDGET (text_view);
@@ -3584,6 +3961,7 @@ gtk_text_view_style_set (GtkWidget *widget,
                          GtkStyle  *previous_style)
 {
   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
+  PangoContext *ltr_context, *rtl_context;
 
   if (GTK_WIDGET_REALIZED (widget))
     {
@@ -3595,7 +3973,17 @@ gtk_text_view_style_set (GtkWidget *widget,
       gtk_text_view_set_attributes_from_style (text_view,
                                                text_view->layout->default_style,
                                                widget->style);
-      gtk_text_layout_default_style_changed (text_view->layout);
+      
+      
+      ltr_context = gtk_widget_create_pango_context (widget);
+      pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
+      rtl_context = gtk_widget_create_pango_context (widget);
+      pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
+
+      gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
+
+      g_object_unref (ltr_context);
+      g_object_unref (rtl_context);
     }
 }
 
@@ -3608,6 +3996,7 @@ gtk_text_view_direction_changed (GtkWidget        *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);
     }
 }
@@ -3617,13 +4006,26 @@ gtk_text_view_state_changed (GtkWidget      *widget,
                             GtkStateType    previous_state)
 {
   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
+  GdkCursor *cursor;
 
   if (GTK_WIDGET_REALIZED (widget))
     {
       gtk_text_view_set_background (text_view);
+
+      if (gtk_widget_is_sensitive (widget))
+        cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
+      else
+        cursor = NULL;
+
+      gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
+
+      if (cursor)
+      gdk_cursor_unref (cursor);
+
+      text_view->mouse_cursor_obscured = FALSE;
     }
 
-  if (!GTK_WIDGET_IS_SENSITIVE (widget))
+  if (!gtk_widget_is_sensitive (widget))
     {
       /* Clear any selection */
       gtk_text_view_unselect (text_view);
@@ -3635,28 +4037,15 @@ gtk_text_view_state_changed (GtkWidget      *widget,
 static void
 set_invisible_cursor (GdkWindow *window)
 {
-  GdkBitmap *empty_bitmap;
+  GdkDisplay *display;
   GdkCursor *cursor;
-  GdkColor useless;
-  char invisible_cursor_bits[] = { 0x0 };      
-       
-  useless.red = useless.green = useless.blue = 0;
-  useless.pixel = 0;
-  
-  empty_bitmap = gdk_bitmap_create_from_data (window,
-                                             invisible_cursor_bits,
-                                             1, 1);
-  
-  cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
-                                      empty_bitmap,
-                                      &useless,
-                                      &useless, 0, 0);
-  
+
+  display = gdk_drawable_get_display (window);
+  cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
   gdk_window_set_cursor (window, cursor);
   
   gdk_cursor_unref (cursor);
-  
-  g_object_unref (empty_bitmap);
 }
 
 static void
@@ -3691,7 +4080,7 @@ gtk_text_view_grab_notify (GtkWidget *widget,
 {
   if (!was_grabbed)
     {
-      gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), NULL);
+      gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
       gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
     }
 }
@@ -3811,13 +4200,10 @@ gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
   else if (event->type == GDK_KEY_PRESS ||
            event->type == GDK_KEY_RELEASE)
     {
-      GtkTextMark *insert;
       GtkTextIter iter;
 
-      insert = gtk_text_buffer_get_mark (get_buffer (text_view),
-                                         "insert");
-
-      gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
+      gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
+                                        gtk_text_buffer_get_insert (get_buffer (text_view)));
 
       return emit_event_on_tags (widget, event, &iter);
     }
@@ -3839,23 +4225,29 @@ gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
       get_buffer (text_view) == NULL)
     return FALSE;
 
+  /* Make sure input method knows where it is */
+  flush_update_im_spot_location (text_view);
+
   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
   can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
-  if (can_insert &&
-      gtk_im_context_filter_keypress (text_view->im_context, event))
+  if (gtk_im_context_filter_keypress (text_view->im_context, event))
     {
       text_view->need_im_reset = TRUE;
-      obscure = TRUE;
+      if (!can_insert)
+        gtk_text_view_reset_im_context (text_view);
+      obscure = can_insert;
       retval = TRUE;
     }
   /* Binding set */
-  else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event &&
-          GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
-    retval = TRUE;
+  else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
+    {
+      retval = TRUE;
+    }
   /* use overall editability not can_insert, more predictable for users */
   else if (text_view->editable &&
            (event->keyval == GDK_Return ||
+            event->keyval == GDK_ISO_Enter ||
             event->keyval == GDK_KP_Enter))
     {
       /* this won't actually insert the newline if the cursor isn't
@@ -3869,7 +4261,8 @@ gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
     }
   /* Pass through Tab as literal tab, unless Control is held down */
   else if ((event->keyval == GDK_Tab ||
-            event->keyval == GDK_KP_Tab) &&
+            event->keyval == GDK_KP_Tab ||
+            event->keyval == GDK_ISO_Left_Tab) &&
            !(event->state & GDK_CONTROL_MASK))
     {
       /* If the text widget isn't editable overall, or if the application
@@ -3882,9 +4275,9 @@ gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
          obscure = TRUE;
        }
       else
-       gtk_text_view_move_focus (text_view,
-                                 (event->state & GDK_SHIFT_MASK) ?
-                                 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
+       g_signal_emit_by_name (text_view, "move-focus",
+                               (event->state & GDK_SHIFT_MASK) ?
+                               GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
 
       retval = TRUE;
     }
@@ -3894,6 +4287,7 @@ gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
   if (obscure)
     gtk_text_view_obscure_mouse_cursor (text_view);
   
+  gtk_text_view_reset_blink_time (text_view);
   gtk_text_view_pend_cursor_blink (text_view);
 
   return retval;
@@ -3937,6 +4331,8 @@ gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
       return FALSE;
     }
 
+  gtk_text_view_reset_blink_time (text_view);
+
 #if 0
   /* debug hack */
   if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
@@ -3964,7 +4360,8 @@ gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
 
           if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
                                                     &start, &end) &&
-              gtk_text_iter_in_range (&iter, &start, &end))
+              gtk_text_iter_in_range (&iter, &start, &end) &&
+              !(event->state & GDK_SHIFT_MASK))
             {
               text_view->drag_start_x = event->x;
               text_view->drag_start_y = event->y;
@@ -4004,7 +4401,7 @@ gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
     {
       GtkTextIter iter;
 
-      gtk_text_view_end_selection_drag (text_view, event);      
+      gtk_text_view_end_selection_drag (text_view);
 
       gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                         &iter,
@@ -4036,7 +4433,7 @@ gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
           text_view->drag_start_y = -1;
         }
 
-      if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
+      if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
         return TRUE;
       else if (text_view->pending_place_cursor_button == event->button)
         {
@@ -4078,7 +4475,9 @@ gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
   gtk_widget_queue_draw (widget);
 
   DV(g_print (G_STRLOC": focus_in_event\n"));
-  
+
+  gtk_text_view_reset_blink_time (text_view);
+
   if (text_view->cursor_visible && text_view->layout)
     {
       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
@@ -4086,12 +4485,15 @@ gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
     }
 
   g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
-                   "direction_changed",
+                   "direction-changed",
                    G_CALLBACK (keymap_direction_changed), text_view);
   gtk_text_view_check_keymap_direction (text_view);
-  
-  text_view->need_im_reset = TRUE;
-  gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
+
+  if (text_view->editable)
+    {
+      text_view->need_im_reset = TRUE;
+      gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
+    }
 
   return FALSE;
 }
@@ -4101,6 +4503,8 @@ gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
 {
   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
 
+  gtk_text_view_end_selection_drag (text_view);
+
   gtk_widget_queue_draw (widget);
 
   DV(g_print (G_STRLOC": focus_out_event\n"));
@@ -4115,8 +4519,11 @@ gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
                                        keymap_direction_changed,
                                        text_view);
 
-  text_view->need_im_reset = TRUE;
-  gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
+  if (text_view->editable)
+    {
+      text_view->need_im_reset = TRUE;
+      gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
+    }
 
   return FALSE;
 }
@@ -4131,10 +4538,10 @@ gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
   if (event->window == text_view->text_window->bin_window &&
       text_view->drag_start_x >= 0)
     {
-      gint x, y;
+      gint x = event->x;
+      gint y = event->y;
 
-      gdk_window_get_pointer (text_view->text_window->bin_window,
-                              &x, &y, NULL);
+      gdk_event_request_motions (event);
 
       if (gtk_drag_check_threshold (widget,
                                    text_view->drag_start_x, 
@@ -4171,7 +4578,6 @@ gtk_text_view_paint (GtkWidget      *widget,
   GtkTextView *text_view;
   GList *child_exposes;
   GList *tmp_list;
-  GdkRegion *updates;
   
   text_view = GTK_TEXT_VIEW (widget);
 
@@ -4186,19 +4592,6 @@ gtk_text_view_paint (GtkWidget      *widget,
       gtk_text_view_flush_first_validate (text_view);
     }
 
-  /* More regions could have become invalid in the above loop */
-  updates = gdk_window_get_update_area (text_view->text_window->bin_window);
-  if (updates)
-    {
-      GdkRectangle rect;
-      
-      gdk_region_get_clipbox (updates, &rect);
-
-      gdk_rectangle_union (area, &rect, area);
-      
-      gdk_region_destroy (updates);
-    }
-  
   if (!text_view->onscreen_validated)
     {
       g_warning (G_STRLOC ": somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - may be a text widget bug.");
@@ -4286,9 +4679,9 @@ gtk_text_view_draw_focus (GtkWidget *widget)
                        "interior-focus", &interior_focus,
                        NULL);
   
-  if (GTK_WIDGET_DRAWABLE (widget))
+  if (gtk_widget_is_drawable (widget))
     {
-      if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
+      if (gtk_widget_has_focus (widget) && !interior_focus)
         {          
           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
                            NULL, widget, "textview",
@@ -4332,6 +4725,17 @@ gtk_text_view_focus (GtkWidget        *widget,
     }
 }
 
+static void
+gtk_text_view_move_focus (GtkWidget        *widget,
+                          GtkDirectionType  direction_type)
+{
+  GtkTextView *text_view = GTK_TEXT_VIEW (widget);
+
+  if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
+    GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
+                                                     direction_type);
+}
+
 /*
  * Container
  */
@@ -4409,9 +4813,10 @@ gtk_text_view_forall (GtkContainer *container,
   g_slist_free (copy);
 }
 
-#define CURSOR_ON_MULTIPLIER 0.66
-#define CURSOR_OFF_MULTIPLIER 0.34
-#define CURSOR_PEND_MULTIPLIER 1.0
+#define CURSOR_ON_MULTIPLIER 2
+#define CURSOR_OFF_MULTIPLIER 1
+#define CURSOR_PEND_MULTIPLIER 3
+#define CURSOR_DIVIDER 3
 
 static gboolean
 cursor_blinks (GtkTextView *text_view)
@@ -4456,41 +4861,68 @@ get_cursor_time (GtkTextView *text_view)
   return time;
 }
 
-/*
- * Blink!
- */
-
 static gint
-blink_cb (gpointer data)
+get_cursor_blink_timeout (GtkTextView *text_view)
+{
+  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
+  gint time;
+
+  g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
+
+  return time;
+}
+
+
+/*
+ * Blink!
+ */
+
+static gint
+blink_cb (gpointer data)
 {
   GtkTextView *text_view;
+  GtkTextViewPrivate *priv;
   gboolean visible;
-
-  GDK_THREADS_ENTER ();
+  gint blink_timeout;
 
   text_view = GTK_TEXT_VIEW (data);
-  
-  if (!GTK_WIDGET_HAS_FOCUS (text_view))
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
+
+  if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
     {
       g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
                  "connect a handler to this signal, it must return\n"
-                 "FALSE so the entry gets the event as well");
+                 "FALSE so the text view gets the event as well");
+
+      gtk_text_view_check_cursor_blink (text_view);
+
+      return FALSE;
     }
 
   g_assert (text_view->layout);
-  g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
   g_assert (text_view->cursor_visible);
 
   visible = gtk_text_layout_get_cursor_visible (text_view->layout);
 
-  if (visible)
-    text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
-                                             blink_cb,
-                                             text_view);
-  else
-    text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
+  blink_timeout = get_cursor_blink_timeout (text_view);
+  if (priv->blink_time > 1000 * blink_timeout &&
+      blink_timeout < G_MAXINT/1000) 
+    {
+      /* we've blinked enough without the user doing anything, stop blinking */
+      visible = 0;
+      text_view->blink_timeout = 0;
+    } 
+  else if (visible)
+    text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
                                              blink_cb,
                                              text_view);
+  else 
+    {
+      text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
+                                               blink_cb,
+                                               text_view);
+      priv->blink_time += get_cursor_time (text_view);
+    }
 
   /* Block changed_handler while changing the layout's cursor visibility
    * because it would expose the whole paragraph. Instead, we expose
@@ -4506,8 +4938,6 @@ blink_cb (gpointer data)
 
   text_window_invalidate_cursors (text_view->text_window);
 
-  GDK_THREADS_LEAVE ();
-
   /* Remove ourselves */
   return FALSE;
 }
@@ -4528,7 +4958,7 @@ gtk_text_view_check_cursor_blink (GtkTextView *text_view)
 {
   if (text_view->layout != NULL &&
       text_view->cursor_visible &&
-      GTK_WIDGET_HAS_FOCUS (text_view))
+      gtk_widget_has_focus (GTK_WIDGET (text_view)))
     {
       if (cursor_blinks (text_view))
        {
@@ -4536,7 +4966,7 @@ gtk_text_view_check_cursor_blink (GtkTextView *text_view)
            {
              gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
              
-             text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
+             text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
                                                        blink_cb,
                                                        text_view);
            }
@@ -4559,18 +4989,28 @@ gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
 {
   if (text_view->layout != NULL &&
       text_view->cursor_visible &&
-      GTK_WIDGET_HAS_FOCUS (text_view) &&
+      gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
       cursor_blinks (text_view))
     {
       gtk_text_view_stop_cursor_blink (text_view);
       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
       
-      text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
+      text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
                                                blink_cb,
                                                text_view);
     }
 }
 
+static void
+gtk_text_view_reset_blink_time (GtkTextView *text_view)
+{
+  GtkTextViewPrivate *priv;
+
+  priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
+
+  priv->blink_time = 0;
+}
+
 
 /*
  * Key binding handlers
@@ -4621,8 +5061,9 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
 {
   GtkTextIter insert;
   GtkTextIter newplace;
-
+  gboolean cancel_selection = FALSE;
   gint cursor_x_pos = 0;
+  GtkDirectionType leave_direction = -1;
 
   if (!text_view->cursor_visible) 
     {
@@ -4630,8 +5071,10 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
 
       switch (step) 
        {
-       case GTK_MOVEMENT_LOGICAL_POSITIONS:
         case GTK_MOVEMENT_VISUAL_POSITIONS:
+          leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
+          /* fall through */
+        case GTK_MOVEMENT_LOGICAL_POSITIONS:
         case GTK_MOVEMENT_WORDS:
          scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
          break;
@@ -4639,6 +5082,8 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
          scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
          break;          
         case GTK_MOVEMENT_DISPLAY_LINES:
+          leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
+          /* fall through */
         case GTK_MOVEMENT_PARAGRAPHS:
         case GTK_MOVEMENT_PARAGRAPH_ENDS:
          scroll_step = GTK_SCROLL_STEPS;
@@ -4656,8 +5101,16 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
           scroll_step = GTK_SCROLL_PAGES;
           break;
        }
-      
-      gtk_text_view_move_viewport (text_view, scroll_step, count);
+
+      if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
+        {
+          if (leave_direction != -1 &&
+              !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
+                                         leave_direction))
+            {
+              g_signal_emit_by_name (text_view, "move-focus", leave_direction);
+            }
+        }
 
       return;
     }
@@ -4666,36 +5119,65 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
 
   if (step == GTK_MOVEMENT_PAGES)
     {
-      gtk_text_view_scroll_pages (text_view, count, extend_selection);
+      if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
+        gtk_widget_error_bell (GTK_WIDGET (text_view));
+
       gtk_text_view_check_cursor_blink (text_view);
       gtk_text_view_pend_cursor_blink (text_view);
       return;
     }
   else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
     {
-      gtk_text_view_scroll_hpages (text_view, count, extend_selection);
+      if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
+        gtk_widget_error_bell (GTK_WIDGET (text_view));
+
       gtk_text_view_check_cursor_blink (text_view);
       gtk_text_view_pend_cursor_blink (text_view);
       return;
     }
 
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
+
+  if (! extend_selection)
+    {
+      GtkTextIter sel_bound;
+
+      gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
+                                        gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
+
+      /* if we move forward, assume the cursor is at the end of the selection;
+       * if we move backward, assume the cursor is at the start
+       */
+      if (count > 0)
+        gtk_text_iter_order (&sel_bound, &insert);
+      else
+        gtk_text_iter_order (&insert, &sel_bound);
+
+      /* if we actually have a selection, just move *to* the beginning/end
+       * of the selection and not *from* there on LOGICAL_POSITIONS
+       * and VISUAL_POSITIONS movement
+       */
+      if (! gtk_text_iter_equal (&sel_bound, &insert))
+        cancel_selection = TRUE;
+    }
+
   newplace = insert;
 
   if (step == GTK_MOVEMENT_DISPLAY_LINES)
-    gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
+    gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
 
   switch (step)
     {
     case GTK_MOVEMENT_LOGICAL_POSITIONS:
-      gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
+      if (! cancel_selection)
+        gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
       break;
 
     case GTK_MOVEMENT_VISUAL_POSITIONS:
-      gtk_text_layout_move_iter_visually (text_view->layout,
-                                          &newplace, count);
+      if (! cancel_selection)
+        gtk_text_layout_move_iter_visually (text_view->layout,
+                                            &newplace, count);
       break;
 
     case GTK_MOVEMENT_WORDS:
@@ -4710,19 +5192,23 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
 
     case GTK_MOVEMENT_DISPLAY_LINES:
       if (count < 0)
-      {
-        if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
-          gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
-        else
-          gtk_text_iter_set_line_offset (&newplace, 0);
-      }
+        {
+          leave_direction = GTK_DIR_UP;
+
+          if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
+            gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
+          else
+            gtk_text_iter_set_line_offset (&newplace, 0);
+        }
       if (count > 0)
-      {
-        if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
-          gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
-        else
-          gtk_text_iter_forward_to_line_end (&newplace);
-      }
+        {
+          leave_direction = GTK_DIR_DOWN;
+
+          if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
+            gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
+          else
+            gtk_text_iter_forward_to_line_end (&newplace);
+        }
       break;
 
     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
@@ -4787,12 +5273,23 @@ gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
     {
       DV(g_print (G_STRLOC": scrolling onscreen\n"));
       gtk_text_view_scroll_mark_onscreen (text_view,
-                                          gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                    "insert"));
+                                          gtk_text_buffer_get_insert (get_buffer (text_view)));
 
       if (step == GTK_MOVEMENT_DISPLAY_LINES)
         gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
     }
+  else if (leave_direction != -1)
+    {
+      if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
+                                     leave_direction))
+        {
+          g_signal_emit_by_name (text_view, "move-focus", leave_direction);
+        }
+    }
+  else if (! cancel_selection)
+    {
+      gtk_widget_error_bell (GTK_WIDGET (text_view));
+    }
 
   gtk_text_view_check_cursor_blink (text_view);
   gtk_text_view_pend_cursor_blink (text_view);
@@ -4817,7 +5314,7 @@ gtk_text_view_page_horizontally (GtkTextView     *text_view,
 }
 
 
-static void
+static gboolean
 gtk_text_view_move_viewport (GtkTextView     *text_view,
                              GtkScrollStep    step,
                              gint             count)
@@ -4861,7 +5358,7 @@ gtk_text_view_move_viewport (GtkTextView     *text_view,
       break;
     }
 
-  set_adjustment_clamped (adjustment, adjustment->value + count * increment);
+  return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
 }
 
 static void
@@ -4870,13 +5367,12 @@ gtk_text_view_set_anchor (GtkTextView *text_view)
   GtkTextIter insert;
 
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
 }
 
-static void
+static gboolean
 gtk_text_view_scroll_pages (GtkTextView *text_view,
                             gint         count,
                             gboolean     extend_selection)
@@ -4885,14 +5381,18 @@ gtk_text_view_scroll_pages (GtkTextView *text_view,
   gdouble oldval;
   GtkAdjustment *adj;
   gint cursor_x_pos, cursor_y_pos;
+  GtkTextMark *insert_mark;
+  GtkTextIter old_insert;
   GtkTextIter new_insert;
   GtkTextIter anchor;
   gint y0, y1;
 
-  g_return_if_fail (text_view->vadjustment != NULL);
+  g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
   
   adj = text_view->vadjustment;
 
+  insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
+
   /* Make sure we start from the current cursor position, even
    * if it was offscreen, but don't queue more scrolls if we're
    * already behind.
@@ -4900,12 +5400,13 @@ gtk_text_view_scroll_pages (GtkTextView *text_view,
   if (text_view->pending_scroll)
     cancel_pending_scroll (text_view);
   else
-    gtk_text_view_scroll_mark_onscreen (text_view,
-                                       gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                 "insert"));
-  
-/* Validate the region that will be brought into view by the cursor motion
+    gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
+
+  /* Validate the region that will be brought into view by the cursor motion
    */
+  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
+                                    &old_insert, insert_mark);
+
   if (count < 0)
     {
       gtk_text_view_get_first_para_iter (text_view, &anchor);
@@ -4922,6 +5423,8 @@ gtk_text_view_scroll_pages (GtkTextView *text_view,
   gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
   /* FIXME do we need to update the adjustment ranges here? */
 
+  new_insert = old_insert;
+
   if (count < 0 && adj->value <= (adj->lower + 1e-12))
     {
       /* already at top, just be sure we are at offset 0 */
@@ -4936,11 +5439,11 @@ gtk_text_view_scroll_pages (GtkTextView *text_view,
     }
   else
     {
-      gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
+      gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
 
-      newval = adj->value;
       oldval = adj->value;
-  
+      newval = adj->value;
+
       newval += count * adj->page_increment;
 
       set_adjustment_clamped (adj, newval);
@@ -4957,12 +5460,12 @@ gtk_text_view_scroll_pages (GtkTextView *text_view,
    * only guarantees 1 pixel onscreen.
    */
   DV(g_print (G_STRLOC": scrolling onscreen\n"));
-  gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+  gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
+
+  return !gtk_text_iter_equal (&old_insert, &new_insert);
 }
 
-static void
+static gboolean
 gtk_text_view_scroll_hpages (GtkTextView *text_view,
                              gint         count,
                              gboolean     extend_selection)
@@ -4971,13 +5474,17 @@ gtk_text_view_scroll_hpages (GtkTextView *text_view,
   gdouble oldval;
   GtkAdjustment *adj;
   gint cursor_x_pos, cursor_y_pos;
+  GtkTextMark *insert_mark;
+  GtkTextIter old_insert;
   GtkTextIter new_insert;
   gint y, height;
   
-  g_return_if_fail (text_view->hadjustment != NULL);
+  g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
 
   adj = text_view->hadjustment;
 
+  insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
+
   /* Make sure we start from the current cursor position, even
    * if it was offscreen, but don't queue more scrolls if we're
    * already behind.
@@ -4985,19 +5492,19 @@ gtk_text_view_scroll_hpages (GtkTextView *text_view,
   if (text_view->pending_scroll)
     cancel_pending_scroll (text_view);
   else
-    gtk_text_view_scroll_mark_onscreen (text_view,
-                                       gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                 "insert"));
-  
+    gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
+
   /* Validate the line that we're moving within.
    */
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
-                                    &new_insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
-  gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
-  gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
+                                    &old_insert, insert_mark);
+
+  gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
+  gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
   /* FIXME do we need to update the adjustment ranges here? */
-  
+
+  new_insert = old_insert;
+
   if (count < 0 && adj->value <= (adj->lower + 1e-12))
     {
       /* already at far left, just be sure we are at offset 0 */
@@ -5013,11 +5520,11 @@ gtk_text_view_scroll_hpages (GtkTextView *text_view,
     }
   else
     {
-      gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
+      gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
 
-      newval = adj->value;
       oldval = adj->value;
-  
+      newval = adj->value;
+
       newval += count * adj->page_increment;
 
       set_adjustment_clamped (adj, newval);
@@ -5040,9 +5547,9 @@ gtk_text_view_scroll_hpages (GtkTextView *text_view,
    * only guarantees 1 pixel onscreen.
    */
   DV(g_print (G_STRLOC": scrolling onscreen\n"));
-  gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+  gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
+
+  return !gtk_text_iter_equal (&old_insert, &new_insert);
 }
 
 static gboolean
@@ -5076,8 +5583,11 @@ static void
 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
                                 const gchar *str)
 {
-  gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
-                                                text_view->editable);
+  if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
+                                                     text_view->editable))
+    {
+      gtk_widget_error_bell (GTK_WIDGET (text_view));
+    }
 }
 
 static void
@@ -5100,10 +5610,8 @@ gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
         return;
     }
 
-  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
-                                    &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   start = insert;
   end = insert;
@@ -5131,26 +5639,42 @@ gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
       break;
 
     case GTK_DELETE_PARAGRAPH_ENDS:
-      /* If we're already at a newline, we need to
-       * simply delete that newline, instead of
-       * moving to the next one.
-       */
-      if (gtk_text_iter_ends_line (&end))
+      if (count > 0)
         {
-          gtk_text_iter_forward_line (&end);
-          --count;
-        }
+          /* If we're already at a newline, we need to
+           * simply delete that newline, instead of
+           * moving to the next one.
+           */
+          if (gtk_text_iter_ends_line (&end))
+            {
+              gtk_text_iter_forward_line (&end);
+              --count;
+            }
 
-      while (count > 0)
-        {
-          if (!gtk_text_iter_forward_to_line_end (&end))
-            break;
+          while (count > 0)
+            {
+              if (!gtk_text_iter_forward_to_line_end (&end))
+                break;
 
-          --count;
+              --count;
+            }
         }
+      else if (count < 0)
+        {
+          if (gtk_text_iter_starts_line (&start))
+            {
+              gtk_text_iter_backward_line (&start);
+              if (!gtk_text_iter_ends_line (&end))
+                gtk_text_iter_forward_to_line_end (&start);
+            }
+          else
+            {
+              gtk_text_iter_set_line_offset (&start, 0);
+            }
+          ++count;
 
-      /* FIXME figure out what a negative count means
-         and support that */
+          gtk_text_iter_backward_lines (&start, -count);
+        }
       break;
 
     case GTK_DELETE_PARAGRAPHS:
@@ -5194,13 +5718,21 @@ gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
                                                           " ", 1,
                                                           text_view->editable);
         }
+      else
+        {
+          gtk_widget_error_bell (GTK_WIDGET (text_view));
+        }
 
       gtk_text_buffer_end_user_action (get_buffer (text_view));
       gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
 
       DV(g_print (G_STRLOC": scrolling onscreen\n"));
       gtk_text_view_scroll_mark_onscreen (text_view,
-                                          gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
+                                          gtk_text_buffer_get_insert (get_buffer (text_view)));
+    }
+  else
+    {
+      gtk_widget_error_bell (GTK_WIDGET (text_view));
     }
 }
 
@@ -5218,8 +5750,7 @@ gtk_text_view_backspace (GtkTextView *text_view)
 
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
                                     &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
                                 TRUE, text_view->editable))
@@ -5227,7 +5758,11 @@ gtk_text_view_backspace (GtkTextView *text_view)
       gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
       DV(g_print (G_STRLOC": scrolling onscreen\n"));
       gtk_text_view_scroll_mark_onscreen (text_view,
-                                         gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
+                                          gtk_text_buffer_get_insert (get_buffer (text_view)));
+    }
+  else
+    {
+      gtk_widget_error_bell (GTK_WIDGET (text_view));
     }
 }
 
@@ -5242,8 +5777,7 @@ gtk_text_view_cut_clipboard (GtkTextView *text_view)
                                 text_view->editable);
   DV(g_print (G_STRLOC": scrolling onscreen\n"));
   gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+                                      gtk_text_buffer_get_insert (get_buffer (text_view)));
 }
 
 static void
@@ -5254,10 +5788,8 @@ gtk_text_view_copy_clipboard (GtkTextView *text_view)
   
   gtk_text_buffer_copy_clipboard (get_buffer (text_view),
                                  clipboard);
-  DV(g_print (G_STRLOC": scrolling onscreen\n"));
-  gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+
+  /* on copy do not scroll, we are already onscreen */
 }
 
 static void
@@ -5270,16 +5802,35 @@ gtk_text_view_paste_clipboard (GtkTextView *text_view)
                                   clipboard,
                                   NULL,
                                   text_view->editable);
+}
+
+static void
+gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
+                                  GtkClipboard  *clipboard,
+                                  gpointer       data)
+{
+  GtkTextView *text_view = data;
   DV(g_print (G_STRLOC": scrolling onscreen\n"));
-  gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+  gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
 }
 
 static void
 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
 {
+  if (text_view->text_window)
+    text_window_invalidate_cursors (text_view->text_window);
+
   text_view->overwrite_mode = !text_view->overwrite_mode;
+
+  if (text_view->layout)
+    gtk_text_layout_set_overwrite_mode (text_view->layout,
+                                       text_view->overwrite_mode && text_view->editable);
+
+  if (text_view->text_window)
+    text_window_invalidate_cursors (text_view->text_window);
+
+  gtk_text_view_pend_cursor_blink (text_view);
+
   g_object_notify (G_OBJECT (text_view), "overwrite");
 }
 
@@ -5318,21 +5869,20 @@ gtk_text_view_set_overwrite (GtkTextView *text_view,
   overwrite = overwrite != FALSE;
 
   if (text_view->overwrite_mode != overwrite)
-    {
-      text_view->overwrite_mode = overwrite;
-
-      g_object_notify (G_OBJECT (text_view), "overwrite");
-    }
+    gtk_text_view_toggle_overwrite (text_view);
 }
 
 /**
  * gtk_text_view_set_accepts_tab:
  * @text_view: A #GtkTextView
- * @accepts_tab: %TRUE if pressing the Tab key should insert a tab character, %FALSE, if pressing the Tab key should move the keyboard focus.
+ * @accepts_tab: %TRUE if pressing the Tab key should insert a tab 
+ *    character, %FALSE, if pressing the Tab key should move the 
+ *    keyboard focus.
  * 
- * Sets the behavior of the text widget when the Tab key is pressed. If @accepts_tab
- * is %TRUE a tab character is inserted. If @accepts_tab is %FALSE the keyboard focus
- * is moved to the next widget in the focus chain.
+ * Sets the behavior of the text widget when the Tab key is pressed. 
+ * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab 
+ * is %FALSE the keyboard focus is moved to the next widget in the focus 
+ * chain.
  * 
  * Since: 2.4
  **/
@@ -5359,7 +5909,8 @@ gtk_text_view_set_accepts_tab (GtkTextView *text_view,
  * Returns whether pressing the Tab key inserts a tab characters.
  * gtk_text_view_set_accepts_tab().
  * 
- * Return value: %TRUE if pressing the Tab key inserts a tab character, %FALSE if pressing the Tab key moves the keyboard focus.
+ * Return value: %TRUE if pressing the Tab key inserts a tab character, 
+ *   %FALSE if pressing the Tab key moves the keyboard focus.
  * 
  * Since: 2.4
  **/
@@ -5372,16 +5923,33 @@ gtk_text_view_get_accepts_tab (GtkTextView *text_view)
 }
 
 static void
-gtk_text_view_move_focus (GtkTextView     *text_view,
-                          GtkDirectionType direction_type)
+gtk_text_view_compat_move_focus (GtkTextView     *text_view,
+                                 GtkDirectionType direction_type)
 {
-  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
+  GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
 
-  if (!GTK_WIDGET_TOPLEVEL (toplevel))
-    return;
+  /*  as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
+   *  the evil code below makes sure that both emitting the signal and
+   *  calling the virtual function directly continue to work as expetcted
+   */
+
+  if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
+    {
+      /*  if this is a signal emission, chain up  */
+
+      gboolean retval;
 
-  /* Propagate to toplevel */
-  g_signal_emit_by_name (toplevel, "move_focus", direction_type);
+      g_signal_chain_from_overridden_handler (text_view,
+                                              direction_type, &retval);
+    }
+  else
+    {
+      /*  otherwise emit the signal, since somebody called the virtual
+       *  function directly
+       */
+
+      g_signal_emit_by_name (text_view, "move-focus", direction_type);
+    }
 }
 
 /*
@@ -5393,31 +5961,35 @@ gtk_text_view_unselect (GtkTextView *text_view)
 {
   GtkTextIter insert;
 
-  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
-                                    &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   gtk_text_buffer_move_mark (get_buffer (text_view),
-                             gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                       "selection_bound"),
+                             gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
                              &insert);
 }
 
 static void
 get_iter_at_pointer (GtkTextView *text_view,
-                     GtkTextIter *iter)
+                     GtkTextIter *iter,
+                    gint        *x,
+                    gint        *y)
 {
-  gint x, y;
+  gint xcoord, ycoord;
   GdkModifierType state;
 
   gdk_window_get_pointer (text_view->text_window->bin_window,
-                          &x, &y, &state);
+                          &xcoord, &ycoord, &state);
   
   gtk_text_layout_get_iter_at_pixel (text_view->layout,
                                      iter,
-                                     x + text_view->xoffset,
-                                     y + text_view->yoffset);
+                                     xcoord + text_view->xoffset,
+                                     ycoord + text_view->yoffset);
+  if (x)
+    *x = xcoord;
+
+  if (y)
+    *y = ycoord;
 }
 
 static void
@@ -5427,7 +5999,7 @@ move_mark_to_pointer_and_scroll (GtkTextView *text_view,
   GtkTextIter newplace;
   GtkTextMark *mark;
 
-  get_iter_at_pointer (text_view, &newplace);
+  get_iter_at_pointer (text_view, &newplace, NULL, NULL);
   
   mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
   
@@ -5445,50 +6017,70 @@ move_mark_to_pointer_and_scroll (GtkTextView *text_view,
                G_STRLOC, text_view->first_validate_idle));
 }
 
-static gint
+static gboolean
 selection_scan_timeout (gpointer data)
 {
   GtkTextView *text_view;
 
-  GDK_THREADS_ENTER ();
-  
   text_view = GTK_TEXT_VIEW (data);
 
   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
   gtk_text_view_scroll_mark_onscreen (text_view, 
-                                     gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                               "insert"));
+                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
 
-  GDK_THREADS_LEAVE ();
-  
   return TRUE; /* remain installed. */
 }
 
-#define DND_SCROLL_MARGIN 0.20
+#define UPPER_OFFSET_ANCHOR 0.8
+#define LOWER_OFFSET_ANCHOR 0.2
+
+static gboolean
+check_scroll (gdouble offset, GtkAdjustment *adj)
+{
+  if ((offset > UPPER_OFFSET_ANCHOR &&
+       adj->value + adj->page_size < adj->upper) ||
+      (offset < LOWER_OFFSET_ANCHOR &&
+       adj->value > adj->lower))
+    return TRUE;
+
+  return FALSE;
+}
 
 static gint
 drag_scan_timeout (gpointer data)
 {
   GtkTextView *text_view;
   GtkTextIter newplace;
+  gint x, y, width, height;
+  gdouble pointer_xoffset, pointer_yoffset;
 
-  GDK_THREADS_ENTER ();
-  
   text_view = GTK_TEXT_VIEW (data);
 
-  get_iter_at_pointer (text_view, &newplace);
+  get_iter_at_pointer (text_view, &newplace, &x, &y);
+  gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
 
   gtk_text_buffer_move_mark (get_buffer (text_view),
                              text_view->dnd_mark,
                              &newplace);
 
-  DV(g_print (G_STRLOC": scrolling onscreen\n"));
-  gtk_text_view_scroll_to_mark (text_view,
-                                text_view->dnd_mark,
-                                DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
+  pointer_xoffset = (gdouble) x / width;
+  pointer_yoffset = (gdouble) y / height;
+
+  if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
+      check_scroll (pointer_yoffset, text_view->vadjustment))
+    {
+      /* do not make offsets surpass lower nor upper anchors, this makes
+       * scrolling speed relative to the distance of the pointer to the
+       * anchors when it moves beyond them.
+       */
+      pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
+      pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
+
+      gtk_text_view_scroll_to_mark (text_view,
+                                    text_view->dnd_mark,
+                                    0., TRUE, pointer_xoffset, pointer_yoffset);
+    }
 
-  GDK_THREADS_LEAVE ();
-  
   return TRUE;
 }
 
@@ -5601,6 +6193,8 @@ selection_motion_event_handler (GtkTextView    *text_view,
                                GdkEventMotion *event, 
                                SelectionData  *data)
 {
+  gdk_event_request_motions (event);
+
   if (data->granularity == SELECT_CHARACTERS) 
     {
       move_mark_to_pointer_and_scroll (text_view, "insert");
@@ -5616,7 +6210,7 @@ selection_motion_event_handler (GtkTextView    *text_view,
       gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
       gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
 
-      get_iter_at_pointer (text_view, &cursor);
+      get_iter_at_pointer (text_view, &cursor, NULL, NULL);
       
       start = cursor;
       extend_selection (text_view, data->granularity, &start, &end);
@@ -5640,7 +6234,7 @@ selection_motion_event_handler (GtkTextView    *text_view,
     g_source_remove (text_view->scroll_timeout);
   
   text_view->scroll_timeout =
-    g_timeout_add (50, selection_scan_timeout, text_view);
+    gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
 
   return TRUE;
 }
@@ -5655,7 +6249,8 @@ gtk_text_view_start_selection_drag (GtkTextView       *text_view,
   GtkTextBuffer *buffer;
   SelectionData *data;
 
-  g_assert (text_view->selection_drag_handler == 0);
+  if (text_view->selection_drag_handler != 0)
+    return;
   
   data = g_new0 (SelectionData, 1);
 
@@ -5720,7 +6315,7 @@ gtk_text_view_start_selection_drag (GtkTextView       *text_view,
   gtk_text_view_check_cursor_blink (text_view);
 
   text_view->selection_drag_handler = g_signal_connect_data (text_view,
-                                                             "motion_notify_event",
+                                                             "motion-notify-event",
                                                              G_CALLBACK (selection_motion_event_handler),
                                                              data,
                                                              (GClosureNotify) selection_data_free, 0);  
@@ -5728,8 +6323,7 @@ gtk_text_view_start_selection_drag (GtkTextView       *text_view,
 
 /* returns whether we were really dragging */
 static gboolean
-gtk_text_view_end_selection_drag (GtkTextView    *text_view, 
-                                 GdkEventButton *event)
+gtk_text_view_end_selection_drag (GtkTextView    *text_view) 
 {
   if (text_view->selection_drag_handler == 0)
     return FALSE;
@@ -5824,18 +6418,21 @@ gtk_text_view_ensure_layout (GtkTextView *text_view)
                        text_view);
 
       g_signal_connect (text_view->layout,
-                       "allocate_child",
+                       "allocate-child",
                        G_CALLBACK (gtk_text_view_child_allocated),
                        text_view);
       
       if (get_buffer (text_view))
         gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
 
-      if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
+      if ((gtk_widget_has_focus (widget) && text_view->cursor_visible))
         gtk_text_view_pend_cursor_blink (text_view);
       else
         gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
 
+      gtk_text_layout_set_overwrite_mode (text_view->layout,
+                                         text_view->overwrite_mode && text_view->editable);
+
       ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
       rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
@@ -5929,9 +6526,9 @@ gtk_text_view_destroy_layout (GtkTextView *text_view)
                                            invalidated_handler,
                                            text_view);
       g_signal_handlers_disconnect_by_func (text_view->layout,
-                                           changed_handler, 
+                                           changed_handler,
                                            text_view);
-      
+
       /* Remove layout from all anchored children */
       tmp_list = text_view->children;
       while (tmp_list != NULL)
@@ -5946,9 +6543,9 @@ gtk_text_view_destroy_layout (GtkTextView *text_view)
 
           tmp_list = g_slist_next (tmp_list);
         }
-      
+
       gtk_text_view_stop_cursor_blink (text_view);
-      gtk_text_view_end_selection_drag (text_view, NULL);
+      gtk_text_view_end_selection_drag (text_view);
 
       g_object_unref (text_view->layout);
       text_view->layout = NULL;
@@ -6208,20 +6805,9 @@ gtk_text_view_drag_motion (GtkWidget        *widget,
       gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
     }
       
-  gtk_text_buffer_move_mark (get_buffer (text_view),
-                             text_view->dnd_mark,
-                             &newplace);
-
-  DV(g_print (G_STRLOC": scrolling to mark\n"));
-  gtk_text_view_scroll_to_mark (text_view,
-                                text_view->dnd_mark,
-                                DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
-  
-  if (text_view->scroll_timeout != 0) /* reset on every motion event */
-    g_source_remove (text_view->scroll_timeout);
-      
-  text_view->scroll_timeout =
-    g_timeout_add (50, drag_scan_timeout, text_view);
+  if (!text_view->scroll_timeout)
+    text_view->scroll_timeout =
+      gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
 
   /* TRUE return means don't propagate the drag motion to parent
    * widgets that may also be drop sites.
@@ -6275,9 +6861,13 @@ insert_text_data (GtkTextView      *text_view,
 
   if (str)
     {
-      gtk_text_buffer_insert_interactive (get_buffer (text_view),
-                                          drop_point, (gchar *) str, -1,
-                                          text_view->editable);
+      if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
+                                               drop_point, (gchar *) str, -1,
+                                               text_view->editable))
+        {
+          gtk_widget_error_bell (GTK_WIDGET (text_view));
+        }
+
       g_free (str);
     }
 }
@@ -6484,7 +7074,7 @@ gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
       text_view->hadjustment = hadj;
       g_object_ref_sink (text_view->hadjustment);
       
-      g_signal_connect (text_view->hadjustment, "value_changed",
+      g_signal_connect (text_view->hadjustment, "value-changed",
                         G_CALLBACK (gtk_text_view_value_changed),
                        text_view);
       need_adjust = TRUE;
@@ -6495,7 +7085,7 @@ gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
       text_view->vadjustment = vadj;
       g_object_ref_sink (text_view->vadjustment);
       
-      g_signal_connect (text_view->vadjustment, "value_changed",
+      g_signal_connect (text_view->vadjustment, "value-changed",
                         G_CALLBACK (gtk_text_view_value_changed),
                        text_view);
       need_adjust = TRUE;
@@ -6518,7 +7108,7 @@ typedef struct
 
 /* The window to which widget->window is relative */
 #define ALLOCATION_WINDOW(widget)              \
-   (GTK_WIDGET_NO_WINDOW (widget) ?            \
+   (!gtk_widget_get_has_window (widget) ?              \
     (widget)->window :                          \
      gdk_window_get_parent ((widget)->window))
 
@@ -6534,7 +7124,7 @@ adjust_allocation_recurse (GtkWidget *widget,
    */
   if (!GTK_WIDGET_REALIZED (widget))
     {
-      if (GTK_WIDGET_VISIBLE (widget))
+      if (gtk_widget_get_visible (widget))
        {
          GdkRectangle tmp_rectangle = widget->allocation;
          tmp_rectangle.x += scroll_data->dx;
@@ -6679,6 +7269,11 @@ gtk_text_view_value_changed (GtkAdjustment *adj,
    */
   gtk_text_view_update_layout_width (text_view);
   
+  /* We also update the IM spot location here, since the IM context
+   * might do something that leads to validation.
+   */
+  gtk_text_view_update_im_spot_location (text_view);
+
   /* note that validation of onscreen could invoke this function
    * recursively, by scrolling to maintain first_para, or in response
    * to updating the layout width, however there is no problem with
@@ -6713,6 +7308,9 @@ gtk_text_view_value_changed (GtkAdjustment *adj,
       text_view->first_validate_idle = 0;
     }
 
+  /* Finally we update the IM cursor location again, to ensure any
+   * changes made by the validation are pushed through.
+   */
   gtk_text_view_update_im_spot_location (text_view);
   
   DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
@@ -6742,24 +7340,30 @@ gtk_text_view_commit_text (GtkTextView   *text_view,
 
   if (!strcmp (str, "\n"))
     {
-      gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
-                                                    text_view->editable);
+      if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
+                                                         text_view->editable))
+        {
+          gtk_widget_error_bell (GTK_WIDGET (text_view));
+        }
     }
   else
     {
       if (!had_selection && text_view->overwrite_mode)
        {
          GtkTextIter insert;
-         
+
          gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
                                            &insert,
-                                           gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                     "insert"));
+                                           gtk_text_buffer_get_insert (get_buffer (text_view)));
          if (!gtk_text_iter_ends_line (&insert))
            gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
        }
-      gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
-                                                    text_view->editable);
+
+      if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
+                                                         text_view->editable))
+        {
+          gtk_widget_error_bell (GTK_WIDGET (text_view));
+        }
     }
 
   gtk_text_buffer_end_user_action (get_buffer (text_view));
@@ -6767,8 +7371,7 @@ gtk_text_view_commit_text (GtkTextView   *text_view,
   gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
   DV(g_print (G_STRLOC": scrolling onscreen\n"));
   gtk_text_view_scroll_mark_onscreen (text_view,
-                                      gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                "insert"));
+                                      gtk_text_buffer_get_insert (get_buffer (text_view)));
 }
 
 static void
@@ -6778,16 +7381,34 @@ gtk_text_view_preedit_changed_handler (GtkIMContext *context,
   gchar *str;
   PangoAttrList *attrs;
   gint cursor_pos;
+  GtkTextIter iter;
 
+  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
+                                   gtk_text_buffer_get_insert (text_view->buffer));
+
+  /* Keypress events are passed to input method even if cursor position is
+   * not editable; so beep here if it's multi-key input sequence, input
+   * method will be reset in key-press-event handler.
+   */
   gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
-  gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
-  pango_attr_list_unref (attrs);
-  g_free (str);
 
-  if (GTK_WIDGET_HAS_FOCUS (text_view))
+  if (str && str[0] && !gtk_text_iter_can_insert (&iter, text_view->editable))
+    {
+      gtk_widget_error_bell (GTK_WIDGET (text_view));
+      goto out;
+    }
+
+  g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
+
+  if (text_view->layout)
+    gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
+  if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
     gtk_text_view_scroll_mark_onscreen (text_view,
-                                       gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                                 "insert"));
+                                       gtk_text_buffer_get_insert (get_buffer (text_view)));
+
+out:
+  pango_attr_list_unref (attrs);
+  g_free (str);
 }
 
 static gboolean
@@ -6799,7 +7420,7 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
   gint pos;
   gchar *text;
 
-  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
+  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
                                    gtk_text_buffer_get_insert (text_view->buffer));
   end = start;
 
@@ -6823,7 +7444,7 @@ gtk_text_view_delete_surrounding_handler (GtkIMContext  *context,
   GtkTextIter start;
   GtkTextIter end;
 
-  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
+  gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
                                    gtk_text_buffer_get_insert (text_view->buffer));
   end = start;
 
@@ -6883,16 +7504,13 @@ gtk_text_view_target_list_notify (GtkTextBuffer    *buffer,
   while (list)
     {
       GtkTargetPair *pair = list->data;
-      guint          info;
 
       list = g_list_next (list); /* get next element before removing */
 
-      for (info = GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS;
-           info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT;
-           info--)
+      if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
+          pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
         {
-          if (pair->info == info)
-            gtk_target_list_remove (view_list, pair->target);
+          gtk_target_list_remove (view_list, pair->target);
         }
     }
 
@@ -6914,22 +7532,29 @@ gtk_text_view_get_cursor_location  (GtkTextView   *text_view,
   GtkTextIter insert;
   
   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
-                                    gtk_text_buffer_get_mark (get_buffer (text_view),
-                                                              "insert"));
+                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
 
   gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
 }
 
 static void
 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
+                                      GtkTextIter *cursor,
                                       gint        *x,
                                       gint        *y)
 {
+  GtkTextIter insert;
   GdkRectangle pos;
 
+  if (cursor)
+    insert = *cursor;
+  else
+    gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
+                                      gtk_text_buffer_get_insert (get_buffer (text_view)));
+
   if ((x && text_view->virtual_cursor_x == -1) ||
       (y && text_view->virtual_cursor_y == -1))
-    gtk_text_view_get_cursor_location (text_view, &pos);
+    gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &pos, NULL);
 
   if (x)
     {
@@ -7188,13 +7813,13 @@ popup_targets_received (GtkClipboard     *clipboard,
       
       can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
       
-      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
+      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
                            have_selection &&
                             range_contains_editable_text (&sel_start, &sel_end,
                                                           text_view->editable));
-      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
+      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
                            have_selection);
-      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
+      append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
                            can_insert && clipboard_contains_text);
       
       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
@@ -7355,7 +7980,7 @@ text_window_free (GtkTextWindow *win)
 
 static void
 text_window_realize (GtkTextWindow *win,
-                     GdkWindow     *parent)
+                     GtkWidget     *widget)
 {
   GdkWindowAttr attributes;
   gint attributes_mask;
@@ -7373,7 +7998,7 @@ text_window_realize (GtkTextWindow *win,
 
   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
 
-  win->window = gdk_window_new (parent,
+  win->window = gdk_window_new (widget->window,
                                 &attributes,
                                 attributes_mask);
 
@@ -7405,23 +8030,26 @@ text_window_realize (GtkTextWindow *win,
 
   if (win->type == GTK_TEXT_WINDOW_TEXT)
     {
-      /* I-beam cursor */
-      cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
-                                          GDK_XTERM);
-      gdk_window_set_cursor (win->bin_window, cursor);
-      gdk_cursor_unref (cursor);
-
-      gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
+      if (gtk_widget_is_sensitive (widget))
+        {
+          /* I-beam cursor */
+          cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
+                                              GDK_XTERM);
+          gdk_window_set_cursor (win->bin_window, cursor);
+          gdk_cursor_unref (cursor);
+        } 
+
+      gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
                                         win->window);
 
 
       gdk_window_set_background (win->bin_window,
-                                 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
+                                 &widget->style->base[GTK_WIDGET_STATE (widget)]);
     }
   else
     {
       gdk_window_set_background (win->bin_window,
-                                 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
+                                 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
     }
 
   g_object_set_qdata (G_OBJECT (win->window),
@@ -7514,7 +8142,7 @@ text_window_invalidate_rect (GtkTextWindow *win,
       break;
 
     default:
-      g_warning ("%s: bug!", G_STRLOC);
+      g_warning ("%s: bug!", G_STRFUNC);
       return;
       break;
     }
@@ -7547,6 +8175,12 @@ text_window_invalidate_cursors (GtkTextWindow *win)
   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
                                     gtk_text_buffer_get_insert (text_view->buffer));
 
+  if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
+    {
+      text_window_invalidate_rect (win, &strong);
+      return;
+    }
+
   gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
                                         &strong, &weak);
 
@@ -7628,7 +8262,7 @@ text_window_get_height (GtkTextWindow *win)
  * height is 0, and are nonexistent before the widget has been
  * realized.
  *
- * Return value: a #GdkWindow, or %NULL
+ * Return value: (transfer none): a #GdkWindow, or %NULL
  **/
 GdkWindow*
 gtk_text_view_get_window (GtkTextView *text_view,
@@ -7675,12 +8309,12 @@ gtk_text_view_get_window (GtkTextView *text_view,
       break;
 
     case GTK_TEXT_WINDOW_PRIVATE:
-      g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
+      g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
       return NULL;
       break;
     }
 
-  g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
+  g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
   return NULL;
 }
 
@@ -7848,11 +8482,11 @@ gtk_text_view_buffer_to_window_coords (GtkTextView      *text_view,
       break;
 
     case GTK_TEXT_WINDOW_PRIVATE:
-      g_warning ("%s: can't get coords for private windows", G_STRLOC);
+      g_warning ("%s: can't get coords for private windows", G_STRFUNC);
       break;
 
     default:
-      g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
+      g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
       break;
     }
 }
@@ -7988,11 +8622,11 @@ gtk_text_view_window_to_buffer_coords (GtkTextView      *text_view,
       break;
 
     case GTK_TEXT_WINDOW_PRIVATE:
-      g_warning ("%s: can't get coords for private windows", G_STRLOC);
+      g_warning ("%s: can't get coords for private windows", G_STRFUNC);
       break;
 
     default:
-      g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
+      g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
       break;
     }
 }
@@ -8021,7 +8655,7 @@ set_window_width (GtkTextView      *text_view,
                                    width, 0);
           /* if the widget is already realized we need to realize the child manually */
           if (GTK_WIDGET_REALIZED (text_view))
-            text_window_realize (*winp, GTK_WIDGET (text_view)->window);
+            text_window_realize (*winp, GTK_WIDGET (text_view));
         }
       else
         {
@@ -8061,7 +8695,7 @@ set_window_height (GtkTextView      *text_view,
 
           /* if the widget is already realized we need to realize the child manually */
           if (GTK_WIDGET_REALIZED (text_view))
-            text_window_realize (*winp, GTK_WIDGET (text_view)->window);
+            text_window_realize (*winp, GTK_WIDGET (text_view));
         }
       else
         {
@@ -8287,7 +8921,6 @@ add_child (GtkTextView      *text_view,
  * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
  * 
  * Adds a child widget in the text buffer, at the given @anchor.
- * 
  **/
 void
 gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
@@ -8321,8 +8954,8 @@ gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
  * @ypos: Y position of child in window coordinates
  *
  * Adds a child at fixed coordinates in one of the text widget's
- * windows.  The window must have nonzero size (see
- * gtk_text_view_set_border_window_size()).  Note that the child
+ * windows. The window must have nonzero size (see
+ * gtk_text_view_set_border_window_size()). Note that the child
  * coordinates are given relative to the #GdkWindow in question, and
  * that these coordinates have no sane relationship to scrolling. When
  * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
@@ -8336,14 +8969,13 @@ gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
  * would be to update all child positions when the scroll adjustments
  * change or the text buffer changes. See bug 64518 on
  * bugzilla.gnome.org for status of fixing this issue.
- *
  **/
 void
-gtk_text_view_add_child_in_window (GtkTextView          *text_view,
-                                   GtkWidget            *child,
-                                   GtkTextWindowType     which_window,
-                                   gint                  xpos,
-                                   gint                  ypos)
+gtk_text_view_add_child_in_window (GtkTextView       *text_view,
+                                   GtkWidget         *child,
+                                   GtkTextWindowType  which_window,
+                                   gint               xpos,
+                                   gint               ypos)
 {
   GtkTextViewChild *vc;
 
@@ -8368,13 +9000,12 @@ gtk_text_view_add_child_in_window (GtkTextView          *text_view,
  * @ypos: new Y position in window coordinates
  *
  * Updates the position of a child, as for gtk_text_view_add_child_in_window().
- * 
  **/
 void
-gtk_text_view_move_child          (GtkTextView          *text_view,
-                                   GtkWidget            *child,
-                                   gint                  xpos,
-                                   gint                  ypos)
+gtk_text_view_move_child (GtkTextView *text_view,
+                          GtkWidget   *child,
+                          gint         xpos,
+                          gint         ypos)
 {
   GtkTextViewChild *vc;
 
@@ -8394,7 +9025,8 @@ gtk_text_view_move_child          (GtkTextView          *text_view,
   vc->x = xpos;
   vc->y = ypos;
 
-  if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
+  if (gtk_widget_get_visible (child) &&
+      gtk_widget_get_visible (GTK_WIDGET (text_view)))
     gtk_widget_queue_resize (child);
 }
 
@@ -8406,10 +9038,10 @@ gtk_text_view_move_child          (GtkTextView          *text_view,
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
  * 
- * Moves the given @iter forward by one display (wrapped) line.  A
- * display line is different from a paragraph. Paragraphs are
+ * Moves the given @iter forward by one display (wrapped) line.
+ * display line is different from a paragraph. Paragraphs are
  * separated by newlines or other paragraph separator characters.
- * Display lines are created by line-wrapping a paragraph.  If
+ * Display lines are created by line-wrapping a paragraph. If
  * wrapping is turned off, display lines and paragraphs will be the
  * same. Display lines are divided differently for each view, since
  * they depend on the view's width; paragraphs are the same in all
@@ -8434,10 +9066,10 @@ gtk_text_view_forward_display_line (GtkTextView *text_view,
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
  * 
- * Moves the given @iter backward by one display (wrapped) line.  A
- * display line is different from a paragraph. Paragraphs are
+ * Moves the given @iter backward by one display (wrapped) line.
+ * display line is different from a paragraph. Paragraphs are
  * separated by newlines or other paragraph separator characters.
- * Display lines are created by line-wrapping a paragraph.  If
+ * Display lines are created by line-wrapping a paragraph. If
  * wrapping is turned off, display lines and paragraphs will be the
  * same. Display lines are divided differently for each view, since
  * they depend on the view's width; paragraphs are the same in all
@@ -8462,10 +9094,10 @@ gtk_text_view_backward_display_line (GtkTextView *text_view,
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
  * 
- * Moves the given @iter forward to the next display line end.  A
- * display line is different from a paragraph. Paragraphs are
+ * Moves the given @iter forward to the next display line end.
+ * display line is different from a paragraph. Paragraphs are
  * separated by newlines or other paragraph separator characters.
- * Display lines are created by line-wrapping a paragraph.  If
+ * Display lines are created by line-wrapping a paragraph. If
  * wrapping is turned off, display lines and paragraphs will be the
  * same. Display lines are divided differently for each view, since
  * they depend on the view's width; paragraphs are the same in all
@@ -8490,10 +9122,10 @@ gtk_text_view_forward_display_line_end (GtkTextView *text_view,
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
  * 
- * Moves the given @iter backward to the next display line start.  A
- * display line is different from a paragraph. Paragraphs are
+ * Moves the given @iter backward to the next display line start.
+ * display line is different from a paragraph. Paragraphs are
  * separated by newlines or other paragraph separator characters.
- * Display lines are created by line-wrapping a paragraph.  If
+ * Display lines are created by line-wrapping a paragraph. If
  * wrapping is turned off, display lines and paragraphs will be the
  * same. Display lines are divided differently for each view, since
  * they depend on the view's width; paragraphs are the same in all
@@ -8540,7 +9172,8 @@ gtk_text_view_starts_display_line (GtkTextView       *text_view,
  * gtk_text_view_move_visually:
  * @text_view: a #GtkTextView
  * @iter: a #GtkTextIter
- * @count:   number of characters to move (negative moves left, positive moves right)
+ * @count: number of characters to move (negative moves left, 
+ *    positive moves right)
  *
  * Move the iterator a given number of characters visually, treating
  * it as the strong cursor position. If @count is positive, then the
@@ -8549,7 +9182,7 @@ gtk_text_view_starts_display_line (GtkTextView       *text_view,
  * cursor position will be @count positions to the left of the old
  * cursor position.
  *
- * In the presence of bidirection text, the correspondence
+ * In the presence of bi-directional text, the correspondence
  * between logical and visual order will depend on the direction
  * of the current run, and there may be jumps when the cursor
  * is moved off of the end of a run.