Index: gtkentry.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkentry.c,v
retrieving revision 1.221
diff -u -p -r1.221 gtkentry.c
--- gtkentry.c	11 Jul 2003 12:51:23 -0000	1.221
+++ gtkentry.c	30 Jul 2003 18:36:24 -0000
@@ -2376,7 +2373,7 @@ static void
 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
 				    GtkEntry  *entry)
 {
-  gtk_entry_queue_draw (entry);
+  gtk_entry_recompute (entry);
 }
 
 /* IM Context Callbacks
@@ -2592,7 +2589,8 @@ static PangoLayout *
 gtk_entry_create_layout (GtkEntry *entry,
 			 gboolean  include_preedit)
 {
-  PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
+  GtkWidget *widget = GTK_WIDGET (entry);
+  PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
   PangoAttrList *tmp_attrs = pango_attr_list_new ();
   
   gchar *preedit_string = NULL;
@@ -2657,6 +2655,33 @@ gtk_entry_create_layout (GtkEntry *entry
     }
   else
     {
+      PangoDirection pango_dir;
+      
+      pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
+      if (pango_dir == PANGO_DIRECTION_NEUTRAL)
+        {
+          if (GTK_WIDGET_HAS_FOCUS (widget))
+	    {
+	      GdkDisplay *display = gtk_widget_get_display (widget);
+	      GdkKeymap *keymap = gdk_keymap_get_for_display (display);
+	      pango_dir = gdk_keymap_get_direction (keymap);
+	    }
+          else
+	    {
+	      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
+		pango_dir = PANGO_DIRECTION_LTR;
+	      else
+		pango_dir = PANGO_DIRECTION_RTL;
+	    }
+        }
+
+      pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
+				  pango_dir);
+
+      pango_layout_set_alignment (layout, pango_dir);
+
+      entry->resolved_dir = pango_dir;
+      
       if (entry->visible)
         {
           pango_layout_set_text (layout, entry->text, entry->n_bytes);
@@ -2827,14 +2852,35 @@ gtk_entry_draw_text (GtkEntry *entry)
 }
 
 static void
+draw_insertion_cursor (GtkEntry      *entry,
+		       GdkRectangle  *cursor_location,
+		       PangoDirection direction,
+		       gboolean       is_primary,
+		       gboolean       draw_arrow)
+{
+  GtkWidget *widget = GTK_WIDGET (entry);
+  GtkTextDirection text_dir;
+  GdkGC *gc;
+
+  if (direction == PANGO_DIRECTION_LTR)
+    text_dir = GTK_TEXT_DIR_LTR;
+  else
+    text_dir = GTK_TEXT_DIR_RTL;
+
+  gc = _gtk_get_insertion_cursor_gc (widget, is_primary);
+  _gtk_draw_insertion_cursor (widget, entry->text_area, gc,
+			      cursor_location,
+			      text_dir,
+			      draw_arrow);
+  g_object_unref (gc);
+}
+
+static void
 gtk_entry_draw_cursor (GtkEntry  *entry,
 		       CursorType type)
 {
   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
-  GtkTextDirection keymap_direction =
-    (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
-    GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
-  GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
+  PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
   
   if (GTK_WIDGET_DRAWABLE (entry))
     {
@@ -2845,11 +2891,10 @@ gtk_entry_draw_cursor (GtkEntry  *entry,
       gint xoffset = INNER_BORDER - entry->scroll_offset;
       gint strong_x, weak_x;
       gint text_area_height;
-      GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
-      GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
+      PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
+      PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
       gint x1 = 0;
       gint x2 = 0;
-      GdkGC *gc;
 
       gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
       
@@ -2859,7 +2904,7 @@ gtk_entry_draw_cursor (GtkEntry  *entry,
 		    "gtk-split-cursor", &split_cursor,
 		    NULL);
 
-      dir1 = widget_direction;
+      dir1 = entry->resolved_dir;
       
       if (split_cursor)
 	{
@@ -2867,13 +2912,13 @@ gtk_entry_draw_cursor (GtkEntry  *entry,
 
 	  if (weak_x != strong_x)
 	    {
-	      dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
+	      dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
 	      x2 = weak_x;
 	    }
 	}
       else
 	{
-	  if (keymap_direction == widget_direction)
+	  if (keymap_direction == entry->resolved_dir)
 	    x1 = strong_x;
 	  else
 	    x1 = weak_x;
@@ -2884,20 +2929,16 @@ gtk_entry_draw_cursor (GtkEntry  *entry,
       cursor_location.width = 0;
       cursor_location.height = text_area_height - 2 * INNER_BORDER ;
 
-      gc = _gtk_get_insertion_cursor_gc (widget, TRUE);
-      _gtk_draw_insertion_cursor (widget, entry->text_area, gc,
-				  &cursor_location, dir1,
-                                  dir2 != GTK_TEXT_DIR_NONE);
-      g_object_unref (gc);
+      draw_insertion_cursor (entry,
+			     &cursor_location,
+			     dir1, TRUE, dir2 != PANGO_DIRECTION_NEUTRAL);
       
-      if (dir2 != GTK_TEXT_DIR_NONE)
+      if (dir2 != PANGO_DIRECTION_NEUTRAL)
 	{
 	  cursor_location.x = xoffset + x2;
-	  gc = _gtk_get_insertion_cursor_gc (widget, FALSE);
-	  _gtk_draw_insertion_cursor (widget, entry->text_area, gc,
-				      &cursor_location, dir2,
-                                      TRUE);
-	  g_object_unref (gc);
+	  draw_insertion_cursor (entry,
+				 &cursor_location,
+				 dir2, FALSE, TRUE);
 	}
     }
 }
@@ -3029,8 +3070,7 @@ gtk_entry_adjust_scroll (GtkEntry *entry
   pango_layout_line_get_extents (line, NULL, &logical_rect);
 
   /* Display as much text as we can */
-
-  if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
+  if (entry->resolved_dir == PANGO_DIRECTION_LTR)
     {
       min_offset = 0;
       max_offset = MAX (min_offset, logical_rect.width / PANGO_SCALE - text_area_width);
@@ -3115,11 +3155,9 @@ gtk_entry_move_visually (GtkEntry *entry
       else
 	{
 	  GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
-	  GtkTextDirection keymap_direction =
-	    (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
-	    GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
+	  PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
 
-	  strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
+	  strong = keymap_direction == entry->resolved_dir;
 	}
       
       if (count > 0)
Index: gtkentry.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkentry.h,v
retrieving revision 1.58
diff -u -p -r1.58 gtkentry.h
--- gtkentry.h	11 Jul 2003 12:51:24 -0000	1.58
+++ gtkentry.h	30 Jul 2003 18:36:24 -0000
@@ -93,6 +93,7 @@ struct _GtkEntry
   
   guint        select_words : 1;
   guint        select_lines : 1;
+  guint        resolved_dir : 4; /* PangoDirection */
   guint   button;
   guint   blink_timeout;
   guint   recompute_idle;
