]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextlayout.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtktextlayout.c
index ddfcc7fad8cd23fac9391c87c949a56412d37bdf..73be6b417c94260929d831b5883ece633c28fae4 100644 (file)
@@ -23,8 +23,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
  *
  * Original Tk license:
  *
  */
 
 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
+#include "config.h"
 #include "gtkmarshalers.h"
 #include "gtktextlayout.h"
 #include "gtktextbtree.h"
 #include "gtktextiterprivate.h"
+#include "gtktextutil.h"
+#include "gtkintl.h"
 
 #include <stdlib.h>
 #include <string.h>
 
+#define GTK_TEXT_LAYOUT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
+
+typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
+
+struct _GtkTextLayoutPrivate
+{
+  /* Cache the line that the cursor is positioned on, as the keyboard
+     direction only influences the direction of the cursor line.
+  */
+  GtkTextLine *cursor_line;
+};
+
 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
                                                    GtkTextLine *line,
                                                    /* may be NULL */
@@ -95,17 +109,52 @@ static void gtk_text_layout_invalidated     (GtkTextLayout     *layout);
 static void gtk_text_layout_real_invalidate        (GtkTextLayout     *layout,
                                                    const GtkTextIter *start,
                                                    const GtkTextIter *end);
+static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout     *layout,
+                                                   const GtkTextIter *start,
+                                                   const GtkTextIter *end);
 static void gtk_text_layout_invalidate_cache       (GtkTextLayout     *layout,
-                                                   GtkTextLine       *line);
-static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout     *layout);
+                                                   GtkTextLine       *line,
+                                                   gboolean           cursors_only);
+static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout     *layout,
+                                                   gboolean           cursors_only);
 static void gtk_text_layout_real_free_line_data    (GtkTextLayout     *layout,
                                                    GtkTextLine       *line,
                                                    GtkTextLineData   *line_data);
+static void gtk_text_layout_emit_changed           (GtkTextLayout     *layout,
+                                                   gint               y,
+                                                   gint               old_height,
+                                                   gint               new_height);
 
 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
 
 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
 
+static void gtk_text_layout_mark_set_handler    (GtkTextBuffer     *buffer,
+                                                const GtkTextIter *location,
+                                                GtkTextMark       *mark,
+                                                gpointer           data);
+static void gtk_text_layout_buffer_insert_text  (GtkTextBuffer     *textbuffer,
+                                                GtkTextIter       *iter,
+                                                gchar             *str,
+                                                gint               len,
+                                                gpointer           data);
+static void gtk_text_layout_buffer_delete_range (GtkTextBuffer     *textbuffer,
+                                                GtkTextIter       *start,
+                                                GtkTextIter       *end,
+                                                gpointer           data);
+
+static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
+
+static void line_display_index_to_iter (GtkTextLayout      *layout,
+                                       GtkTextLineDisplay *display,
+                                       GtkTextIter        *iter,
+                                        gint                index,
+                                        gint                trailing);
+
+static gint line_display_iter_to_index (GtkTextLayout      *layout,
+                                        GtkTextLineDisplay *display,
+                                        const GtkTextIter  *iter);
+
 enum {
   INVALIDATED,
   CHANGED,
@@ -118,41 +167,58 @@ enum {
   LAST_ARG
 };
 
-static void gtk_text_layout_init       (GtkTextLayout      *text_layout);
-static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
-static void gtk_text_layout_finalize   (GObject            *object);
+#define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
 
-
-static GtkObjectClass *parent_class = NULL;
 static guint signals[LAST_SIGNAL] = { 0 };
 
 PangoAttrType gtk_text_attr_appearance_type = 0;
 
-GType
-gtk_text_layout_get_type (void)
+G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
+
+static void
+gtk_text_layout_dispose (GObject *object)
 {
-  static GType our_type = 0;
+  GtkTextLayout *layout;
+
+  layout = GTK_TEXT_LAYOUT (object);
 
-  if (our_type == 0)
+  gtk_text_layout_set_buffer (layout, NULL);
+
+  if (layout->default_style != NULL)
     {
-      static const GTypeInfo our_info =
-      {
-        sizeof (GtkTextLayoutClass),
-        (GBaseInitFunc) NULL,
-        (GBaseFinalizeFunc) NULL,
-        (GClassInitFunc) gtk_text_layout_class_init,
-        NULL,           /* class_finalize */
-        NULL,           /* class_data */
-        sizeof (GtkTextLayout),
-        0,              /* n_preallocs */
-        (GInstanceInitFunc) gtk_text_layout_init
-      };
+      gtk_text_attributes_unref (layout->default_style);
+      layout->default_style = NULL;
+    }
+
+  g_clear_object (&layout->ltr_context);
+  g_clear_object (&layout->rtl_context);
+
+  if (layout->one_display_cache)
+    {
+      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
+      layout->one_display_cache = NULL;
+      gtk_text_layout_free_line_display (layout, tmp_display);
+    }
 
-      our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
-                                         &our_info, 0);
+  if (layout->preedit_attrs != NULL)
+    {
+      pango_attr_list_unref (layout->preedit_attrs);
+      layout->preedit_attrs = NULL;
     }
 
-  return our_type;
+  G_OBJECT_CLASS (gtk_text_layout_parent_class)->dispose (object);
+}
+
+static void
+gtk_text_layout_finalize (GObject *object)
+{
+  GtkTextLayout *layout;
+
+  layout = GTK_TEXT_LAYOUT (object);
+
+  g_free (layout->preedit_string);
+
+  G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
 }
 
 static void
@@ -160,16 +226,16 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  parent_class = g_type_class_peek_parent (klass);
-  
+  object_class->dispose = gtk_text_layout_dispose;
   object_class->finalize = gtk_text_layout_finalize;
 
   klass->wrap = gtk_text_layout_real_wrap;
   klass->invalidate = gtk_text_layout_real_invalidate;
+  klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
   klass->free_line_data = gtk_text_layout_real_free_line_data;
 
   signals[INVALIDATED] =
-    g_signal_new ("invalidated",
+    g_signal_new (I_("invalidated"),
                   G_OBJECT_CLASS_TYPE (object_class),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
@@ -179,7 +245,7 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   0);
 
   signals[CHANGED] =
-    g_signal_new ("changed",
+    g_signal_new (I_("changed"),
                   G_OBJECT_CLASS_TYPE (object_class),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
@@ -190,9 +256,11 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   G_TYPE_INT,
                   G_TYPE_INT,
                   G_TYPE_INT);
+  g_signal_set_va_marshaller (signals[CHANGED], G_TYPE_FROM_CLASS (klass),
+                              _gtk_marshal_VOID__INT_INT_INTv);
 
   signals[ALLOCATE_CHILD] =
-    g_signal_new ("allocate_child",
+    g_signal_new (I_("allocate-child"),
                   G_OBJECT_CLASS_TYPE (object_class),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
@@ -200,9 +268,11 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   _gtk_marshal_VOID__OBJECT_INT_INT,
                   G_TYPE_NONE,
                   3,
-                  GTK_TYPE_OBJECT,
+                  G_TYPE_OBJECT,
                   G_TYPE_INT,
                   G_TYPE_INT);
+  
+  g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
 }
 
 static void
@@ -227,40 +297,10 @@ free_style_cache (GtkTextLayout *text_layout)
     }
 }
 
-static void
-gtk_text_layout_finalize (GObject *object)
-{
-  GtkTextLayout *layout;
-
-  layout = GTK_TEXT_LAYOUT (object);
-
-  gtk_text_layout_set_buffer (layout, NULL);
-
-  if (layout->default_style)
-    gtk_text_attributes_unref (layout->default_style);
-  layout->default_style = NULL;
-
-  if (layout->ltr_context)
-    {
-      g_object_unref (layout->ltr_context);
-      layout->ltr_context = NULL;
-    }
-  if (layout->rtl_context)
-    {
-      g_object_unref (layout->rtl_context);
-      layout->rtl_context = NULL;
-    }
-  
-  if (layout->one_display_cache) 
-    {
-      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
-      layout->one_display_cache = NULL;
-      gtk_text_layout_free_line_display (layout, tmp_display);
-    }
-
-  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
-}
-
+/**
+ * gtk_text_layout_set_buffer:
+ * @buffer: (allow-none):
+ */
 void
 gtk_text_layout_set_buffer (GtkTextLayout *layout,
                             GtkTextBuffer *buffer)
@@ -278,6 +318,16 @@ gtk_text_layout_set_buffer (GtkTextLayout *layout,
       _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
                                   layout);
 
+      g_signal_handlers_disconnect_by_func (layout->buffer, 
+                                            G_CALLBACK (gtk_text_layout_mark_set_handler), 
+                                            layout);
+      g_signal_handlers_disconnect_by_func (layout->buffer, 
+                                            G_CALLBACK (gtk_text_layout_buffer_insert_text), 
+                                            layout);
+      g_signal_handlers_disconnect_by_func (layout->buffer, 
+                                            G_CALLBACK (gtk_text_layout_buffer_delete_range), 
+                                            layout);
+
       g_object_unref (layout->buffer);
       layout->buffer = NULL;
     }
@@ -289,6 +339,16 @@ gtk_text_layout_set_buffer (GtkTextLayout *layout,
       g_object_ref (buffer);
 
       _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
+
+      /* Bind to all signals that move the insert mark. */
+      g_signal_connect_after (layout->buffer, "mark-set",
+                              G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
+      g_signal_connect_after (layout->buffer, "insert-text",
+                              G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
+      g_signal_connect_after (layout->buffer, "delete-range",
+                              G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
+
+      gtk_text_layout_update_cursor_line (layout);
     }
 }
 
@@ -328,22 +388,47 @@ gtk_text_layout_set_contexts (GtkTextLayout *layout,
 {
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
 
-  if (layout->ltr_context)
-    g_object_unref (ltr_context);
+  if (layout->ltr_context != ltr_context)
+    {
+      if (layout->ltr_context)
+       g_object_unref (layout->ltr_context);
 
-  layout->ltr_context = ltr_context;
-  g_object_ref (ltr_context);
+      layout->ltr_context = ltr_context;
+      g_object_ref (layout->ltr_context);
+    }
 
-  if (layout->rtl_context)
-    g_object_unref (rtl_context);
+  if (layout->rtl_context != rtl_context)
+    {
+      if (layout->rtl_context)
+       g_object_unref (layout->rtl_context);
 
-  layout->rtl_context = rtl_context;
-  g_object_ref (rtl_context);
+      layout->rtl_context = rtl_context;
+      g_object_ref (layout->rtl_context);
+    }
 
   DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
   gtk_text_layout_invalidate_all (layout);
 }
 
+/**
+ * gtk_text_layout_set_overwrite_mode:
+ * @layout: a #GtkTextLayout
+ * @overwrite: overwrite mode
+ *
+ * Sets overwrite mode
+ */
+void
+gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
+                                   gboolean       overwrite)
+{
+  overwrite = overwrite != 0;
+  if (overwrite != layout->overwrite_mode)
+    {
+      layout->overwrite_mode = overwrite;
+      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
+    }
+}
+
 /**
  * gtk_text_layout_set_cursor_direction:
  * @direction: the new direction(s) for which to draw cursors.
@@ -356,7 +441,7 @@ gtk_text_layout_set_contexts (GtkTextLayout *layout,
  * point at which new text is inserted depends on whether the new
  * text is right-to-left or left-to-right, so it may be desired to
  * make the drawn position of the cursor depend on the keyboard state.
- **/
+ */
 void
 gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
                                      GtkTextDirection direction)
@@ -364,7 +449,26 @@ gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
   if (direction != layout->cursor_direction)
     {
       layout->cursor_direction = direction;
-      gtk_text_layout_invalidate_cursor_line (layout);
+      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
+    }
+}
+
+/**
+ * gtk_text_layout_set_keyboard_direction:
+ * @keyboard_dir: the current direction of the keyboard.
+ *
+ * Sets the keyboard direction; this is used as for the bidirectional
+ * base direction for the line with the cursor if the line contains
+ * only neutral characters.
+ */
+void
+gtk_text_layout_set_keyboard_direction (GtkTextLayout   *layout,
+                                       GtkTextDirection keyboard_dir)
+{
+  if (keyboard_dir != layout->keyboard_direction)
+    {
+      layout->keyboard_direction = keyboard_dir;
+      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
     }
 }
 
@@ -376,7 +480,7 @@ gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
  * gtk_text_layout_set_buffer().
  *
  * Return value: the text buffer used by the layout.
- **/
+ */
 GtkTextBuffer *
 gtk_text_layout_get_buffer (GtkTextLayout *layout)
 {
@@ -410,7 +514,7 @@ gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
  * Sets whether the insertion cursor should be shown. Generally,
  * widgets using #GtkTextLayout will hide the cursor when the
  * widget does not have the input focus.
- **/
+ */
 void
 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
                                     gboolean       cursor_visible)
@@ -427,12 +531,12 @@ gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
       /* Now queue a redraw on the paragraph containing the cursor
        */
       gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
-                                        gtk_text_buffer_get_mark (layout->buffer, "insert"));
+                                        gtk_text_buffer_get_insert (layout->buffer));
 
       gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
-      gtk_text_layout_changed (layout, y, height, height);
+      gtk_text_layout_emit_changed (layout, y, height, height);
 
-      gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
+      gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
     }
 }
 
@@ -443,8 +547,8 @@ gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
  * Returns whether the insertion cursor will be shown.
  *
  * Return value: if %FALSE, the insertion cursor will not be
-    shown, even if the text is editable.
- **/
*     shown, even if the text is editable.
+ */
 gboolean
 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
 {
@@ -461,7 +565,7 @@ gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
  * Set the preedit string and attributes. The preedit string is a
  * string showing text that is currently being edited and not
  * yet committed into the buffer.
- **/
+ */
 void
 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
                                    const gchar   *preedit_string,
@@ -471,8 +575,7 @@ gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
   g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
 
-  if (layout->preedit_string)
-    g_free (layout->preedit_string);
+  g_free (layout->preedit_string);
 
   if (layout->preedit_attrs)
     pango_attr_list_unref (layout->preedit_attrs);
@@ -495,7 +598,7 @@ gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
       layout->preedit_cursor = 0;
     }
 
-  gtk_text_layout_invalidate_cursor_line (layout);
+  gtk_text_layout_invalidate_cursor_line (layout, FALSE);
 }
 
 void
@@ -518,13 +621,55 @@ gtk_text_layout_invalidated (GtkTextLayout *layout)
   g_signal_emit (layout, signals[INVALIDATED], 0);
 }
 
+static void
+gtk_text_layout_emit_changed (GtkTextLayout *layout,
+                             gint           y,
+                             gint           old_height,
+                             gint           new_height)
+{
+  g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
+}
+
+static void
+text_layout_changed (GtkTextLayout *layout,
+                     gint           y,
+                     gint           old_height,
+                     gint           new_height,
+                     gboolean       cursors_only)
+{
+  /* Check if the range intersects our cached line display,
+   * and invalidate the cached line if so.
+   */
+  if (layout->one_display_cache)
+    {
+      GtkTextLine *line = layout->one_display_cache->line;
+      gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
+                                                   line, layout);
+      gint cache_height = layout->one_display_cache->height;
+
+      if (cache_y + cache_height > y && cache_y < y + old_height)
+       gtk_text_layout_invalidate_cache (layout, line, cursors_only);
+    }
+
+  gtk_text_layout_emit_changed (layout, y, old_height, new_height);
+}
+
 void
 gtk_text_layout_changed (GtkTextLayout *layout,
                          gint           y,
                          gint           old_height,
                          gint           new_height)
 {
-  g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
+  text_layout_changed (layout, y, old_height, new_height, FALSE);
+}
+
+void
+gtk_text_layout_cursors_changed (GtkTextLayout *layout,
+                                 gint           y,
+                                gint           old_height,
+                                gint           new_height)
+{
+  text_layout_changed (layout, y, old_height, new_height, TRUE);
 }
 
 void
@@ -532,8 +677,7 @@ gtk_text_layout_free_line_data (GtkTextLayout     *layout,
                                 GtkTextLine       *line,
                                 GtkTextLineData   *line_data)
 {
-  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
-    (layout, line, line_data);
+  GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
 }
 
 void
@@ -541,8 +685,15 @@ gtk_text_layout_invalidate (GtkTextLayout *layout,
                             const GtkTextIter *start_index,
                             const GtkTextIter *end_index)
 {
-  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
-    (layout, start_index, end_index);
+  GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
+}
+
+void
+gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
+                                   const GtkTextIter *start_index,
+                                   const GtkTextIter *end_index)
+{
+  GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
 }
 
 GtkTextLineData*
@@ -551,9 +702,15 @@ gtk_text_layout_wrap (GtkTextLayout *layout,
                       /* may be NULL */
                       GtkTextLineData *line_data)
 {
-  return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
+  return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
 }
 
+
+/**
+ * gtk_text_layout_get_lines:
+ *
+ * Return value: (element-type GtkTextLine) (transfer container):
+ */
 GSList*
 gtk_text_layout_get_lines (GtkTextLayout *layout,
                            /* [top_y, bottom_y) */
@@ -660,38 +817,68 @@ gtk_text_layout_invalidate_all (GtkTextLayout *layout)
 
 static void
 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
-                                  GtkTextLine   *line)
+                                  GtkTextLine   *line,
+                                 gboolean       cursors_only)
 {
   if (layout->one_display_cache && line == layout->one_display_cache->line)
     {
-      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
-      layout->one_display_cache = NULL;
-      gtk_text_layout_free_line_display (layout, tmp_display);
+      GtkTextLineDisplay *display = layout->one_display_cache;
+
+      if (cursors_only)
+       {
+          if (display->cursors)
+            g_array_free (display->cursors, TRUE);
+         display->cursors = NULL;
+         display->cursors_invalid = TRUE;
+         display->has_block_cursor = FALSE;
+       }
+      else
+       {
+         layout->one_display_cache = NULL;
+         gtk_text_layout_free_line_display (layout, display);
+       }
     }
 }
 
 /* Now invalidate the paragraph containing the cursor
  */
 static void
-gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
+gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
+                                       gboolean cursors_only)
 {
-  GtkTextIter iter;
-  GtkTextLine *line;
+  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
   GtkTextLineData *line_data;
 
-  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
-                                   gtk_text_buffer_get_mark (layout->buffer, "insert"));
-  
-  line = _gtk_text_iter_get_text_line (&iter);
-  line_data = _gtk_text_line_get_data (line, layout);
+  if (priv->cursor_line == NULL)
+    return;
+
+  line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
   if (line_data)
     {
-      gtk_text_layout_invalidate_cache (layout, line);
-      _gtk_text_line_invalidate_wrap (line, line_data);
+      if (cursors_only)
+         gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
+      else
+       {
+         gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
+         _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
+       }
+
       gtk_text_layout_invalidated (layout);
     }
 }
 
+static void
+gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
+{
+  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
+  GtkTextIter iter;
+
+  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
+                                    gtk_text_buffer_get_insert (layout->buffer));
+
+  priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
+}
+
 static void
 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                  const GtkTextIter *start,
@@ -722,7 +909,7 @@ gtk_text_layout_real_invalidate (GtkTextLayout *layout,
     {
       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
 
-      gtk_text_layout_invalidate_cache (layout, line);
+      gtk_text_layout_invalidate_cache (layout, line, FALSE);
       
       if (line_data)
         _gtk_text_line_invalidate_wrap (line, line_data);
@@ -736,18 +923,52 @@ gtk_text_layout_real_invalidate (GtkTextLayout *layout,
   gtk_text_layout_invalidated (layout);
 }
 
+static void
+gtk_text_layout_real_invalidate_cursors (GtkTextLayout     *layout,
+                                        const GtkTextIter *start,
+                                        const GtkTextIter *end)
+{
+  /* Check if the range intersects our cached line display,
+   * and invalidate the cached line if so.
+   */
+  if (layout->one_display_cache)
+    {
+      GtkTextIter line_start, line_end;
+      GtkTextLine *line = layout->one_display_cache->line;
+
+      gtk_text_layout_get_iter_at_line (layout, &line_start, line, 0);
+
+      line_end = line_start;
+      if (!gtk_text_iter_ends_line (&line_end))
+       gtk_text_iter_forward_to_line_end (&line_end);
+
+      if (gtk_text_iter_compare (start, end) > 0)
+       {
+         const GtkTextIter *tmp = start;
+         start = end;
+         end = tmp;
+       }
+
+      if (gtk_text_iter_compare (&line_start, end) <= 0 &&
+         gtk_text_iter_compare (start, &line_end) <= 0)
+       {
+         gtk_text_layout_invalidate_cache (layout, line, TRUE);
+       }
+    }
+
+  gtk_text_layout_invalidated (layout);
+}
+
 static void
 gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
                                      GtkTextLine       *line,
                                      GtkTextLineData   *line_data)
 {
-  gtk_text_layout_invalidate_cache (layout, line);
+  gtk_text_layout_invalidate_cache (layout, line, FALSE);
 
   g_free (line_data);
 }
 
-
-
 /**
  * gtk_text_layout_is_valid:
  * @layout: a #GtkTextLayout
@@ -755,7 +976,7 @@ gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
  * Check if there are any invalid regions in a #GtkTextLayout's buffer
  *
  * Return value: %TRUE if any invalid regions were found
- **/
+ */
 gboolean
 gtk_text_layout_is_valid (GtkTextLayout *layout)
 {
@@ -787,7 +1008,7 @@ update_layout_size (GtkTextLayout *layout)
  *
  * Ensure that a region of a #GtkTextLayout is valid. The ::changed
  * signal will be emitted if any lines are validated.
- **/
+ */
 void
 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
                                  GtkTextIter   *anchor,
@@ -812,30 +1033,35 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
   /* Validate backwards from the anchor line to y0
    */
   line = _gtk_text_iter_get_text_line (anchor);
+  line = _gtk_text_line_previous (line);
   seen = 0;
   while (line && seen < -y0)
     {
       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
       if (!line_data || !line_data->valid)
         {
-          gint old_height = line_data ? line_data->height : 0;
+          gint old_height, new_height;
+         
+         old_height = line_data ? line_data->height : 0;
 
           _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
                                          line, layout);
           line_data = _gtk_text_line_get_data (line, layout);
 
-          delta_height += line_data->height - old_height;
+         new_height = line_data ? line_data->height : 0;
+
+          delta_height += new_height - old_height;
           
           first_line = line;
-          first_line_y = -seen;
+          first_line_y = -seen - new_height;
           if (!last_line)
             {
               last_line = line;
-              last_line_y = -seen + line_data->height;
+              last_line_y = -seen;
             }
         }
 
-      seen += line_data->height;
+      seen += line_data ? line_data->height : 0;
       line = _gtk_text_line_previous (line);
     }
 
@@ -847,13 +1073,16 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
       if (!line_data || !line_data->valid)
         {
-          gint old_height = line_data ? line_data->height : 0;
+          gint old_height, new_height;
+         
+         old_height = line_data ? line_data->height : 0;
 
           _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
                                          line, layout);
           line_data = _gtk_text_line_get_data (line, layout);
+         new_height = line_data ? line_data->height : 0;
 
-          delta_height += line_data->height - old_height;
+          delta_height += new_height - old_height;
           
           if (!first_line)
             {
@@ -861,10 +1090,10 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
               first_line_y = seen;
             }
           last_line = line;
-          last_line_y = seen + line_data->height;
+          last_line_y = seen + new_height;
         }
 
-      seen += line_data->height;
+      seen += line_data ? line_data->height : 0;
       line = _gtk_text_line_next_excluding_last (line);
     }
 
@@ -880,10 +1109,10 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
       line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
                                                 first_line, layout);
 
-      gtk_text_layout_changed (layout,
-                               line_top,
-                               last_line_y - first_line_y - delta_height,
-                               last_line_y - first_line_y);
+      gtk_text_layout_emit_changed (layout,
+                                   line_top,
+                                   last_line_y - first_line_y - delta_height,
+                                   last_line_y - first_line_y);
     }
 }
 
@@ -912,7 +1141,7 @@ gtk_text_layout_validate (GtkTextLayout *layout,
       max_pixels -= new_height;
 
       update_layout_size (layout);
-      gtk_text_layout_changed (layout, y, old_height, new_height);
+      gtk_text_layout_emit_changed (layout, y, old_height, new_height);
     }
 }
 
@@ -950,10 +1179,8 @@ gtk_text_layout_real_wrap (GtkTextLayout   *layout,
    release_style () to free it. */
 static GtkTextAttributes*
 get_style (GtkTextLayout *layout,
-           const GtkTextIter *iter)
+          GPtrArray     *tags)
 {
-  GtkTextTag** tags;
-  gint tag_count = 0;
   GtkTextAttributes *style;
 
   /* If we have the one-style cache, then it means
@@ -968,11 +1195,8 @@ get_style (GtkTextLayout *layout,
 
   g_assert (layout->one_style_cache == NULL);
 
-  /* Get the tags at this spot */
-  tags = _gtk_text_btree_get_tags (iter, &tag_count);
-
   /* No tags, use default style */
-  if (tags == NULL || tag_count == 0)
+  if (tags == NULL || tags->len == 0)
     {
       /* One ref for the return value, one ref for the
          layout->one_style_cache reference */
@@ -980,25 +1204,17 @@ get_style (GtkTextLayout *layout,
       gtk_text_attributes_ref (layout->default_style);
       layout->one_style_cache = layout->default_style;
 
-      if (tags)
-        g_free (tags);
-
       return layout->default_style;
     }
 
-  /* Sort tags in ascending order of priority */
-  _gtk_text_tag_array_sort (tags, tag_count);
-
   style = gtk_text_attributes_new ();
 
   gtk_text_attributes_copy_values (layout->default_style,
                                    style);
 
   _gtk_text_attributes_fill_from_tags (style,
-                                       tags,
-                                       tag_count);
-
-  g_free (tags);
+                                       (GtkTextTag**) tags->pdata,
+                                       tags->len);
 
   g_assert (style->refcount == 1);
 
@@ -1040,9 +1256,7 @@ totally_invisible_line (GtkTextLayout *layout,
    * invisible/noninvisible toggle state; this function can use the whole btree 
    * to get it right.
    */
-  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                   iter, line, 0);
-  
+  gtk_text_layout_get_iter_at_line (layout, iter, line, 0);
   if (!_gtk_text_btree_char_is_invisible (iter))
     return FALSE;
 
@@ -1066,8 +1280,8 @@ totally_invisible_line (GtkTextLayout *layout,
           invalidate_cached_style (layout);
 
           /* Bail out if an elision-unsetting tag begins */
-          if (seg->body.toggle.info->tag->invisible_set &&
-              !seg->body.toggle.info->tag->values->invisible)
+          if (seg->body.toggle.info->tag->priv->invisible_set &&
+              !seg->body.toggle.info->tag->priv->values->invisible)
             break;
         }
       else if (seg->type == &gtk_text_toggle_off_type)
@@ -1075,8 +1289,8 @@ totally_invisible_line (GtkTextLayout *layout,
           invalidate_cached_style (layout);
 
           /* Bail out if an elision-setting tag ends */
-          if (seg->body.toggle.info->tag->invisible_set &&
-              seg->body.toggle.info->tag->values->invisible)
+          if (seg->body.toggle.info->tag->priv->invisible_set &&
+              seg->body.toggle.info->tag->priv->values->invisible)
             break;
         }
 
@@ -1091,32 +1305,53 @@ totally_invisible_line (GtkTextLayout *layout,
 
 static void
 set_para_values (GtkTextLayout      *layout,
-                 GtkTextAttributes *style,
+                 PangoDirection      base_dir,
+                 GtkTextAttributes  *style,
                  GtkTextLineDisplay *display)
 {
   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
-  int layout_width;
+  PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
 
-  display->direction = style->direction;
+  switch (base_dir)
+    {
+    /* If no base direction was found, then use the style direction */
+    case PANGO_DIRECTION_NEUTRAL :
+      display->direction = style->direction;
 
-  if (display->direction == GTK_TEXT_DIR_LTR)
-    display->layout = pango_layout_new (layout->ltr_context);
-  else
+      /* Override the base direction */
+      if (display->direction == GTK_TEXT_DIR_RTL)
+        base_dir = PANGO_DIRECTION_RTL;
+      else
+        base_dir = PANGO_DIRECTION_LTR;
+      
+      break;
+    case PANGO_DIRECTION_RTL :
+      display->direction = GTK_TEXT_DIR_RTL;
+      break;
+    default:
+      display->direction = GTK_TEXT_DIR_LTR;
+      break;
+    }
+  
+  if (display->direction == GTK_TEXT_DIR_RTL)
     display->layout = pango_layout_new (layout->rtl_context);
+  else
+    display->layout = pango_layout_new (layout->ltr_context);
 
   switch (style->justification)
     {
     case GTK_JUSTIFY_LEFT:
-      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
+      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
       break;
     case GTK_JUSTIFY_RIGHT:
-      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
+      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
       break;
     case GTK_JUSTIFY_CENTER:
       pango_align = PANGO_ALIGN_CENTER;
       break;
     case GTK_JUSTIFY_FILL:
-      g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
+      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
+      pango_layout_set_justify (display->layout, TRUE);
       break;
     default:
       g_assert_not_reached ();
@@ -1144,22 +1379,38 @@ set_para_values (GtkTextLayout      *layout,
   switch (style->wrap_mode)
     {
     case GTK_WRAP_CHAR:
-      layout_width = layout->screen_width - display->left_margin - display->right_margin;
-      pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
-      pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
+      pango_wrap = PANGO_WRAP_CHAR;
       break;
-
     case GTK_WRAP_WORD:
-      layout_width = layout->screen_width - display->left_margin - display->right_margin;
-      pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
-      pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
+      pango_wrap = PANGO_WRAP_WORD;
+      break;
+
+    case GTK_WRAP_WORD_CHAR:
+      pango_wrap = PANGO_WRAP_WORD_CHAR;
       break;
 
     case GTK_WRAP_NONE:
       break;
     }
-  
+
+  if (style->wrap_mode != GTK_WRAP_NONE)
+    {
+      int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
+      pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
+      pango_layout_set_wrap (display->layout, pango_wrap);
+    }
+
   display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
+  
+  if (style->pg_bg_color)
+    display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
+  else
+    display->pg_bg_color = NULL;
+
+  if (style->pg_bg_rgba)
+    display->pg_bg_rgba = gdk_rgba_copy (style->pg_bg_rgba);
+  else
+    display->pg_bg_rgba = NULL;
 }
 
 static PangoAttribute *
@@ -1173,14 +1424,27 @@ gtk_text_attr_appearance_copy (const PangoAttribute *attr)
 static void
 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
 {
-  GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
+  GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
 
-  if (appearance->bg_stipple)
-    g_object_unref (appearance->bg_stipple);
-  if (appearance->fg_stipple)
-    g_object_unref (appearance->fg_stipple);
+  if (appearance_attr->appearance.rgba[0])
+    gdk_rgba_free (appearance_attr->appearance.rgba[0]);
 
-  g_free (attr);
+  if (appearance_attr->appearance.rgba[1])
+    gdk_rgba_free (appearance_attr->appearance.rgba[1]);
+
+  g_slice_free (GtkTextAttrAppearance, appearance_attr);
+}
+
+static gboolean 
+rgba_equal (const GdkRGBA *rgba1, const GdkRGBA *rgba2)
+{
+  if (rgba1 && rgba2)
+    return gdk_rgba_equal (rgba1, rgba2);
+  
+  if (rgba1 || rgba2)
+    return FALSE;
+
+  return TRUE;
 }
 
 static gboolean
@@ -1190,17 +1454,14 @@ gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
   const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
   const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
 
-  return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
-          gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
-          appearance1->fg_stipple ==  appearance2->fg_stipple &&
-          appearance1->bg_stipple ==  appearance2->bg_stipple &&
+  return (rgba_equal (appearance1->rgba[0], appearance2->rgba[0]) &&
+          rgba_equal (appearance1->rgba[1], appearance2->rgba[1]) &&
           appearance1->underline == appearance2->underline &&
           appearance1->strikethrough == appearance2->strikethrough &&
           appearance1->draw_bg == appearance2->draw_bg);
-
 }
 
-/**
+/*
  * gtk_text_attr_appearance_new:
  * @desc:
  *
@@ -1209,7 +1470,7 @@ gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
  * and size simultaneously.)
  *
  * Return value:
- **/
+ */
 static PangoAttribute *
 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
 {
@@ -1226,20 +1487,20 @@ gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
     klass.type = gtk_text_attr_appearance_type =
       pango_attr_type_register ("GtkTextAttrAppearance");
 
-  result = g_new (GtkTextAttrAppearance, 1);
+  result = g_slice_new (GtkTextAttrAppearance);
   result->attr.klass = &klass;
 
   result->appearance = *appearance;
 
-  if (appearance->bg_stipple)
-    g_object_ref (appearance->bg_stipple);
-  if (appearance->fg_stipple)
-    g_object_ref (appearance->fg_stipple);
+  if (appearance->rgba[0])
+    result->appearance.rgba[0] = gdk_rgba_copy (appearance->rgba[0]);
+
+  if (appearance->rgba[1])
+    result->appearance.rgba[1] = gdk_rgba_copy (appearance->rgba[1]);
 
   return (PangoAttribute *)result;
 }
 
-
 static void
 add_generic_attrs (GtkTextLayout      *layout,
                    GtkTextAppearance  *appearance,
@@ -1261,6 +1522,16 @@ add_generic_attrs (GtkTextLayout      *layout,
       pango_attr_list_insert (attrs, attr);
     }
 
+  if (appearance->strikethrough)
+    {
+      attr = pango_attr_strikethrough_new (appearance->strikethrough);
+      
+      attr->start_index = start;
+      attr->end_index = start + byte_count;
+      
+      pango_attr_list_insert (attrs, attr);
+    }
+
   if (appearance->rise != 0)
     {
       attr = pango_attr_rise_new (appearance->rise);
@@ -1332,13 +1603,11 @@ add_pixbuf_attrs (GtkTextLayout      *layout,
   logical_rect.width = width * PANGO_SCALE;
   logical_rect.height = height * PANGO_SCALE;
 
-  attr = pango_attr_shape_new (&logical_rect, &logical_rect);
+  attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
+                                        pixbuf->pixbuf, NULL, NULL);
   attr->start_index = start;
   attr->end_index = start + seg->byte_count;
   pango_attr_list_insert (attrs, attr);
-
-  display->shaped_objects =
-    g_slist_append (display->shaped_objects, pixbuf->pixbuf);
 }
 
 static void
@@ -1351,15 +1620,13 @@ add_child_attrs (GtkTextLayout      *layout,
 {
   PangoAttribute *attr;
   PangoRectangle logical_rect;
-  GtkTextChildAnchor *anchor;
   gint width, height;
   GSList *tmp_list;
+  GtkWidget *widget;
 
   width = 1;
   height = 1;
   
-  anchor = seg->body.child.obj;
-
   tmp_list = seg->body.child.widgets;
   while (tmp_list != NULL)
     {
@@ -1370,13 +1637,12 @@ add_child_attrs (GtkTextLayout      *layout,
           /* Found it */
           GtkRequisition req;
 
-          gtk_widget_get_child_requisition (child, &req);
-          
+          gtk_widget_get_preferred_size (child, &req, NULL);
+
           width = req.width;
           height = req.height;
 
-          display->shaped_objects =
-            g_slist_append (display->shaped_objects, child);
+         widget = child;
           
           break;
         }
@@ -1397,32 +1663,73 @@ add_child_attrs (GtkTextLayout      *layout,
       width = 30;
       height = 20;
 
-      display->shaped_objects =
-        g_slist_append (display->shaped_objects, NULL);
+      widget = NULL;
     }
-  
+
   logical_rect.x = 0;
   logical_rect.y = -height * PANGO_SCALE;
   logical_rect.width = width * PANGO_SCALE;
   logical_rect.height = height * PANGO_SCALE;
 
-  attr = pango_attr_shape_new (&logical_rect, &logical_rect);
+  attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
+                                        widget, NULL, NULL);
   attr->start_index = start;
   attr->end_index = start + seg->byte_count;
   pango_attr_list_insert (attrs, attr);
 }
 
+/*
+ * get_block_cursor:
+ * @layout: a #GtkTextLayout
+ * @display: a #GtkTextLineDisplay
+ * @insert_iter: iter pointing to the cursor location
+ * @insert_index: cursor offset in the @display's layout, it may
+ * be different from @insert_iter's offset in case when preedit
+ * string is present.
+ * @pos: location to store cursor position
+ * @cursor_at_line_end: whether cursor is at the end of line
+ *
+ * Checks whether layout should display block cursor at given position.
+ * For this layout must be in overwrite mode and text at @insert_iter 
+ * must be editable.
+ */
+static gboolean
+get_block_cursor (GtkTextLayout      *layout,
+                 GtkTextLineDisplay *display,
+                 const GtkTextIter  *insert_iter,
+                 gint                insert_index,
+                 GdkRectangle       *pos,
+                 gboolean           *cursor_at_line_end)
+{
+  PangoRectangle pango_pos;
+
+  if (layout->overwrite_mode &&
+      gtk_text_iter_editable (insert_iter, TRUE) &&
+      _gtk_text_util_get_block_cursor_location (display->layout,
+                                               insert_index,
+                                               &pango_pos,
+                                               cursor_at_line_end))
+    {
+      if (pos)
+       {
+         pos->x = PANGO_PIXELS (pango_pos.x);
+         pos->y = PANGO_PIXELS (pango_pos.y);
+         pos->width = PANGO_PIXELS (pango_pos.width);
+         pos->height = PANGO_PIXELS (pango_pos.height);
+       }
+
+      return TRUE;
+    }
+  else
+    return FALSE;
+}
+
 static void
 add_cursor (GtkTextLayout      *layout,
             GtkTextLineDisplay *display,
             GtkTextLineSegment *seg,
             gint                start)
 {
-  PangoRectangle strong_pos, weak_pos;
-  GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
-  gboolean add_weak = FALSE;
-  gboolean add_strong = FALSE;
-  
   /* Hide insertion cursor when we have a selection or the layout
    * user has hidden the cursor.
    */
@@ -1432,46 +1739,30 @@ add_cursor (GtkTextLayout      *layout,
        gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
     return;
 
-  pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
-
-  if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
+  if (layout->overwrite_mode &&
+      _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
+                                     seg->body.mark.obj))
     {
-      add_strong = TRUE;
-      add_weak = TRUE;
-    }
-  else if (display->direction == layout->cursor_direction)
-    add_strong = TRUE;
-  else
-    add_weak = TRUE;
+      GtkTextIter iter;
+      gboolean cursor_at_line_end;
 
-  if (add_strong)
-    {
-      cursor = g_new (GtkTextCursorDisplay, 1);
+      _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
+                                       &iter, seg->body.mark.obj);
 
-      cursor->x = PANGO_PIXELS (strong_pos.x);
-      cursor->y = PANGO_PIXELS (strong_pos.y);
-      cursor->height = PANGO_PIXELS (strong_pos.height);
-      cursor->is_strong = TRUE;
-      cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
-      display->cursors = g_slist_prepend (display->cursors, cursor);
-    }
-  
-  if (add_weak)
-    {
-      if (weak_pos.x == strong_pos.x && add_strong)
-       cursor->is_weak = TRUE;
-      else
+      if (get_block_cursor (layout, display, &iter, start,
+                           &display->block_cursor,
+                           &cursor_at_line_end))
        {
-         cursor = g_new (GtkTextCursorDisplay, 1);
-         
-         cursor->x = PANGO_PIXELS (weak_pos.x);
-         cursor->y = PANGO_PIXELS (weak_pos.y);
-         cursor->height = PANGO_PIXELS (weak_pos.height);
-         cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
-         cursor->is_weak = TRUE;
-         display->cursors = g_slist_prepend (display->cursors, cursor);
+         display->has_block_cursor = TRUE;
+         display->cursor_at_line_end = cursor_at_line_end;
+         return;
        }
     }
+
+  if (!display->cursors)
+    display->cursors = g_array_new (FALSE, FALSE, sizeof(int));
+
+  display->cursors = g_array_append_val (display->cursors, start);
 }
 
 static gboolean
@@ -1496,58 +1787,72 @@ static void
 allocate_child_widgets (GtkTextLayout      *text_layout,
                         GtkTextLineDisplay *display)
 {
-  GSList *shaped = display->shaped_objects;
   PangoLayout *layout = display->layout;
-  PangoLayoutIter *iter;
-  
-  iter = pango_layout_get_iter (layout);
-  
+  PangoLayoutIter *run_iter;
+
+  run_iter = pango_layout_get_iter (layout);
   do
     {
-      PangoLayoutRun *run = pango_layout_iter_get_run (iter);
+      PangoLayoutRun *run = pango_layout_iter_get_run_readonly (run_iter);
 
       if (run && is_shape (run))
         {
-          GObject *shaped_object = shaped->data;
-          shaped = shaped->next;
-
-          /* shaped_object is NULL for child anchors with no
-           * widgets stored at them
+          gint byte_index;
+          GtkTextIter text_iter;
+          GtkTextChildAnchor *anchor = NULL;
+          GList *widgets = NULL;
+          GList *l;
+
+          /* The pango iterator iterates in visual order.
+           * We use the byte index to find the child widget.
            */
-          if (shaped_object && GTK_IS_WIDGET (shaped_object))
+          byte_index = pango_layout_iter_get_index (run_iter);
+          line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
+          anchor = gtk_text_iter_get_child_anchor (&text_iter);
+         if (anchor)
+            widgets = gtk_text_child_anchor_get_widgets (anchor);
+
+          for (l = widgets; l; l = l->next)
             {
               PangoRectangle extents;
+              GtkWidget *child = l->data;
 
-              /* We emit "allocate_child" with the x,y of
-               * the widget with respect to the top of the line
-               * and the left side of the buffer
-               */
-              
-              pango_layout_iter_get_run_extents (iter,
-                                                 NULL,
-                                                 &extents);
-              
-              g_signal_emit (text_layout,
-                             signals[ALLOCATE_CHILD],
-                             0,
-                             shaped_object,
-                             PANGO_PIXELS (extents.x) + display->x_offset,
-                             PANGO_PIXELS (extents.y) + display->top_margin);
+              if (_gtk_anchored_child_get_layout (child) == text_layout)
+                {
+
+                  /* We emit "allocate_child" with the x,y of
+                   * the widget with respect to the top of the line
+                   * and the left side of the buffer
+                   */
+                  pango_layout_iter_get_run_extents (run_iter,
+                                                     NULL,
+                                                     &extents);
+
+                  g_signal_emit (text_layout,
+                                 signals[ALLOCATE_CHILD],
+                                 0,
+                                 child,
+                                 PANGO_PIXELS (extents.x) + display->x_offset,
+                                 PANGO_PIXELS (extents.y) + display->top_margin);
+                }
             }
+
+          g_list_free (widgets);
         }
     }
-  while (pango_layout_iter_next_run (iter));
-  
-  pango_layout_iter_free (iter);
+  while (pango_layout_iter_next_run (run_iter));
+
+  pango_layout_iter_free (run_iter);
 }
 
 static void
-convert_color (GdkColor       *result,
+convert_color (GdkRGBA        *result,
               PangoAttrColor *attr)
 {
-  result->red = attr->color.red;
-  result->blue = attr->color.blue;
-  result->green = attr->color.green;
+  result->red = attr->color.red / 65535.;
+  result->blue = attr->color.blue / 65535.;
+  result->green = attr->color.green / 65535.;
+  result->alpha = 1;
 }
 
 /* This function is used to convert the preedit string attributes, which are
@@ -1587,14 +1892,21 @@ add_preedit_attrs (GtkTextLayout     *layout,
       while (tmp_list)
        {
          PangoAttribute *attr = tmp_list->data;
+         GdkRGBA rgba;
          
          switch (attr->klass->type)
            {
            case PANGO_ATTR_FOREGROUND:
-             convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
+             convert_color (&rgba, (PangoAttrColor *)attr);
+             if (appearance.rgba[1])
+               gdk_rgba_free (appearance.rgba[1]);
+             appearance.rgba[1] = gdk_rgba_copy (&rgba);
              break;
            case PANGO_ATTR_BACKGROUND:
-             convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
+             convert_color (&rgba, (PangoAttrColor *)attr);
+             if (appearance.rgba[0])
+               gdk_rgba_free (appearance.rgba[0]);
+             appearance.rgba[0] = gdk_rgba_copy (&rgba);
              appearance.draw_bg = TRUE;
              break;
            case PANGO_ATTR_UNDERLINE:
@@ -1642,11 +1954,166 @@ add_preedit_attrs (GtkTextLayout     *layout,
   pango_attr_iterator_destroy (iter);
 }
 
+/* Iterate over the line and fill in display->cursors.
+ * It's a stripped copy of gtk_text_layout_get_line_display() */
+static void
+update_text_display_cursors (GtkTextLayout      *layout,
+                            GtkTextLine        *line,
+                            GtkTextLineDisplay *display)
+{
+  GtkTextLineSegment *seg;
+  GtkTextIter iter;
+  gint layout_byte_offset, buffer_byte_offset;
+  GSList *cursor_byte_offsets = NULL;
+  GSList *cursor_segs = NULL;
+  GSList *tmp_list1, *tmp_list2;
+
+  if (!display->cursors_invalid)
+    return;
+
+  display->cursors_invalid = FALSE;
+
+  /* Special-case optimization for completely
+   * invisible lines; makes it faster to deal
+   * with sequences of invisible lines.
+   */
+  if (totally_invisible_line (layout, line, &iter))
+    return;
+
+  /* Iterate over segments */
+  layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
+  buffer_byte_offset = 0; /* position in the buffer line */
+  seg = _gtk_text_iter_get_any_segment (&iter);
+  while (seg != NULL)
+    {
+      /* Displayable segments */
+      if (seg->type == &gtk_text_char_type ||
+          seg->type == &gtk_text_pixbuf_type ||
+          seg->type == &gtk_text_child_type)
+        {
+          gtk_text_layout_get_iter_at_line (layout, &iter, line,
+                                            buffer_byte_offset);
+
+          if (!_gtk_text_btree_char_is_invisible (&iter))
+            layout_byte_offset += seg->byte_count;
+
+         buffer_byte_offset += seg->byte_count;
+        }
+
+      /* Marks */
+      else if (seg->type == &gtk_text_right_mark_type ||
+               seg->type == &gtk_text_left_mark_type)
+        {
+         gint cursor_offset = 0;
+
+         /* At the insertion point, add the preedit string, if any */
+
+         if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
+                                             seg->body.mark.obj))
+           {
+             display->insert_index = layout_byte_offset;
+
+             if (layout->preedit_len > 0)
+               {
+                 layout_byte_offset += layout->preedit_len;
+                  /* DO NOT increment the buffer byte offset for preedit */
+                 cursor_offset = layout->preedit_cursor - layout->preedit_len;
+               }
+           }
+
+          /* Display visible marks */
+
+          if (seg->body.mark.visible)
+            {
+              cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
+                                                     GINT_TO_POINTER (layout_byte_offset + cursor_offset));
+              cursor_segs = g_slist_prepend (cursor_segs, seg);
+            }
+        }
+
+      /* Toggles */
+      else if (seg->type == &gtk_text_toggle_on_type ||
+               seg->type == &gtk_text_toggle_off_type)
+        {
+        }
+
+      else
+        g_error ("Unknown segment type: %s", seg->type->name);
+
+      seg = seg->next;
+    }
+
+  tmp_list1 = cursor_byte_offsets;
+  tmp_list2 = cursor_segs;
+  while (tmp_list1)
+    {
+      add_cursor (layout, display, tmp_list2->data,
+                  GPOINTER_TO_INT (tmp_list1->data));
+      tmp_list1 = tmp_list1->next;
+      tmp_list2 = tmp_list2->next;
+    }
+  g_slist_free (cursor_byte_offsets);
+  g_slist_free (cursor_segs);
+}
+
+/* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
+ * to be used in gtk_text_layout_get_line_display(). */
+static GPtrArray *
+get_tags_array_at_iter (GtkTextIter *iter)
+{
+  GtkTextTag **tags;
+  GPtrArray *array = NULL;
+  gint n_tags;
+
+  tags = _gtk_text_btree_get_tags (iter, &n_tags);
+
+  if (n_tags > 0)
+    {
+      array = g_ptr_array_sized_new (n_tags);
+      g_ptr_array_set_size (array, n_tags);
+      memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
+    }
+
+  g_free (tags);
+  return array;
+}
+
+/* Add the tag to the array if it's not there already, and remove
+ * it otherwise. It keeps the array sorted by tags priority. */
+static GPtrArray *
+tags_array_toggle_tag (GPtrArray  *array,
+                      GtkTextTag *tag)
+{
+  gint pos;
+  GtkTextTag **tags;
+
+  if (array == NULL)
+    array = g_ptr_array_new ();
+
+  tags = (GtkTextTag**) array->pdata;
+
+  for (pos = 0; pos < array->len && tags[pos]->priv->priority < tag->priv->priority; pos++) ;
+
+  if (pos < array->len && tags[pos] == tag)
+    g_ptr_array_remove_index (array, pos);
+  else
+    {
+      g_ptr_array_set_size (array, array->len + 1);
+      if (pos < array->len - 1)
+       memmove (array->pdata + pos + 1, array->pdata + pos,
+                (array->len - pos - 1) * sizeof (GtkTextTag*));
+      array->pdata[pos] = tag;
+    }
+
+  return array;
+}
+
 GtkTextLineDisplay *
 gtk_text_layout_get_line_display (GtkTextLayout *layout,
                                   GtkTextLine   *line,
                                   gboolean       size_only)
 {
+  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
   GtkTextLineDisplay *display;
   GtkTextLineSegment *seg;
   GtkTextIter iter;
@@ -1660,6 +2127,9 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   GSList *cursor_segs = NULL;
   GSList *tmp_list1, *tmp_list2;
   gboolean saw_widget = FALSE;
+  PangoDirection base_dir;
+  GPtrArray *tags;
+  gboolean initial_toggle_segments;
   
   g_return_val_if_fail (line != NULL, NULL);
 
@@ -1667,7 +2137,11 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
     {
       if (line == layout->one_display_cache->line &&
           (size_only || !layout->one_display_cache->size_only))
-        return layout->one_display_cache;
+       {
+         if (!size_only)
+            update_text_display_cursors (layout, line, layout->one_display_cache);
+         return layout->one_display_cache;
+       }
       else
         {
           GtkTextLineDisplay *tmp_display = layout->one_display_cache;
@@ -1676,22 +2150,40 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
         }
     }
 
-  display = g_new0 (GtkTextLineDisplay, 1);
+  DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
+
+  display = g_slice_new0 (GtkTextLineDisplay);
 
   display->size_only = size_only;
   display->line = line;
   display->insert_index = -1;
 
-  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                    &iter, line, 0);
-
   /* Special-case optimization for completely
    * invisible lines; makes it faster to deal
    * with sequences of invisible lines.
    */
   if (totally_invisible_line (layout, line, &iter))
-    return display;
+    {
+      if (display->direction == GTK_TEXT_DIR_RTL)
+       display->layout = pango_layout_new (layout->rtl_context);
+      else
+       display->layout = pango_layout_new (layout->ltr_context);
+      
+      return display;
+    }
 
+  /* Find the bidi base direction */
+  base_dir = line->dir_propagated_forward;
+  if (base_dir == PANGO_DIRECTION_NEUTRAL)
+    base_dir = line->dir_propagated_back;
+
+  if (line == priv->cursor_line &&
+      line->dir_strong == PANGO_DIRECTION_NEUTRAL)
+    {
+      base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
+        PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
+    }
+  
   /* Allocate space for flat text for buffer
    */
   text_allocated = _gtk_text_line_byte_count (line);
@@ -1699,10 +2191,12 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   attrs = pango_attr_list_new ();
 
-  /* Iterate over segments, creating display chunks for them. */
+  /* Iterate over segments, creating display chunks for them, and updating the tags array. */
   layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
   buffer_byte_offset = 0; /* position in the buffer line */
   seg = _gtk_text_iter_get_any_segment (&iter);
+  tags = get_tags_array_at_iter (&iter);
+  initial_toggle_segments = TRUE;
   while (seg != NULL)
     {
       /* Displayable segments */
@@ -1710,10 +2204,8 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
           seg->type == &gtk_text_pixbuf_type ||
           seg->type == &gtk_text_child_type)
         {
-          _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                            &iter, line,
-                                            buffer_byte_offset);
-          style = get_style (layout, &iter);
+          style = get_style (layout, tags);
+         initial_toggle_segments = FALSE;
 
           /* We have to delay setting the paragraph values until we
            * hit the first pixbuf or text segment because toggles at
@@ -1722,7 +2214,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
            */
           if (!para_values_set)
             {
-              set_para_values (layout, style, display);
+              set_para_values (layout, base_dir, style, display);
               para_values_set = TRUE;
             }
 
@@ -1770,6 +2262,9 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                            {
                              cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
                              cursor_segs = g_slist_prepend (cursor_segs, seg);
+                             if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
+                                                                 seg->body.mark.obj))
+                               display->insert_index = layout_byte_offset;
                            }
                         }
                      else
@@ -1796,7 +2291,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                                      size_only, FALSE);
                   add_pixbuf_attrs (layout, display, style,
                                     seg, attrs, layout_byte_offset);
-                  memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
+                  memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
                           seg->byte_count);
                   layout_byte_offset += seg->byte_count;
                   buffer_byte_offset += seg->byte_count;
@@ -1811,7 +2306,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                                      size_only, FALSE);
                   add_child_attrs (layout, display, style,
                                    seg, attrs, layout_byte_offset);
-                  memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
+                  memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
                           seg->byte_count);
                   layout_byte_offset += seg->byte_count;
                   buffer_byte_offset += seg->byte_count;
@@ -1839,6 +2334,10 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
           /* Style may have changed, drop our
              current cached style */
           invalidate_cached_style (layout);
+         /* Add the tag only after we have seen some non-toggle non-mark segment,
+          * otherwise the tag is already accounted for by _gtk_text_btree_get_tags(). */
+         if (!initial_toggle_segments)
+           tags = tags_array_toggle_tag (tags, seg->body.toggle.info->tag);
         }
 
       /* Marks */
@@ -1859,7 +2358,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                  text_allocated += layout->preedit_len;
                  text = g_realloc (text, text_allocated);
 
-                 style = get_style (layout, &iter);
+                 style = get_style (layout, tags);
                  add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
                  release_style (layout, style);
                   
@@ -1890,8 +2389,8 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   
   if (!para_values_set)
     {
-      style = get_style (layout, &iter);
-      set_para_values (layout, style, display);
+      style = get_style (layout, tags);
+      set_para_values (layout, base_dir, style, display);
       release_style (layout, style);
     }
   
@@ -1938,8 +2437,28 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   pango_layout_get_extents (display->layout, NULL, &extents);
 
-  display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
+  display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
   display->height += PANGO_PIXELS (extents.height);
+
+  /* If we aren't wrapping, we need to do the alignment of each
+   * paragraph ourselves.
+   */
+  if (pango_layout_get_width (display->layout) < 0)
+    {
+      gint excess = display->total_width - display->width;
+
+      switch (pango_layout_get_alignment (display->layout))
+       {
+       case PANGO_ALIGN_LEFT:
+         break;
+       case PANGO_ALIGN_CENTER:
+         display->x_offset += excess / 2;
+         break;
+       case PANGO_ALIGN_RIGHT:
+         display->x_offset += excess;
+         break;
+       }
+    }
   
   /* Free this if we aren't in a loop */
   if (layout->wrap_loop_count == 0)
@@ -1947,6 +2466,8 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   g_free (text);
   pango_attr_list_unref (attrs);
+  if (tags != NULL)
+    g_ptr_array_free (tags, TRUE);
 
   layout->one_display_cache = display;
 
@@ -1966,13 +2487,15 @@ gtk_text_layout_free_line_display (GtkTextLayout      *layout,
         g_object_unref (display->layout);
 
       if (display->cursors)
-        {
-          g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
-          g_slist_free (display->cursors);
-        }
-      g_slist_free (display->shaped_objects);
+        g_array_free (display->cursors, TRUE);
 
-      g_free (display);
+      if (display->pg_bg_color)
+        gdk_color_free (display->pg_bg_color);
+
+      if (display->pg_bg_rgba)
+        gdk_rgba_free (display->pg_bg_rgba);
+
+      g_slice_free (GtkTextLineDisplay, display);
     }
 }
 
@@ -1990,8 +2513,11 @@ line_display_iter_to_index (GtkTextLayout      *layout,
 
   index = gtk_text_iter_get_visible_line_index (iter);
   
-  if (index >= display->insert_index)
-    index += layout->preedit_len;
+  if (layout->preedit_len > 0 && display->insert_index >= 0)
+    {
+      if (index >= display->insert_index)
+       index += layout->preedit_len;
+    }
 
   return index;
 }
@@ -2006,16 +2532,18 @@ line_display_index_to_iter (GtkTextLayout      *layout,
   g_return_if_fail (!_gtk_text_line_is_last (display->line,
                                              _gtk_text_buffer_get_btree (layout->buffer)));
   
-  if (index >= display->insert_index + layout->preedit_len)
-    index -= layout->preedit_len;
-  else if (index > display->insert_index)
+  if (layout->preedit_len > 0 && display->insert_index >= 0)
     {
-      index = display->insert_index;
-      trailing = 0;
+      if (index >= display->insert_index + layout->preedit_len)
+       index -= layout->preedit_len;
+      else if (index > display->insert_index)
+       {
+         index = display->insert_index;
+         trailing = 0;
+       }
     }
 
-  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                    iter, display->line, 0);
+  gtk_text_layout_get_iter_at_line (layout, iter, display->line, 0);
 
   gtk_text_iter_set_visible_line_index (iter, index);
   
@@ -2024,9 +2552,7 @@ line_display_index_to_iter (GtkTextLayout      *layout,
       /* Clamp to end of line - really this clamping should have been done
        * before here, maybe in Pango, this is a broken band-aid I think
        */
-      _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                        iter, display->line, 0);
-
+      gtk_text_layout_get_iter_at_line (layout, iter, display->line, 0);
       if (!gtk_text_iter_ends_line (iter))
         gtk_text_iter_forward_to_line_end (iter);
     }
@@ -2064,11 +2590,11 @@ get_line_at_y (GtkTextLayout *layout,
  * @target_iter: the iterator in which the result is stored
  * @y: the y positition
  * @line_top: location to store the y coordinate of the
- *            top of the line. (Can by %NULL.)
+ *            top of the line. (Can by %NULL)
  *
  * Get the iter at the beginning of the line which is displayed
  * at the given y.
- **/
+ */
 void
 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
                                GtkTextIter   *target_iter,
@@ -2081,17 +2607,30 @@ gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
   g_return_if_fail (target_iter != NULL);
 
   get_line_at_y (layout, y, &line, line_top);
-  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                   target_iter, line, 0);
+  gtk_text_layout_get_iter_at_line (layout, target_iter, line, 0);
 }
 
 void
 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
-                                   GtkTextIter *target_iter,
-                                   gint x, gint y)
+                                   GtkTextIter   *target_iter,
+                                   gint           x, 
+                                  gint           y)
+{
+  gint trailing;
+
+  gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
+
+  gtk_text_iter_forward_chars (target_iter, trailing);  
+}
+
+void gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
+                                          GtkTextIter       *target_iter,
+                                          gint              *trailing,
+                                          gint               x,
+                                          gint               y)
 {
   GtkTextLine *line;
-  gint byte_index, trailing;
+  gint byte_index;
   gint line_top;
   GtkTextLineDisplay *display;
 
@@ -2111,7 +2650,8 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
   if (y > display->height - display->top_margin - display->bottom_margin)
     {
       byte_index = _gtk_text_line_byte_count (line);
-      trailing = 0;
+      if (trailing)
+        *trailing = 0;
     }
   else
     {
@@ -2120,20 +2660,21 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
         * x-direction.
         */
       pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
-                                &byte_index, &trailing);
+                                &byte_index, trailing);
     }
 
-  line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
+  line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
 
   gtk_text_layout_free_line_display (layout, display);
 }
 
+
 /**
  * gtk_text_layout_get_cursor_locations:
  * @layout: a #GtkTextLayout
  * @iter: a #GtkTextIter
- * @strong_pos: location to store the strong cursor position (may be %NULL)
- * @weak_pos: location to store the weak cursor position (may be %NULL)
+ * @strong_pos: (allow-none): location to store the strong cursor position (may be %NULL)
+ * @weak_pos: (allow-none): location to store the weak cursor position (may be %NULL)
  *
  * Given an iterator within a text layout, determine the positions of the
  * strong and weak cursors if the insertion point is at that
@@ -2154,6 +2695,7 @@ gtk_text_layout_get_cursor_locations (GtkTextLayout  *layout,
   GtkTextLineDisplay *display;
   gint line_top;
   gint index;
+  GtkTextIter insert_iter;
 
   PangoRectangle pango_strong_pos;
   PangoRectangle pango_weak_pos;
@@ -2168,9 +2710,15 @@ gtk_text_layout_get_cursor_locations (GtkTextLayout  *layout,
   line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
                                            line, layout);
   
+  gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
+                                    gtk_text_buffer_get_insert (layout->buffer));
+
+  if (gtk_text_iter_equal (iter, &insert_iter))
+    index += layout->preedit_cursor - layout->preedit_len;
+  
   pango_layout_get_cursor_pos (display->layout, index,
-                               strong_pos ? &pango_strong_pos : NULL,
-                               weak_pos ? &pango_weak_pos : NULL);
+                              strong_pos ? &pango_strong_pos : NULL,
+                              weak_pos ? &pango_weak_pos : NULL);
 
   if (strong_pos)
     {
@@ -2191,6 +2739,66 @@ gtk_text_layout_get_cursor_locations (GtkTextLayout  *layout,
   gtk_text_layout_free_line_display (layout, display);
 }
 
+/**
+ * _gtk_text_layout_get_block_cursor:
+ * @layout: a #GtkTextLayout
+ * @pos: a #GdkRectangle to store block cursor position
+ *
+ * If layout is to display a block cursor, calculates its position
+ * and returns %TRUE. Otherwise it returns %FALSE. In case when
+ * cursor is visible, it simply returns the position stored in
+ * the line display, otherwise it has to compute the position
+ * (see get_block_cursor()).
+ **/
+gboolean
+_gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
+                                  GdkRectangle  *pos)
+{
+  GtkTextLine *line;
+  GtkTextLineDisplay *display;
+  GtkTextIter iter;
+  GdkRectangle rect;
+  gboolean block = FALSE;
+
+  g_return_val_if_fail (layout != NULL, FALSE);
+
+  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
+                                    gtk_text_buffer_get_insert (layout->buffer));
+  line = _gtk_text_iter_get_text_line (&iter);
+  display = gtk_text_layout_get_line_display (layout, line, FALSE);
+
+  if (display->has_block_cursor)
+    {
+      block = TRUE;
+      rect = display->block_cursor;
+    }
+  else
+    {
+      gint index = display->insert_index;
+
+      if (index < 0)
+        index = gtk_text_iter_get_line_index (&iter);
+
+      if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
+       block = TRUE;
+    }
+
+  if (block && pos)
+    {
+      gint line_top;
+
+      line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
+                                               line, layout);
+
+      *pos = rect;
+      pos->x += display->x_offset;
+      pos->y += line_top + display->top_margin;
+    }
+
+  gtk_text_layout_free_line_display (layout, display);
+  return block;
+}
+
 /**
  * gtk_text_layout_get_line_yrange:
  * @layout: a #GtkTextLayout
@@ -2345,7 +2953,7 @@ find_display_line_below (GtkTextLayout *layout,
       do
         {
           gint first_y, last_y;
-          PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
+          PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
 
           found_byte = layout_line->start_index;
           
@@ -2372,8 +2980,7 @@ find_display_line_below (GtkTextLayout *layout,
       line = next;
     }
 
-  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                   iter, found_line, found_byte);
+  gtk_text_layout_get_iter_at_line (layout, iter, found_line, found_byte);
 }
 
 /* Find the iter for the logical beginning of the last display line whose
@@ -2416,7 +3023,7 @@ find_display_line_above (GtkTextLayout *layout,
       do
         {
           gint first_y, last_y;
-          PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
+          PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
 
           found_byte = layout_line->start_index;
 
@@ -2441,10 +3048,9 @@ find_display_line_above (GtkTextLayout *layout,
     }
 
  done:
-  
+
   if (found_line)
-    _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                     iter, found_line, found_byte);
+    gtk_text_layout_get_iter_at_line (layout, iter, found_line, found_byte);
   else
     gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
 }
@@ -2547,19 +3153,24 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
       update_byte = TRUE;
     }
   
-  tmp_list = pango_layout_get_lines (display->layout);
+  tmp_list = pango_layout_get_lines_readonly (display->layout);
   layout_line = tmp_list->data;
 
   if (update_byte)
     {
       line_byte = layout_line->start_index + layout_line->length;
     }
-  
+
   if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
     {
       GtkTextLine *prev_line;
 
       prev_line = _gtk_text_line_previous (line);
+
+      /* first line of the whole buffer, do not move the iter and return FALSE */
+      if (prev_line == NULL)
+        goto out;
+
       while (prev_line)
         {
           gtk_text_layout_free_line_display (layout, display);
@@ -2568,7 +3179,7 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
 
           if (display->height > 0)
             {
-              tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
+              tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
               layout_line = tmp_list->data;
 
               line_display_index_to_iter (layout, display, iter,
@@ -2578,9 +3189,6 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
 
           prev_line = _gtk_text_line_previous (prev_line);
         }
-
-      if (prev_line == NULL)
-       line_display_index_to_iter (layout, display, iter, 0, 0);
     }
   else
     {
@@ -2657,7 +3265,7 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
       else
        line_byte = 0;
        
-      tmp_list = pango_layout_get_lines (display->layout);
+      tmp_list = pango_layout_get_lines_readonly (display->layout);
       while (tmp_list && !found_after)
         {
           PangoLayoutLine *layout_line = tmp_list->data;
@@ -2681,6 +3289,9 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
       line = _gtk_text_line_next_excluding_last (line);
     }
 
+  if (!found_after)
+    gtk_text_buffer_get_end_iter (layout->buffer, iter);
+  
   return
     !gtk_text_iter_equal (iter, &orig) &&
     !gtk_text_iter_is_end (iter);
@@ -2714,7 +3325,7 @@ gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
   display = gtk_text_layout_get_line_display (layout, line, FALSE);
   line_byte = line_display_iter_to_index (layout, display, iter);
 
-  tmp_list = pango_layout_get_lines (display->layout);
+  tmp_list = pango_layout_get_lines_readonly (display->layout);
   while (tmp_list)
     {
       PangoLayoutLine *layout_line = tmp_list->data;
@@ -2729,9 +3340,10 @@ gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
           * are inside a paragraph to avoid going to next line on a
           * forced break not at whitespace. Real fix is to keep track
           * of whether marks are at leading or trailing edge?  */
-          if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
+          if (direction > 0 && layout_line->length > 0 && 
+             !gtk_text_iter_ends_line (iter) && 
+             !_gtk_text_btree_char_is_invisible (iter))
             gtk_text_iter_backward_char (iter);
-
           break;
         }
       
@@ -2769,7 +3381,7 @@ gtk_text_layout_iter_starts_line (GtkTextLayout       *layout,
   display = gtk_text_layout_get_line_display (layout, line, FALSE);
   line_byte = line_display_iter_to_index (layout, display, iter);
 
-  tmp_list = pango_layout_get_lines (display->layout);
+  tmp_list = pango_layout_get_lines_readonly (display->layout);
   while (tmp_list)
     {
       PangoLayoutLine *layout_line = tmp_list->data;
@@ -2805,7 +3417,6 @@ gtk_text_layout_get_iter_at_line (GtkTextLayout  *layout,
                                     iter, line, byte_offset);
 }
 
-
 /**
  * gtk_text_layout_move_iter_to_x:
  * @layout: a #GtkTextLayout
@@ -2838,7 +3449,7 @@ gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
 
   do
     {
-      PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
+      PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
 
       if (line_byte < layout_line->start_index + layout_line->length ||
           pango_layout_iter_at_last_line (layout_iter))
@@ -2891,6 +3502,7 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
 {
   GtkTextLineDisplay *display = NULL;
   GtkTextIter orig;
+  GtkTextIter lineiter;
   
   g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (iter != NULL, FALSE);
@@ -2948,20 +3560,28 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
       
       if (new_index < 0 || (new_index == 0 && extra_back))
         {
-          line = _gtk_text_line_previous (line);
-
-          if (!line)
-            goto done;
+          do
+            {
+              line = _gtk_text_line_previous (line);
+              if (!line)
+                goto done;
+            }
+          while (totally_invisible_line (layout, line, &lineiter));
           
          gtk_text_layout_free_line_display (layout, display);
          display = gtk_text_layout_get_line_display (layout, line, FALSE);
-          new_index = _gtk_text_line_byte_count (line);
+          gtk_text_iter_forward_to_line_end (&lineiter);
+          new_index = gtk_text_iter_get_visible_line_index (&lineiter);
         }
       else if (new_index > byte_count)
         {
-          line = _gtk_text_line_next_excluding_last (line);
-          if (!line)
-            goto done;
+          do
+            {
+              line = _gtk_text_line_next_excluding_last (line);
+              if (!line)
+                goto done;
+            }
+          while (totally_invisible_line (layout, line, &lineiter));
 
          gtk_text_layout_free_line_display (layout, display);
          display = gtk_text_layout_get_line_display (layout, line, FALSE);
@@ -3017,3 +3637,40 @@ gtk_text_layout_spew (GtkTextLayout *layout)
           layout->height, layout->screen_width);
 #endif
 }
+
+/* Catch all situations that move the insertion point.
+ */
+static void
+gtk_text_layout_mark_set_handler (GtkTextBuffer     *buffer,
+                                  const GtkTextIter *location,
+                                  GtkTextMark       *mark,
+                                  gpointer           data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  if (mark == gtk_text_buffer_get_insert (buffer))
+    gtk_text_layout_update_cursor_line (layout);
+}
+
+static void
+gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
+                                   GtkTextIter   *iter,
+                                   gchar         *str,
+                                   gint           len,
+                                   gpointer       data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line (layout);
+}
+
+static void
+gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
+                                    GtkTextIter   *start,
+                                    GtkTextIter   *end,
+                                    gpointer       data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line (layout);
+}