]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextlayout.c
Forgotten commit.
[~andy/gtk] / gtk / gtktextlayout.c
index b92e85cc8f16abdd9cd88ccb7e09bdf7be8706fd..3738a93e57a7594d07359ce379ac95762f732935 100644 (file)
@@ -77,7 +77,8 @@
  */
 
 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
-#include "gtksignal.h"
+#include <config.h>
+#include "gtkalias.h"
 #include "gtkmarshalers.h"
 #include "gtktextlayout.h"
 #include "gtktextbtree.h"
 #include <stdlib.h>
 #include <string.h>
 
+#define GTK_TEXT_LAYOUT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
+
+typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
+
+struct _GtkTextLayoutPrivate
+{
+  /* Cache the line that the cursor is positioned on, as the keyboard
+     direction only influences the direction of the cursor line.
+  */
+  GtkTextLine *cursor_line;
+};
+
 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
                                                    GtkTextLine *line,
                                                    /* may be NULL */
@@ -102,11 +115,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,
@@ -119,6 +152,8 @@ enum {
   LAST_ARG
 };
 
+#define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
+
 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);
@@ -149,10 +184,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;
@@ -178,7 +211,7 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
                   NULL, NULL,
                   _gtk_marshal_VOID__VOID,
-                  GTK_TYPE_NONE,
+                  G_TYPE_NONE,
                   0);
 
   signals[CHANGED] =
@@ -188,11 +221,11 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
                   NULL, NULL,
                   _gtk_marshal_VOID__INT_INT_INT,
-                  GTK_TYPE_NONE,
+                  G_TYPE_NONE,
                   3,
-                  GTK_TYPE_INT,
-                  GTK_TYPE_INT,
-                  GTK_TYPE_INT);
+                  G_TYPE_INT,
+                  G_TYPE_INT,
+                  G_TYPE_INT);
 
   signals[ALLOCATE_CHILD] =
     g_signal_new ("allocate_child",
@@ -201,11 +234,13 @@ gtk_text_layout_class_init (GtkTextLayoutClass *klass)
                   G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
                   NULL, NULL,
                   _gtk_marshal_VOID__OBJECT_INT_INT,
-                  GTK_TYPE_NONE,
+                  G_TYPE_NONE,
                   3,
                   GTK_TYPE_OBJECT,
-                  GTK_TYPE_INT,
-                  GTK_TYPE_INT);
+                  G_TYPE_INT,
+                  G_TYPE_INT);
+  
+  g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
 }
 
 static void
@@ -217,7 +252,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 +280,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 +316,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;
     }
 
@@ -282,9 +334,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);
     }
 }
 
@@ -325,16 +387,16 @@ 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);
@@ -364,6 +426,25 @@ 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
@@ -426,7 +507,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));
     }
@@ -511,7 +592,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
@@ -520,8 +610,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
@@ -672,23 +775,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,
@@ -700,6 +813,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");
@@ -712,12 +832,10 @@ 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;
@@ -733,12 +851,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);
 }
@@ -775,12 +888,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.
@@ -809,6 +922,7 @@ 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)
     {
@@ -824,11 +938,11 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
           delta_height += line_data->height - old_height;
           
           first_line = line;
-          first_line_y = -seen;
+          first_line_y = -seen - line_data->height;
           if (!last_line)
             {
               last_line = line;
-              last_line_y = -seen + line_data->height;
+              last_line_y = -seen;
             }
         }
 
@@ -877,10 +991,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);
     }
 }
 
@@ -909,7 +1023,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);
     }
 }
 
@@ -1032,25 +1146,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;
@@ -1097,26 +1202,46 @@ totally_invisible_line (GtkTextLayout *layout,
 
 static void
 set_para_values (GtkTextLayout      *layout,
-                 GtkTextAttributes *style,
+                 PangoDirection      base_dir,
+                 GtkTextAttributes  *style,
                  GtkTextLineDisplay *display)
 {
   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
   int layout_width;
 
-  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;
@@ -1161,10 +1286,16 @@ set_para_values (GtkTextLayout      *layout,
       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;
 }
 
@@ -1182,9 +1313,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);
 }
@@ -1238,9 +1369,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;
 }
@@ -1267,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);
@@ -1338,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);
@@ -1360,6 +1502,7 @@ add_child_attrs (GtkTextLayout      *layout,
   GtkTextChildAnchor *anchor;
   gint width, height;
   GSList *tmp_list;
+  GtkWidget *widget;
 
   width = 1;
   height = 1;
@@ -1381,8 +1524,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;
         }
@@ -1403,16 +1545,18 @@ add_child_attrs (GtkTextLayout      *layout,
       width = 30;
       height = 20;
 
-      display->shaped_objects =
-        g_slist_append (display->shaped_objects, NULL);
+      widget = 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);
@@ -1533,7 +1677,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,
@@ -1584,6 +1728,9 @@ add_preedit_attrs (GtkTextLayout     *layout,
       if (end == G_MAXINT)
        end = layout->preedit_len;
       
+      if (end == start)
+       continue;
+
       pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
       
       tmp_list = extra_attrs;
@@ -1650,6 +1797,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;
@@ -1663,6 +1811,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   GSList *cursor_segs = NULL;
   GSList *tmp_list1, *tmp_list2;
   gboolean saw_widget = FALSE;
+  PangoDirection base_dir;
   
   g_return_val_if_fail (line != NULL, NULL);
 
@@ -1695,6 +1844,18 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   if (totally_invisible_line (layout, line, &iter))
     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);
@@ -1725,7 +1886,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
            */
           if (!para_values_set)
             {
-              set_para_values (layout, style, display);
+              set_para_values (layout, base_dir, style, display);
               para_values_set = TRUE;
             }
 
@@ -1894,7 +2055,7 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
   if (!para_values_set)
     {
       style = get_style (layout, &iter);
-      set_para_values (layout, style, display);
+      set_para_values (layout, base_dir, style, display);
       release_style (layout, style);
     }
   
@@ -1941,8 +2102,28 @@ gtk_text_layout_get_line_display (GtkTextLayout *layout,
 
   pango_layout_get_extents (display->layout, NULL, &extents);
 
-  display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
+  display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
   display->height += PANGO_PIXELS (extents.height);
+
+  /* If we aren't wrapping, we need to do the alignment of each
+   * paragraph ourselves.
+   */
+  if (pango_layout_get_width (display->layout) < 0)
+    {
+      gint excess = display->total_width - display->width;
+
+      switch (pango_layout_get_alignment (display->layout))
+       {
+       case PANGO_ALIGN_LEFT:
+         break;
+       case PANGO_ALIGN_CENTER:
+         display->x_offset += excess / 2;
+         break;
+       case PANGO_ALIGN_RIGHT:
+         display->x_offset += excess;
+         break;
+       }
+    }
   
   /* Free this if we aren't in a loop */
   if (layout->wrap_loop_count == 0)
@@ -1966,7 +2147,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)
         {
@@ -1993,8 +2174,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;
 }
@@ -2009,12 +2193,15 @@ line_display_index_to_iter (GtkTextLayout      *layout,
   g_return_if_fail (!_gtk_text_line_is_last (display->line,
                                              _gtk_text_buffer_get_btree (layout->buffer)));
   
-  if (index >= display->insert_index + layout->preedit_len)
-    index -= layout->preedit_len;
-  else if (index > display->insert_index)
+  if (layout->preedit_len > 0 && display->insert_index >= 0)
     {
-      index = display->insert_index;
-      trailing = 0;
+      if (index >= display->insert_index + layout->preedit_len)
+       index -= layout->preedit_len;
+      else if (index > display->insert_index)
+       {
+         index = display->insert_index;
+         trailing = 0;
+       }
     }
 
   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
@@ -2090,11 +2277,25 @@ 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;
 
@@ -2114,7 +2315,7 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
   if (y > display->height - display->top_margin - display->bottom_margin)
     {
       byte_index = _gtk_text_line_byte_count (line);
-      trailing = 0;
+      *trailing = 0;
     }
   else
     {
@@ -2123,22 +2324,23 @@ gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
         * x-direction.
         */
       pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
-                                &byte_index, &trailing);
+                                &byte_index, trailing);
     }
 
-  line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
+  line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
 
   gtk_text_layout_free_line_display (layout, display);
 }
 
+
 /**
- * gtk_text_layout_get_cursor_locations
+ * 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
@@ -2157,6 +2359,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;
@@ -2171,6 +2374,13 @@ 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);
@@ -2557,12 +2767,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);
@@ -2581,9 +2796,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
     {
@@ -2684,6 +2896,9 @@ gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
       line = _gtk_text_line_next_excluding_last (line);
     }
 
+  if (!found_after)
+    gtk_text_buffer_get_end_iter (layout->buffer, iter);
+  
   return
     !gtk_text_iter_equal (iter, &orig) &&
     !gtk_text_iter_is_end (iter);
@@ -3020,3 +3235,40 @@ gtk_text_layout_spew (GtkTextLayout *layout)
           layout->height, layout->screen_width);
 #endif
 }
+
+/* Catch all situations that move the insertion point.
+ */
+static void
+gtk_text_layout_mark_set_handler (GtkTextBuffer     *buffer,
+                                  const GtkTextIter *location,
+                                  GtkTextMark       *mark,
+                                  gpointer           data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  if (mark == gtk_text_buffer_get_insert (buffer))
+    gtk_text_layout_update_cursor_line (layout);
+}
+
+static void
+gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
+                                   GtkTextIter   *iter,
+                                   gchar         *str,
+                                   gint           len,
+                                   gpointer       data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line (layout);
+}
+
+static void
+gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
+                                    GtkTextIter   *start,
+                                    GtkTextIter   *end,
+                                    gpointer       data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line (layout);
+}