]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextlayout.c
Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>
[~andy/gtk] / gtk / gtktextlayout.c
index 603c47af92dc17e6b47b0bf3b2fc8de70a4e40db..5f62e70304b58003604ae9395173506891db0507 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#include "gtksignal.h"
+#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
+#include <config.h>
+#include "gtkmarshalers.h"
 #include "gtktextlayout.h"
 #include "gtktextbtree.h"
 #include "gtktextiterprivate.h"
+#include "gtkintl.h"
+#include "gtkalias.h"
 
 #include <stdlib.h>
 #include <string.h>
 
-static GtkTextLineData    *gtk_text_line_data_new                 (GtkTextLayout     *layout,
-                                                                   GtkTextLine       *line);
+#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,
@@ -103,11 +116,31 @@ static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout     *layout);
 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);
+
 enum {
   INVALIDATED,
   CHANGED,
@@ -120,52 +153,21 @@ 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 void gtk_text_layout_finalize (GObject *object);
 
-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)
-{
-  static GType our_type = 0;
-
-  if (our_type == 0)
-    {
-      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
-      };
-
-      our_type = g_type_register_static (G_TYPE_OBJECT,
-                                         "GtkTextLayout",
-                                         &our_info,
-                                         0);
-    }
-
-  return our_type;
-}
+G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
 
 static void
 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  parent_class = g_type_class_peek_parent (klass);
-  
   object_class->finalize = gtk_text_layout_finalize;
 
   klass->wrap = gtk_text_layout_real_wrap;
@@ -173,43 +175,45 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
   klass->free_line_data = gtk_text_layout_real_free_line_data;
 
   signals[INVALIDATED] =
-    g_signal_newc ("invalidated",
-                   G_OBJECT_CLASS_TYPE (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
-                  NULL, NULL,
-                  gtk_marshal_VOID__VOID,
-                   GTK_TYPE_NONE,
-                   0);
+    g_signal_new (I_("invalidated"),
+                  G_OBJECT_CLASS_TYPE (object_class),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__VOID,
+                  G_TYPE_NONE,
+                  0);
 
   signals[CHANGED] =
-    g_signal_newc ("changed",
-                   G_OBJECT_CLASS_TYPE (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
-                  NULL, NULL,
-                  gtk_marshal_VOID__INT_INT_INT,
-                   GTK_TYPE_NONE,
-                   3,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT);
+    g_signal_new (I_("changed"),
+                  G_OBJECT_CLASS_TYPE (object_class),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__INT_INT_INT,
+                  G_TYPE_NONE,
+                  3,
+                  G_TYPE_INT,
+                  G_TYPE_INT,
+                  G_TYPE_INT);
 
   signals[ALLOCATE_CHILD] =
-    g_signal_newc ("allocate_child",
-                   G_OBJECT_CLASS_TYPE (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
-                  NULL, NULL,
-                  gtk_marshal_VOID__OBJECT_INT_INT,
-                   GTK_TYPE_NONE,
-                   3,
-                   GTK_TYPE_OBJECT,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT);
+    g_signal_new (I_("allocate_child"),
+                  G_OBJECT_CLASS_TYPE (object_class),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__OBJECT_INT_INT,
+                  G_TYPE_NONE,
+                  3,
+                  GTK_TYPE_OBJECT,
+                  G_TYPE_INT,
+                  G_TYPE_INT);
+  
+  g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
 }
 
-void
+static void
 gtk_text_layout_init (GtkTextLayout *text_layout)
 {
   text_layout->cursor_visible = TRUE;
@@ -218,7 +222,7 @@ gtk_text_layout_init (GtkTextLayout *text_layout)
 GtkTextLayout*
 gtk_text_layout_new (void)
 {
-  return GTK_TEXT_LAYOUT (g_object_new (gtk_text_layout_get_type (), NULL));
+  return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
 }
 
 static void
@@ -246,16 +250,36 @@ gtk_text_layout_finalize (GObject *object)
 
   if (layout->ltr_context)
     {
-      g_object_unref (G_OBJECT (layout->ltr_context));
+      g_object_unref (layout->ltr_context);
       layout->ltr_context = NULL;
     }
   if (layout->rtl_context)
     {
-      g_object_unref (G_OBJECT (layout->rtl_context));
+      g_object_unref (layout->rtl_context);
       layout->rtl_context = NULL;
     }
   
-  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
+  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);
+    }
+
+  if (layout->preedit_string)
+    {
+      g_free (layout->preedit_string);
+      layout->preedit_string = NULL;
+    }
+
+  if (layout->preedit_attrs)
+    {
+      pango_attr_list_unref (layout->preedit_attrs);
+      layout->preedit_attrs = NULL;
+    }
+
+
+  (* G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize) (object);
 }
 
 void
@@ -275,7 +299,17 @@ gtk_text_layout_set_buffer (GtkTextLayout *layout,
       _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
                                   layout);
 
-      g_object_unref (G_OBJECT (layout->buffer));
+      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;
     }
 
@@ -283,9 +317,19 @@ gtk_text_layout_set_buffer (GtkTextLayout *layout,
     {
       layout->buffer = buffer;
 
-      g_object_ref (G_OBJECT (buffer));
+      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);
     }
 }
 
@@ -294,6 +338,7 @@ gtk_text_layout_default_style_changed (GtkTextLayout *layout)
 {
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
 
+  DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
   gtk_text_layout_invalidate_all (layout);
 }
 
@@ -324,18 +369,25 @@ gtk_text_layout_set_contexts (GtkTextLayout *layout,
 {
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
 
-  if (layout->ltr_context)
-    g_object_unref (G_OBJECT (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 (G_OBJECT (ltr_context));
+      layout->ltr_context = ltr_context;
+      g_object_ref (layout->ltr_context);
+    }
 
-  if (layout->rtl_context)
-    g_object_unref (G_OBJECT (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 (G_OBJECT (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);
 }
 
@@ -363,6 +415,42 @@ gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
     }
 }
 
+/**
+ * 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);
+    }
+}
+
+/**
+ * gtk_text_layout_get_buffer:
+ * @layout: a #GtkTextLayout
+ *
+ * Gets the text buffer used by the layout. See
+ * gtk_text_layout_set_buffer().
+ *
+ * Return value: the text buffer used by the layout.
+ **/
+GtkTextBuffer *
+gtk_text_layout_get_buffer (GtkTextLayout *layout)
+{
+  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
+
+  return layout->buffer;
+}
+
 void
 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
 {
@@ -375,6 +463,7 @@ gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
 
   layout->screen_width = width;
 
+  DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
   gtk_text_layout_invalidate_all (layout);
 }
 
@@ -407,7 +496,7 @@ gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
                                         gtk_text_buffer_get_mark (layout->buffer, "insert"));
 
       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));
     }
@@ -492,7 +581,16 @@ gtk_text_layout_get_size (GtkTextLayout *layout,
 static void
 gtk_text_layout_invalidated (GtkTextLayout *layout)
 {
-  g_signal_emit (G_OBJECT (layout), signals[INVALIDATED], 0);
+  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);
 }
 
 void
@@ -501,8 +599,21 @@ gtk_text_layout_changed (GtkTextLayout *layout,
                          gint           old_height,
                          gint           new_height)
 {
-  g_signal_emit (G_OBJECT (layout), signals[CHANGED], 0,
-                 y, old_height, new_height);
+  /* 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);
+    }
+  
+  gtk_text_layout_emit_changed (layout, y, old_height, new_height);
 }
 
 void
@@ -561,19 +672,11 @@ gtk_text_layout_get_lines (GtkTextLayout *layout,
   /* -1 since bottom_y is one past */
   last_btree_line =
     _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
-                                   layout, bottom_y - 1, NULL);
+                                    layout, bottom_y - 1, NULL);
 
   if (!last_btree_line)
     last_btree_line =
-      _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
-                               _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1,
-                               NULL);
-
-  {
-    GtkTextLineData *ld = _gtk_text_line_get_data (last_btree_line, layout);
-    if (ld->height == 0)
-      G_BREAKPOINT ();
-  }
+      _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
 
   g_assert (last_btree_line != NULL);
 
@@ -585,7 +688,7 @@ gtk_text_layout_get_lines (GtkTextLayout *layout,
       if (line == last_btree_line)
         break;
 
-      line = _gtk_text_line_next (line);
+      line = _gtk_text_line_next_excluding_last (line);
     }
 
   retval = g_slist_reverse (retval);
@@ -661,23 +764,33 @@ gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
 static void
 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
 {
-  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);
+      gtk_text_layout_invalidate_cache (layout, priv->cursor_line);
+      _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_mark (layout->buffer, "insert"));
+  
+  priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
+}
+
 static void
 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                  const GtkTextIter *start,
@@ -689,6 +802,13 @@ gtk_text_layout_real_invalidate (GtkTextLayout *layout,
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
   g_return_if_fail (layout->wrap_loop_count == 0);
 
+  /* Because we may be invalidating a mark, it's entirely possible
+   * that gtk_text_iter_equal (start, end) in which case we
+   * should still invalidate the line they are both on. i.e.
+   * we always invalidate the line with "start" even
+   * if there's an empty range.
+   */
+  
 #if 0
   gtk_text_view_index_spew (start_index, "invalidate start");
   gtk_text_view_index_spew (end_index, "invalidate end");
@@ -701,17 +821,15 @@ gtk_text_layout_real_invalidate (GtkTextLayout *layout,
     {
       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
 
-      if (line_data &&
-          (line != last_line || !gtk_text_iter_starts_line (end)))
-        {
-          gtk_text_layout_invalidate_cache (layout, line);
-          _gtk_text_line_invalidate_wrap (line, line_data);
-        }
+      gtk_text_layout_invalidate_cache (layout, line);
+      
+      if (line_data)
+        _gtk_text_line_invalidate_wrap (line, line_data);
 
       if (line == last_line)
         break;
 
-      line = _gtk_text_line_next (line);
+      line = _gtk_text_line_next_excluding_last (line);
     }
 
   gtk_text_layout_invalidated (layout);
@@ -722,12 +840,7 @@ gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
                                      GtkTextLine       *line,
                                      GtkTextLineData   *line_data)
 {
-  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);
-    }
+  gtk_text_layout_invalidate_cache (layout, line);
 
   g_free (line_data);
 }
@@ -740,12 +853,11 @@ 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
+ * Return value: %TRUE if any invalid regions were found
  **/
 gboolean
 gtk_text_layout_is_valid (GtkTextLayout *layout)
 {
-  g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
 
   return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
@@ -765,12 +877,12 @@ update_layout_size (GtkTextLayout *layout)
  * @layout: a #GtkTextLayout
  * @anchor: iter pointing into a line that will be used as the
  *          coordinate origin
- * @y0: offset from the top of the line pointed to by @anchor at
- *      which to begin validation. (The offset here is in pixels
- *      after validation.)
- * @y1: offset from the top of the line pointed to by @anchor at
- *      which to end validation. (The offset here is in pixels
- *      after validation.)
+ * @y0_: offset from the top of the line pointed to by @anchor at
+ *       which to begin validation. (The offset here is in pixels
+ *       after validation.)
+ * @y1_: offset from the top of the line pointed to by @anchor at
+ *       which to end validation. (The offset here is in pixels
+ *       after validation.)
  *
  * Ensure that a region of a #GtkTextLayout is valid. The ::changed
  * signal will be emitted if any lines are validated.
@@ -789,7 +901,6 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
   gint first_line_y = 0;        /* Quiet GCC */
   gint last_line_y = 0;         /* Quiet GCC */
 
-  g_return_if_fail (layout != NULL);
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
 
   if (y0 > 0)
@@ -800,30 +911,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);
     }
 
@@ -835,13 +951,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)
             {
@@ -849,11 +968,11 @@ 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;
-      line = _gtk_text_line_next (line);
+      seen += line_data ? line_data->height : 0;
+      line = _gtk_text_line_next_excluding_last (line);
     }
 
   /* If we found and validated any invalid lines, update size and
@@ -868,10 +987,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);
     }
 }
 
@@ -890,7 +1009,6 @@ gtk_text_layout_validate (GtkTextLayout *layout,
 {
   gint y, old_height, new_height;
 
-  g_return_if_fail (layout != NULL);
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
 
   while (max_pixels > 0 &&
@@ -901,7 +1019,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);
     }
 }
 
@@ -914,10 +1032,11 @@ gtk_text_layout_real_wrap (GtkTextLayout   *layout,
   GtkTextLineDisplay *display;
 
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
-
+  g_return_val_if_fail (line != NULL, NULL);
+  
   if (line_data == NULL)
     {
-      line_data = gtk_text_line_data_new (layout, line);
+      line_data = _gtk_text_line_data_new (layout, line);
       _gtk_text_line_add_data (line, line_data);
     }
 
@@ -1023,25 +1142,16 @@ totally_invisible_line (GtkTextLayout *layout,
   GtkTextLineSegment *seg;
   int bytes = 0;
 
-  /* If we have a cached style, then we know it does actually apply
-   * and we can just see if it is invisible.
+  /* Check if the first char is visible, if so we are partially visible.  
+   * Note that we have to check this since we don't know the current 
+   * invisible/noninvisible toggle state; this function can use the whole btree 
+   * to get it right.
    */
-  if (layout->one_style_cache &&
-      !layout->one_style_cache->invisible)
+  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
+                                   iter, line, 0);
+  
+  if (!_gtk_text_btree_char_is_invisible (iter))
     return FALSE;
-  /* Without the cache, we check if the first char is visible, if so
-   * we are partially visible.  Note that we have to check this since
-   * we don't know the current invisible/noninvisible toggle state; this
-   * function can use the whole btree to get it right.
-   */
-  else
-    {
-      _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                       iter, line, 0);
-
-      if (!_gtk_text_btree_char_is_invisible (iter))
-        return FALSE;
-    }
 
   bytes = 0;
   seg = line->segments;
@@ -1088,27 +1198,46 @@ totally_invisible_line (GtkTextLayout *layout,
 
 static void
 set_para_values (GtkTextLayout      *layout,
-                 GtkTextAttributes *style,
-                 GtkTextLineDisplay *display,
-                 gdouble            *align)
+                 PangoDirection      base_dir,
+                 GtkTextAttributes  *style,
+                 GtkTextLineDisplay *display)
 {
   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
   int layout_width;
 
-  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;
@@ -1121,19 +1250,6 @@ set_para_values (GtkTextLayout      *layout,
       break;
     }
 
-  switch (pango_align)
-    {
-    case PANGO_ALIGN_LEFT:
-      *align = 0.0;
-      break;
-    case PANGO_ALIGN_RIGHT:
-      *align = 1.0;
-      break;
-    case PANGO_ALIGN_CENTER:
-      *align = 0.5;
-      break;
-    }
-
   pango_layout_set_alignment (display->layout, pango_align);
   pango_layout_set_spacing (display->layout,
                             style->pixels_inside_wrap * PANGO_SCALE);
@@ -1159,18 +1275,28 @@ set_para_values (GtkTextLayout      *layout,
       pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
       pango_layout_set_wrap (display->layout, 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);
       break;
 
+    case GTK_WRAP_WORD_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_WORD_CHAR);
+      break;
+
     case GTK_WRAP_NONE:
       break;
     }
-  
+
   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;  
 }
 
 static PangoAttribute *
@@ -1184,14 +1310,14 @@ 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)
-    gdk_drawable_unref (appearance->bg_stipple);
-  if (appearance->fg_stipple)
-    gdk_drawable_unref (appearance->fg_stipple);
+  if (appearance_attr->appearance.bg_stipple)
+    g_object_unref (appearance_attr->appearance.bg_stipple);
+  if (appearance_attr->appearance.fg_stipple)
+    g_object_unref (appearance_attr->appearance.fg_stipple);
 
-  g_free (attr);
+  g_slice_free (GtkTextAttrAppearance, appearance_attr);
 }
 
 static gboolean
@@ -1237,15 +1363,15 @@ 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)
-    gdk_drawable_ref (appearance->bg_stipple);
+    g_object_ref (appearance->bg_stipple);
   if (appearance->fg_stipple)
-    gdk_drawable_ref (appearance->fg_stipple);
+    g_object_ref (appearance->fg_stipple);
 
   return (PangoAttribute *)result;
 }
@@ -1272,6 +1398,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);
@@ -1305,7 +1441,7 @@ add_text_attrs (GtkTextLayout      *layout,
 {
   PangoAttribute *attr;
 
-  attr = pango_attr_font_desc_new (&style->font);
+  attr = pango_attr_font_desc_new (style->font);
   attr->start_index = start;
   attr->end_index = start + byte_count;
 
@@ -1343,7 +1479,8 @@ 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);
@@ -1362,15 +1499,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)
     {
@@ -1386,8 +1521,7 @@ add_child_attrs (GtkTextLayout      *layout,
           width = req.width;
           height = req.height;
 
-          display->shaped_objects =
-            g_slist_append (display->shaped_objects, child);
+         widget = child;
           
           break;
         }
@@ -1397,31 +1531,29 @@ add_child_attrs (GtkTextLayout      *layout,
 
   if (tmp_list == NULL)
     {
-      /* No widget at this anchor in this display;
-       * not an error.
+      /* If tmp_list == NULL then there is no widget at this anchor in
+       * this display; not an error. We make up an arbitrary size
+       * to use, just so the programmer can see the blank spot.
+       * We also put a NULL in the shaped objects list, to keep
+       * the correspondence between the list and the shaped chars in
+       * the layout. A bad hack, yes.
        */
 
-      return;
-    }
+      width = 30;
+      height = 20;
 
-  if (layout->preedit_string)
-    {
-      g_free (layout->preedit_string);
-      layout->preedit_string = NULL;
+      widget = NULL;
     }
 
-  if (layout->preedit_attrs)
-    {
-      pango_attr_list_unref (layout->preedit_attrs);
-      layout->preedit_attrs = NULL;
-    }
+  display->shaped_objects = g_slist_append (display->shaped_objects, widget);
   
   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);
@@ -1467,7 +1599,7 @@ add_cursor (GtkTextLayout      *layout,
       cursor->y = PANGO_PIXELS (strong_pos.y);
       cursor->height = PANGO_PIXELS (strong_pos.height);
       cursor->is_strong = TRUE;
-      cursor->is_weak = FALSE;
+      cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
       display->cursors = g_slist_prepend (display->cursors, cursor);
     }
   
@@ -1482,7 +1614,7 @@ add_cursor (GtkTextLayout      *layout,
          cursor->x = PANGO_PIXELS (weak_pos.x);
          cursor->y = PANGO_PIXELS (weak_pos.y);
          cursor->height = PANGO_PIXELS (weak_pos.height);
-         cursor->is_strong = FALSE;
+         cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
          cursor->is_weak = TRUE;
          display->cursors = g_slist_prepend (display->cursors, cursor);
        }
@@ -1526,7 +1658,10 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
           GObject *shaped_object = shaped->data;
           shaped = shaped->next;
 
-          if (GTK_IS_WIDGET (shaped_object))
+          /* shaped_object is NULL for child anchors with no
+           * widgets stored at them
+           */
+          if (shaped_object && GTK_IS_WIDGET (shaped_object))
             {
               PangoRectangle extents;
 
@@ -1539,7 +1674,7 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
                                                  NULL,
                                                  &extents);
               
-              g_signal_emit (G_OBJECT (text_layout),
+              g_signal_emit (text_layout,
                              signals[ALLOCATE_CHILD],
                              0,
                              shaped_object,
@@ -1578,7 +1713,7 @@ add_preedit_attrs (GtkTextLayout     *layout,
   do
     {
       GtkTextAppearance appearance = style->appearance;
-      PangoFontDescription font_desc;
+      PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
       PangoAttribute *insert_attr;
       GSList *extra_attrs = NULL;
       GSList *tmp_list;
@@ -1590,8 +1725,10 @@ add_preedit_attrs (GtkTextLayout     *layout,
       if (end == G_MAXINT)
        end = layout->preedit_len;
       
-      pango_attr_iterator_get_font (iter, &style->font,
-                                   &font_desc, &language, &extra_attrs);
+      if (end == start)
+       continue;
+
+      pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
       
       tmp_list = extra_attrs;
       while (tmp_list)
@@ -1626,7 +1763,7 @@ add_preedit_attrs (GtkTextLayout     *layout,
       
       g_slist_free (extra_attrs);
       
-      insert_attr = pango_attr_font_desc_new (&font_desc);
+      insert_attr = pango_attr_font_desc_new (font_desc);
       insert_attr->start_index = start + offset;
       insert_attr->end_index = end + offset;
       
@@ -1644,6 +1781,8 @@ add_preedit_attrs (GtkTextLayout     *layout,
       add_generic_attrs (layout, &appearance, end - start,
                          attrs, start + offset,
                          size_only, TRUE);
+      
+      pango_font_description_free (font_desc);
     }
   while (pango_attr_iterator_next (iter));
 
@@ -1655,6 +1794,7 @@ 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;
@@ -1662,13 +1802,13 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   gchar *text;
   PangoAttrList *attrs;
   gint text_allocated, layout_byte_offset, buffer_byte_offset;
-  gdouble align;
   PangoRectangle extents;
   gboolean para_values_set = FALSE;
   GSList *cursor_byte_offsets = NULL;
   GSList *cursor_segs = NULL;
   GSList *tmp_list1, *tmp_list2;
   gboolean saw_widget = FALSE;
+  PangoDirection base_dir;
   
   g_return_val_if_fail (line != NULL, NULL);
 
@@ -1699,8 +1839,27 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
    * 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);
@@ -1731,7 +1890,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
            */
           if (!para_values_set)
             {
-              set_para_values (layout, style, display, &align);
+              set_para_values (layout, base_dir, style, display);
               para_values_set = TRUE;
             }
 
@@ -1900,7 +2059,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   if (!para_values_set)
     {
       style = get_style (layout, &iter);
-      set_para_values (layout, style, display, &align);
+      set_para_values (layout, base_dir, style, display);
       release_style (layout, style);
     }
   
@@ -1947,10 +2106,28 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   pango_layout_get_extents (display->layout, NULL, &extents);
 
-  display->x_offset += (display->total_width - PANGO_PIXELS (extents.x + extents.width)) * align;
-
-  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)
@@ -1974,7 +2151,7 @@ gtk_text_layout_free_line_display (GtkTextLayout      *layout,
   if (display != layout->one_display_cache)
     {
       if (display->layout)
-        g_object_unref (G_OBJECT (display->layout));
+        g_object_unref (display->layout);
 
       if (display->cursors)
         {
@@ -1982,6 +2159,9 @@ gtk_text_layout_free_line_display (GtkTextLayout      *layout,
           g_slist_free (display->cursors);
         }
       g_slist_free (display->shaped_objects);
+      
+      if (display->pg_bg_color)
+        gdk_color_free (display->pg_bg_color);
 
       g_free (display);
     }
@@ -2001,8 +2181,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;
 }
@@ -2014,12 +2197,18 @@ line_display_index_to_iter (GtkTextLayout      *layout,
                            gint                index,
                            gint                trailing)
 {
-  if (index >= display->insert_index + layout->preedit_len)
-    index -= layout->preedit_len;
-  else if (index > display->insert_index)
+  g_return_if_fail (!_gtk_text_line_is_last (display->line,
+                                             _gtk_text_buffer_get_btree (layout->buffer)));
+  
+  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),
@@ -2042,24 +2231,6 @@ line_display_index_to_iter (GtkTextLayout      *layout,
   gtk_text_iter_forward_chars (iter, trailing);
 }
 
-/* FIXME: This really doesn't belong in this file ... */
-static GtkTextLineData*
-gtk_text_line_data_new (GtkTextLayout *layout,
-                        GtkTextLine   *line)
-{
-  GtkTextLineData *line_data;
-
-  line_data = g_new (GtkTextLineData, 1);
-
-  line_data->view_id = layout;
-  line_data->next = NULL;
-  line_data->width = 0;
-  line_data->height = 0;
-  line_data->valid = FALSE;
-
-  return line_data;
-}
-
 static void
 get_line_at_y (GtkTextLayout *layout,
                gint           y,
@@ -2075,8 +2246,8 @@ get_line_at_y (GtkTextLayout *layout,
                                          layout, y, line_top);
   if (*line == NULL)
     {
-      *line = _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                       _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1, NULL);
+      *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
+      
       if (line_top)
         *line_top =
           _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
@@ -2113,25 +2284,31 @@ gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
 
 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;
 
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
   g_return_if_fail (target_iter != NULL);
 
-  /* Adjust pixels to be on-screen. This gives nice
-     behavior if the user is dragging with a pointer grab.
-  */
-  if (x < 0)
-    x = 0;
-  if (x > layout->width)
-    x = layout->width;
-
   get_line_at_y (layout, y, &line, &line_top);
 
   display = gtk_text_layout_get_line_display (layout, line, FALSE);
@@ -2139,31 +2316,38 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
   x -= display->x_offset;
   y -= line_top + display->top_margin;
 
-  /* We clamp y to the area of the actual layout so that the layouts
-   * hit testing works OK on the space above and below the layout
+  /* If we are below the layout, position the cursor at the last character
+   * of the line.
    */
-  y = CLAMP (y, 0, display->height - display->top_margin - display->bottom_margin - 1);
-
-  if (!pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
-                                 &byte_index, &trailing))
+  if (y > display->height - display->top_margin - display->bottom_margin)
     {
       byte_index = _gtk_text_line_byte_count (line);
-      trailing = 0;
+      *trailing = 0;
+    }
+  else
+    {
+       /* Ignore the "outside" return value from pango. Pango is doing
+        * the right thing even if we are outside the layout in the
+        * x-direction.
+        */
+      pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
+                                &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
+ * 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)
  *
- * Given an iterator within a text laout, determine the positions that of the
+ * Given an iterator within a text layout, determine the positions of the
  * strong and weak cursors if the insertion point is at that
  * iterator. The position of each cursor is stored as a zero-width
  * rectangle. The strong cursor location is the location where
@@ -2182,6 +2366,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;
@@ -2196,9 +2381,16 @@ 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_mark (layout->buffer,
+                                                              "insert"));
+
+  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)
     {
@@ -2257,6 +2449,45 @@ gtk_text_layout_get_line_yrange (GtkTextLayout     *layout,
     }
 }
 
+/**
+ * _gtk_text_layout_get_line_xrange:
+ * @layout: a #GtkTextLayout
+ * @iter:   a #GtkTextIter
+ * @x:      location to store the top of the paragraph in pixels,
+ *          or %NULL.
+ * @width  location to store the height of the paragraph in pixels,
+ *          or %NULL.
+ *
+ * Find the range of X coordinates for the paragraph containing
+ * the given iter. Private for 2.0 due to API freeze, could
+ * be made public for 2.2.
+ **/
+void
+_gtk_text_layout_get_line_xrange (GtkTextLayout     *layout,
+                                  const GtkTextIter *iter,
+                                  gint              *x,
+                                  gint              *width)
+{
+  GtkTextLine *line;
+
+  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
+  g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
+
+  line = _gtk_text_iter_get_text_line (iter);
+
+  if (x)
+    *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
+  
+  if (width)
+    {
+      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
+      if (line_data)
+        *width = line_data->width;
+      else
+        *width = 0;
+    }
+}
+
 void
 gtk_text_layout_get_iter_location (GtkTextLayout     *layout,
                                    const GtkTextIter *iter,
@@ -2315,9 +2546,8 @@ find_display_line_below (GtkTextLayout *layout,
   if (!line)
     {
       line =
-        _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                 _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1,
-                                 NULL);
+        _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
+
       line_top =
         _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
                                       line, layout);
@@ -2355,7 +2585,7 @@ find_display_line_below (GtkTextLayout *layout,
       line_top += display->bottom_margin;
       gtk_text_layout_free_line_display (layout, display);
 
-      next = _gtk_text_line_next (line);
+      next = _gtk_text_line_next_excluding_last (line);
       if (!next)
         found_line = line;
 
@@ -2383,8 +2613,8 @@ find_display_line_above (GtkTextLayout *layout,
   line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
   if (!line)
     {
-      line = _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                      _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1, NULL);
+      line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
+      
       line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
     }
 
@@ -2417,6 +2647,7 @@ find_display_line_above (GtkTextLayout *layout,
           if (tmp_top < y)
             {
               found_line = line;
+             pango_layout_iter_free (layout_iter);
               goto done;
             }
         }
@@ -2504,7 +2735,6 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
   GtkTextIter orig;
   gboolean update_byte = FALSE;
   
-  g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
   g_return_val_if_fail (iter != NULL, FALSE);
 
@@ -2544,12 +2774,17 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
     {
       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,9 +2803,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
     {
@@ -2623,7 +2855,6 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
   gboolean found_after = FALSE;
   gboolean first = TRUE;
 
-  g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
   g_return_val_if_fail (iter != NULL, FALSE);
 
@@ -2669,9 +2900,12 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
       
       gtk_text_layout_free_line_display (layout, display);
 
-      line = _gtk_text_line_next (line);
+      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);
@@ -2696,7 +2930,6 @@ gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
   GSList *tmp_list;
   GtkTextIter orig;
   
-  g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
   g_return_val_if_fail (iter != NULL, FALSE);
 
@@ -2721,9 +2954,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;
         }
       
@@ -2754,7 +2988,6 @@ gtk_text_layout_iter_starts_line (GtkTextLayout       *layout,
   gint line_byte;
   GSList *tmp_list;
   
-  g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
   g_return_val_if_fail (iter != NULL, FALSE);
 
@@ -2819,7 +3052,6 @@ gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
   gint line_byte;
   PangoLayoutIter *layout_iter;
   
-  g_return_if_fail (layout != NULL);
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
   g_return_if_fail (iter != NULL);
 
@@ -2885,6 +3117,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);
@@ -2942,10 +3175,17 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
       
       if (new_index < 0 || (new_index == 0 && extra_back))
         {
-          line = _gtk_text_line_previous (line);
+          do
+            {
+              line = _gtk_text_line_previous (line);
 
-          if (!line)
-            goto done;
+              if (!line)
+                goto done;
+              
+              _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
+                                                &lineiter, line, 0);
+            }
+          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);
@@ -2953,10 +3193,17 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
         }
       else if (new_index > byte_count)
         {
-          line = _gtk_text_line_next (line);
-          if (!line)
-            goto done;
+          do
+            {
+              line = _gtk_text_line_next_excluding_last (line);
+              if (!line)
+                goto done;
 
+              _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
+                                                &lineiter, line, 0);
+            }
+          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 = 0;
@@ -3011,3 +3258,43 @@ 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);
+}
+
+#define __GTK_TEXT_LAYOUT_C__
+#include "gtkaliasdef.c"