]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextlayout.c
Include "." and ".." in the list of entries; they are needed for correct
[~andy/gtk] / gtk / gtktextlayout.c
index 01c8177a91b5f67d33918f34f98721c8cf8cc9b9..70125e52278b06a0a68ce4dff18fce7d590c9709 100644 (file)
@@ -76,7 +76,8 @@
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#include "gtksignal.h"
+#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
+#include "gtkmarshalers.h"
 #include "gtktextlayout.h"
 #include "gtktextbtree.h"
 #include "gtktextiterprivate.h"
@@ -84,9 +85,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-static GtkTextLineData    *gtk_text_line_data_new                 (GtkTextLayout     *layout,
-                                                                   GtkTextLine       *line);
-
 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
                                                    GtkTextLine *line,
                                                    /* may be NULL */
@@ -94,14 +92,15 @@ static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
 
 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_invalidate_cache    (GtkTextLayout     *layout,
-                                                 GtkTextLine       *line);
-static void gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
-                                                 GtkTextLine       *line,
-                                                 GtkTextLineData   *line_data);
+static void gtk_text_layout_real_invalidate        (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);
+static void gtk_text_layout_real_free_line_data    (GtkTextLayout     *layout,
+                                                   GtkTextLine       *line,
+                                                   GtkTextLineData   *line_data);
 
 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
 
@@ -149,10 +148,8 @@ gtk_text_layout_get_type (void)
         (GInstanceInitFunc) gtk_text_layout_init
       };
 
-      our_type = g_type_register_static (G_TYPE_OBJECT,
-                                         "GtkTextLayout",
-                                         &our_info,
-                                         0);
+      our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
+                                         &our_info, 0);
     }
 
   return our_type;
@@ -172,43 +169,43 @@ 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_TYPE_FROM_CLASS (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
-                   NULL,
-                   gtk_marshal_VOID__VOID,
-                   GTK_TYPE_NONE,
-                   0);
+    g_signal_new ("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_TYPE_FROM_CLASS (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
-                   NULL,
-                   gtk_marshal_VOID__INT_INT_INT,
-                   GTK_TYPE_NONE,
-                   3,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT);
+    g_signal_new ("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_TYPE_FROM_CLASS (object_class),
-                   G_SIGNAL_RUN_LAST,
-                   G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
-                   NULL,
-                   gtk_marshal_VOID__OBJECT_INT_INT,
-                   GTK_TYPE_NONE,
-                   3,
-                   GTK_TYPE_OBJECT,
-                   GTK_TYPE_INT,
-                   GTK_TYPE_INT);
+    g_signal_new ("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);
 }
 
-void
+static void
 gtk_text_layout_init (GtkTextLayout *text_layout)
 {
   text_layout->cursor_visible = TRUE;
@@ -217,7 +214,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
@@ -245,15 +242,22 @@ 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;
     }
   
+  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);
 }
 
@@ -274,7 +278,7 @@ 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_object_unref (layout->buffer);
       layout->buffer = NULL;
     }
 
@@ -282,7 +286,7 @@ 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);
     }
@@ -293,6 +297,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,20 +329,62 @@ 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));
+    g_object_unref (ltr_context);
 
   layout->ltr_context = ltr_context;
-  g_object_ref (G_OBJECT (ltr_context));
+  g_object_ref (ltr_context);
 
   if (layout->rtl_context)
-    g_object_unref (G_OBJECT (rtl_context));
+    g_object_unref (rtl_context);
 
   layout->rtl_context = rtl_context;
-  g_object_ref (G_OBJECT (rtl_context));
+  g_object_ref (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_cursor_direction:
+ * @direction: the new direction(s) for which to draw cursors.
+ *             %GTK_TEXT_DIR_NONE means draw cursors for both
+ *             left-to-right insertion and right-to-left insertion.
+ *             (The two cursors will be visually distinguished.)
+ * 
+ * Sets which text directions (left-to-right and/or right-to-left) for
+ * which cursors will be drawn for the insertion point. The visual
+ * 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)
+{
+  if (direction != layout->cursor_direction)
+    {
+      layout->cursor_direction = direction;
+      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)
 {
@@ -350,6 +397,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);
 }
 
@@ -420,10 +468,6 @@ gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
                                    PangoAttrList *preedit_attrs,
                                    gint           cursor_pos)
 {
-  GtkTextIter iter;
-  GtkTextLine *line;
-  GtkTextLineData *line_data;
-
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
   g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
 
@@ -451,19 +495,7 @@ gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
       layout->preedit_cursor = 0;
     }
 
-  /* Now invalidate the paragraph containing the cursor
-   */
-  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 (line_data)
-    {
-      gtk_text_layout_invalidate_cache (layout, line);
-      _gtk_text_line_invalidate_wrap (line, line_data);
-      gtk_text_layout_invalidated (layout);
-    }
+  gtk_text_layout_invalidate_cursor_line (layout);
 }
 
 void
@@ -483,7 +515,7 @@ 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);
 }
 
 void
@@ -492,8 +524,7 @@ 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);
+  g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
 }
 
 void
@@ -545,7 +576,6 @@ gtk_text_layout_get_lines (GtkTextLayout *layout,
                                    layout, top_y, first_line_y);
   if (first_btree_line == NULL)
     {
-      g_assert (top_y > 0);
       /* off the bottom */
       return NULL;
     }
@@ -553,19 +583,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);
 
@@ -577,7 +599,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);
@@ -648,6 +670,28 @@ gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
     }
 }
 
+/* Now invalidate the paragraph containing the cursor
+ */
+static void
+gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
+{
+  GtkTextIter iter;
+  GtkTextLine *line;
+  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 (line_data)
+    {
+      gtk_text_layout_invalidate_cache (layout, line);
+      _gtk_text_line_invalidate_wrap (line, line_data);
+      gtk_text_layout_invalidated (layout);
+    }
+}
+
 static void
 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                  const GtkTextIter *start,
@@ -659,6 +703,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");
@@ -671,17 +722,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);
@@ -692,12 +741,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);
 }
@@ -710,12 +754,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),
@@ -735,12 +778,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.
@@ -759,7 +802,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)
@@ -823,7 +865,7 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
         }
 
       seen += line_data->height;
-      line = _gtk_text_line_next (line);
+      line = _gtk_text_line_next_excluding_last (line);
     }
 
   /* If we found and validated any invalid lines, update size and
@@ -860,7 +902,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 &&
@@ -884,10 +925,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);
     }
 
@@ -994,14 +1036,16 @@ totally_invisible_line (GtkTextLayout *layout,
   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. */
+   * and we can just see if it is invisible.
+   */
   if (layout->one_style_cache &&
       !layout->one_style_cache->invisible)
     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. */
+   * 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),
@@ -1020,10 +1064,11 @@ totally_invisible_line (GtkTextLayout *layout,
         bytes += seg->byte_count;
 
       /* Note that these two tests can cause us to bail out
-         when we shouldn't, because a higher-priority tag
-         may override these settings. However the important
-         thing is to only invisible really-invisible lines, rather
-         than to invisible all really-invisible lines. */
+       * when we shouldn't, because a higher-priority tag
+       * may override these settings. However the important
+       * thing is to only invisible really-invisible lines, rather
+       * than to invisible all really-invisible lines.
+       */
 
       else if (seg->type == &gtk_text_toggle_on_type)
         {
@@ -1056,8 +1101,7 @@ totally_invisible_line (GtkTextLayout *layout,
 static void
 set_para_values (GtkTextLayout      *layout,
                  GtkTextAttributes *style,
-                 GtkTextLineDisplay *display,
-                 gdouble            *align)
+                 GtkTextLineDisplay *display)
 {
   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
   int layout_width;
@@ -1088,19 +1132,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);
@@ -1121,19 +1152,19 @@ set_para_values (GtkTextLayout      *layout,
 
   switch (style->wrap_mode)
     {
-    case GTK_WRAPMODE_CHAR:
+    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);
       break;
 
-    case GTK_WRAPMODE_WORD:
+    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_WRAPMODE_NONE:
+    case GTK_WRAP_NONE:
       break;
     }
   
@@ -1154,9 +1185,9 @@ gtk_text_attr_appearance_destroy (PangoAttribute *attr)
   GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
 
   if (appearance->bg_stipple)
-    gdk_drawable_unref (appearance->bg_stipple);
+    g_object_unref (appearance->bg_stipple);
   if (appearance->fg_stipple)
-    gdk_drawable_unref (appearance->fg_stipple);
+    g_object_unref (appearance->fg_stipple);
 
   g_free (attr);
 }
@@ -1210,9 +1241,9 @@ gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
   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,11 +1303,21 @@ 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;
 
   pango_attr_list_insert (attrs, attr);
+
+  if (style->font_scale != 1.0)
+    {
+      attr = pango_attr_scale_new (style->font_scale);
+
+      attr->start_index = start;
+      attr->end_index = start + byte_count;
+      
+      pango_attr_list_insert (attrs, attr);
+    }
 }
 
 static void
@@ -1354,23 +1395,19 @@ 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;
-    }
-
-  if (layout->preedit_string)
-    {
-      g_free (layout->preedit_string);
-      layout->preedit_string = NULL;
-    }
+      width = 30;
+      height = 20;
 
-  if (layout->preedit_attrs)
-    {
-      pango_attr_list_unref (layout->preedit_attrs);
-      layout->preedit_attrs = NULL;
+      display->shaped_objects =
+        g_slist_append (display->shaped_objects, NULL);
     }
   
   logical_rect.x = 0;
@@ -1391,8 +1428,10 @@ add_cursor (GtkTextLayout      *layout,
             gint                start)
 {
   PangoRectangle strong_pos, weak_pos;
-  GtkTextCursorDisplay *cursor;
-
+  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.
    */
@@ -1404,35 +1443,50 @@ add_cursor (GtkTextLayout      *layout,
 
   pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
 
-  cursor = g_new (GtkTextCursorDisplay, 1);
-
-  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;
-  display->cursors = g_slist_prepend (display->cursors, cursor);
-
-  if (weak_pos.x == strong_pos.x)
-    cursor->is_weak = TRUE;
-  else
+  if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
     {
-      cursor->is_weak = FALSE;
+      add_strong = TRUE;
+      add_weak = TRUE;
+    }
+  else if (display->direction == layout->cursor_direction)
+    add_strong = TRUE;
+  else
+    add_weak = TRUE;
 
+  if (add_strong)
+    {
       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 = FALSE;
-      cursor->is_weak = TRUE;
+      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
+       {
+         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);
+       }
+    }
 }
 
 static gboolean
 is_shape (PangoLayoutRun *run)
 {
-  GSList *tmp_list = run->item->extra_attrs;
+  GSList *tmp_list = run->item->analysis.extra_attrs;
     
   while (tmp_list)
     {
@@ -1466,7 +1520,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;
 
@@ -1478,10 +1535,8 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
               pango_layout_iter_get_run_extents (iter,
                                                  NULL,
                                                  &extents);
-
-              g_print ("extents at %d,%d\n", extents.x, extents.y);
               
-              g_signal_emit (G_OBJECT (text_layout),
+              g_signal_emit (text_layout,
                              signals[ALLOCATE_CHILD],
                              0,
                              shaped_object,
@@ -1499,9 +1554,9 @@ static void
 convert_color (GdkColor       *result,
               PangoAttrColor *attr)
 {
-  result->red = attr->red;
-  result->blue = attr->blue;
-  result->green = attr->green;
+  result->red = attr->color.red;
+  result->blue = attr->color.blue;
+  result->green = attr->color.green;
 }
 
 /* This function is used to convert the preedit string attributes, which are
@@ -1520,10 +1575,11 @@ 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;
+      PangoLanguage *language;
       gint start, end;
 
       pango_attr_iterator_range (iter, &start, &end);
@@ -1531,8 +1587,7 @@ add_preedit_attrs (GtkTextLayout     *layout,
       if (end == G_MAXINT)
        end = layout->preedit_len;
       
-      pango_attr_iterator_get_font (iter, &style->font,
-                                   &font_desc, &extra_attrs);
+      pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
       
       tmp_list = extra_attrs;
       while (tmp_list)
@@ -1567,15 +1622,26 @@ 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;
       
       pango_attr_list_insert (attrs, insert_attr);
 
+      if (language)
+       {
+         insert_attr = pango_attr_language_new (language);
+         insert_attr->start_index = start + offset;
+         insert_attr->end_index = end + offset;
+         
+         pango_attr_list_insert (attrs, insert_attr);
+       }
+
       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));
 
@@ -1593,8 +1659,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   GtkTextAttributes *style;
   gchar *text;
   PangoAttrList *attrs;
-  gint byte_count, layout_byte_offset, layout_only_bytes;
-  gdouble align;
+  gint text_allocated, layout_byte_offset, buffer_byte_offset;
   PangoRectangle extents;
   gboolean para_values_set = FALSE;
   GSList *cursor_byte_offsets = NULL;
@@ -1635,14 +1700,14 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   /* Allocate space for flat text for buffer
    */
-  byte_count = _gtk_text_line_byte_count (line);
-  text = g_malloc (byte_count);
+  text_allocated = _gtk_text_line_byte_count (line);
+  text = g_malloc (text_allocated);
 
   attrs = pango_attr_list_new ();
 
   /* Iterate over segments, creating display chunks for them. */
-  layout_byte_offset = 0; /* current length of layout text (includes preedit) */
-  layout_only_bytes = 0; /* bytes in layout_byte_offset not in buffer */
+  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);
   while (seg != NULL)
     {
@@ -1653,7 +1718,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
         {
           _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
                                             &iter, line,
-                                            layout_byte_offset - layout_only_bytes);
+                                            buffer_byte_offset);
           style = get_style (layout, &iter);
 
           /* We have to delay setting the paragraph values until we
@@ -1663,7 +1728,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
            */
           if (!para_values_set)
             {
-              set_para_values (layout, style, display, &align);
+              set_para_values (layout, style, display);
               para_values_set = TRUE;
             }
 
@@ -1692,6 +1757,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                         {
                           memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
                           layout_byte_offset += seg->byte_count;
+                          buffer_byte_offset += seg->byte_count;
                           bytes += seg->byte_count;
                         }
                      else if (seg->type == &gtk_text_right_mark_type ||
@@ -1739,6 +1805,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                   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;
                 }
               else if (seg->type == &gtk_text_child_type)
                 {
@@ -1753,11 +1820,19 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                   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;
                 }
               else
                 {
+                  /* We don't know this segment type */
                   g_assert_not_reached ();
                 }
+              
+            } /* if (segment was visible) */
+          else
+            {
+              /* Invisible segment */
+              buffer_byte_offset += seg->byte_count;
             }
 
           release_style (layout, style);
@@ -1787,8 +1862,8 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
              
              if (layout->preedit_len > 0)
                {
-                 byte_count += layout->preedit_len;
-                 text = g_realloc (text, byte_count);
+                 text_allocated += layout->preedit_len;
+                 text = g_realloc (text, text_allocated);
 
                  style = get_style (layout, &iter);
                  add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
@@ -1796,7 +1871,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
                   
                  memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
                  layout_byte_offset += layout->preedit_len;
-                  layout_only_bytes += layout->preedit_len;
+                  /* DO NOT increment the buffer byte offset for preedit */
                   
                  cursor_offset = layout->preedit_cursor - layout->preedit_len;
                }
@@ -1822,11 +1897,9 @@ 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, style, display);
       release_style (layout, style);
     }
-
-  g_assert (layout_byte_offset == byte_count);
   
   /* Pango doesn't want the trailing paragraph delimiters */
 
@@ -1871,8 +1944,6 @@ 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->height += PANGO_PIXELS (extents.height);
   
@@ -1897,21 +1968,22 @@ gtk_text_layout_free_line_display (GtkTextLayout      *layout,
 {
   if (display != layout->one_display_cache)
     {
-      g_object_unref (G_OBJECT (display->layout));
+      if (display->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_slist_free (display->shaped_objects);
 
       g_free (display);
     }
 }
 
 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
- * taking into account the preedit string, if necessary.
+ * taking into account the preedit string and invisible text if necessary.
  */
 static gint
 line_display_iter_to_index (GtkTextLayout      *layout,
@@ -1922,8 +1994,8 @@ line_display_iter_to_index (GtkTextLayout      *layout,
 
   g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
 
-  index = gtk_text_iter_get_line_index (iter);
-
+  index = gtk_text_iter_get_visible_line_index (iter);
+  
   if (index >= display->insert_index)
     index += layout->preedit_len;
 
@@ -1937,7 +2009,8 @@ line_display_index_to_iter (GtkTextLayout      *layout,
                            gint                index,
                            gint                trailing)
 {
-  gint line_len;
+  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;
@@ -1946,49 +2019,27 @@ line_display_index_to_iter (GtkTextLayout      *layout,
       index = display->insert_index;
       trailing = 0;
     }
-  
-  line_len = _gtk_text_line_byte_count (display->line);
-  g_assert (index <= line_len);
 
-  if (index < line_len)
-    _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
-                                      iter, display->line, index);
-  else
+  _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
+                                    iter, display->line, 0);
+
+  gtk_text_iter_set_visible_line_index (iter, index);
+  
+  if (_gtk_text_iter_get_text_line (iter) != display->line)
     {
       /* 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
        */
-      g_assert (index == line_len);
-      
       _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
                                         iter, display->line, 0);
 
       if (!gtk_text_iter_ends_line (iter))
         gtk_text_iter_forward_to_line_end (iter);
     }
-
-  /* FIXME should this be cursor positions? */
+  
   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,
@@ -2004,8 +2055,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),
@@ -2053,14 +2104,6 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
   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);
@@ -2068,17 +2111,23 @@ 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;
     }
+  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);
 
@@ -2186,6 +2235,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,
@@ -2244,9 +2332,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);
@@ -2284,7 +2371,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;
 
@@ -2312,8 +2399,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);
     }
 
@@ -2346,6 +2433,7 @@ find_display_line_above (GtkTextLayout *layout,
           if (tmp_top < y)
             {
               found_line = line;
+             pango_layout_iter_free (layout_iter);
               goto done;
             }
         }
@@ -2414,7 +2502,7 @@ gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
 }
 
 /**
- * gtk_text_layout_move_iter_to_next_line:
+ * gtk_text_layout_move_iter_to_previous_line:
  * @layout: a #GtkLayout
  * @iter:   a #GtkTextIter
  *
@@ -2431,35 +2519,73 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
   GSList *tmp_list;
   PangoLayoutLine *layout_line;
   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);
 
   orig = *iter;
-  
+
+
   line = _gtk_text_iter_get_text_line (iter);
   display = gtk_text_layout_get_line_display (layout, line, FALSE);
   line_byte = line_display_iter_to_index (layout, display, iter);
 
+  /* If display->height == 0 then the line is invisible, so don't
+   * move onto it.
+   */
+  while (display->height == 0)
+    {
+      GtkTextLine *prev_line;
+
+      prev_line = _gtk_text_line_previous (line);
+
+      if (prev_line == NULL)
+        {
+          line_display_index_to_iter (layout, display, iter, 0, 0);
+          goto out;
+        }
+
+      gtk_text_layout_free_line_display (layout, display);
+
+      line = prev_line;
+      display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
+      update_byte = TRUE;
+    }
+  
   tmp_list = pango_layout_get_lines (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 = _gtk_text_line_previous (line);
+      GtkTextLine *prev_line;
 
-      if (prev_line)
+      prev_line = _gtk_text_line_previous (line);
+      while (prev_line)
         {
           gtk_text_layout_free_line_display (layout, display);
+
           display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
-         tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
-         layout_line = tmp_list->data;
 
-          line_display_index_to_iter (layout, display, iter,
-                                      layout_line->start_index + layout_line->length, 0);
+          if (display->height > 0)
+            {
+              tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
+              layout_line = tmp_list->data;
+
+              line_display_index_to_iter (layout, display, iter,
+                                          layout_line->start_index + layout_line->length, 0);
+              break;
+            }
+
+          prev_line = _gtk_text_line_previous (prev_line);
         }
-      else
+
+      if (prev_line == NULL)
        line_display_index_to_iter (layout, display, iter, 0, 0);
     }
   else
@@ -2483,11 +2609,13 @@ gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
         }
     }
 
+ out:
+  
   gtk_text_layout_free_line_display (layout, display);
 
   return
     !gtk_text_iter_equal (iter, &orig) &&
-    !gtk_text_iter_is_last (iter);
+    !gtk_text_iter_is_end (iter);
 }
 
 /**
@@ -2511,7 +2639,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);
 
@@ -2525,6 +2652,9 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
 
       display = gtk_text_layout_get_line_display (layout, line, FALSE);
 
+      if (display->height == 0)
+        goto next;
+      
       if (first)
        {
          line_byte = line_display_iter_to_index (layout, display, iter);
@@ -2550,14 +2680,16 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
           tmp_list = tmp_list->next;
         }
 
+    next:
+      
       gtk_text_layout_free_line_display (layout, display);
 
-      line = _gtk_text_line_next (line);
+      line = _gtk_text_line_next_excluding_last (line);
     }
 
   return
     !gtk_text_iter_equal (iter, &orig) &&
-    !gtk_text_iter_is_last (iter);
+    !gtk_text_iter_is_end (iter);
 }
 
 /**
@@ -2579,7 +2711,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);
 
@@ -2617,7 +2748,7 @@ gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
 
   return
     !gtk_text_iter_equal (iter, &orig) &&
-    !gtk_text_iter_is_last (iter);
+    !gtk_text_iter_is_end (iter);
 }
 
 
@@ -2637,7 +2768,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);
 
@@ -2653,7 +2783,7 @@ gtk_text_layout_iter_starts_line (GtkTextLayout       *layout,
       if (line_byte < layout_line->start_index + layout_line->length ||
           !tmp_list->next)
         {
-          /* We're located on this line of the para delimiters before
+          /* We're located on this line or the para delimiters before
            * it
            */
           gtk_text_layout_free_line_display (layout, display);
@@ -2702,7 +2832,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);
 
@@ -2779,25 +2908,31 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
       GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
       gint line_byte;
       gint extra_back = 0;
+      gboolean strong;
 
       int byte_count = _gtk_text_line_byte_count (line);
 
       int new_index;
       int new_trailing;
 
       if (!display)
        display = gtk_text_layout_get_line_display (layout, line, FALSE);
+
+      if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
+       strong = TRUE;
+      else
+       strong = display->direction == layout->cursor_direction;
+
       line_byte = line_display_iter_to_index (layout, display, iter);
 
       if (count > 0)
         {
-          pango_layout_move_cursor_visually (display->layout, line_byte, 0, 1, &new_index, &new_trailing);
+          pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
           count--;
         }
       else
         {
-          pango_layout_move_cursor_visually (display->layout, line_byte, 0, -1, &new_index, &new_trailing);
+          pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
           count++;
         }
 
@@ -2830,7 +2965,7 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
         }
       else if (new_index > byte_count)
         {
-          line = _gtk_text_line_next (line);
+          line = _gtk_text_line_next_excluding_last (line);
           if (!line)
             goto done;
 
@@ -2850,7 +2985,7 @@ gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
   
   return
     !gtk_text_iter_equal (iter, &orig) &&
-    !gtk_text_iter_is_last (iter);
+    !gtk_text_iter_is_end (iter);
 }
 
 void