Index: gtklabel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklabel.c,v
retrieving revision 1.139
diff -u -p -r1.139 gtklabel.c
--- gtklabel.c	31 Jan 2003 00:08:33 -0000	1.139
+++ gtklabel.c	30 Jul 2003 19:52:29 -0000
@@ -1415,8 +1415,31 @@ gtk_label_ensure_layout (GtkLabel *label
 
   if (!label->layout)
     {
+      PangoContext *context = gtk_widget_get_pango_context (widget);
       PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
-
+      PangoDirection dir = pango_find_base_dir (label->text, -1);
+      gboolean flip_dir = FALSE;
+      
+      if (dir == PANGO_DIRECTION_RTL)
+	{
+	  flip_dir = TRUE;
+	  pango_context_set_base_dir (context, PANGO_DIRECTION_RTL);
+	}
+      else if (dir == PANGO_DIRECTION_LTR)
+	{
+	  pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
+	}
+      else
+	{
+	  if (gtk_widget_get_direction (GTK_WIDGET (label) == GTK_TEXT_DIR_LTR))
+	    dir = PANGO_DIRECTION_LTR;
+	  else
+	    dir = PANGO_DIRECTION_RTL;
+	}
+      
+      label->resolved_dir = dir;
+      pango_context_set_base_dir (context, dir);
+  
       label->layout = gtk_widget_create_pango_layout (widget, label->text);
 
       if (label->effective_attrs)
@@ -1425,17 +1448,26 @@ gtk_label_ensure_layout (GtkLabel *label
       switch (label->jtype)
 	{
 	case GTK_JUSTIFY_LEFT:
-	  align = PANGO_ALIGN_LEFT;
+	  if (flip_dir)
+	    align = PANGO_ALIGN_RIGHT;
+	  else
+	    align = PANGO_ALIGN_LEFT;
 	  break;
 	case GTK_JUSTIFY_RIGHT:
-	  align = PANGO_ALIGN_RIGHT;
+	  if (flip_dir)
+	    align = PANGO_ALIGN_LEFT;
+	  else
+	    align = PANGO_ALIGN_RIGHT;
 	  break;
 	case GTK_JUSTIFY_CENTER:
 	  align = PANGO_ALIGN_CENTER;
 	  break;
 	case GTK_JUSTIFY_FILL:
 	  /* FIXME: This just doesn't work to do this */
-	  align = PANGO_ALIGN_LEFT;
+	  if (flip_dir)
+	    align = PANGO_ALIGN_RIGHT;
+	  else
+	    align = PANGO_ALIGN_LEFT;
 	  pango_layout_set_justify (label->layout, TRUE);
 	  break;
 	default:
@@ -1670,7 +1702,7 @@ get_layout_location (GtkLabel  *label,
   misc = GTK_MISC (label);
   widget = GTK_WIDGET (label);
   
-  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
+  if (label->resolved_dir == PANGO_DIRECTION_LTR)
     xalign = misc->xalign;
   else
     xalign = 1.0 - misc->xalign;
@@ -1679,7 +1711,7 @@ get_layout_location (GtkLabel  *label,
 	     xalign * (widget->allocation.width - widget->requisition.width)
 	     + 0.5);
 
-  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
+  if (label->resolved_dir == PANGO_DIRECTION_LTR)
     x = MAX (x, widget->allocation.x + misc->xpad);
   else
     x = MIN (x,
@@ -1698,6 +1730,30 @@ get_layout_location (GtkLabel  *label,
 }
 
 static void
+draw_insertion_cursor (GtkLabel      *label,
+		       GdkRectangle  *cursor_location,
+		       PangoDirection direction,
+		       gboolean       is_primary,
+		       gboolean       draw_arrow)
+{
+  GtkWidget *widget = GTK_WIDGET (label);
+  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, widget->window, gc,
+			      cursor_location,
+			      text_dir,
+			      draw_arrow);
+  g_object_unref (gc);
+}
+
+static void
 gtk_label_draw_cursor (GtkLabel  *label, gint xoffset, gint yoffset)
 {
   if (label->select_info == NULL)
@@ -1707,22 +1763,13 @@ gtk_label_draw_cursor (GtkLabel  *label,
     {
       GtkWidget *widget = GTK_WIDGET (label);
 
-      GtkTextDirection keymap_direction;
-      GtkTextDirection widget_direction;
       PangoRectangle strong_pos, weak_pos;
       gboolean split_cursor;
       PangoRectangle *cursor1 = NULL;
       PangoRectangle *cursor2 = NULL;
       GdkRectangle cursor_location;
-      GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
-      GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
-      GdkGC *gc;
-
-      keymap_direction =
-	(gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget))) == PANGO_DIRECTION_LTR) ?
-	GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
-
-      widget_direction = gtk_widget_get_direction (widget);
+      PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
+      PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
 
       gtk_label_ensure_layout (label);
       
@@ -1733,22 +1780,26 @@ gtk_label_draw_cursor (GtkLabel  *label,
 		    "gtk-split-cursor", &split_cursor,
 		    NULL);
 
-      dir1 = widget_direction;
-      
       if (split_cursor)
 	{
+	  dir1 = label->resolved_dir;
 	  cursor1 = &strong_pos;
 
 	  if (strong_pos.x != weak_pos.x ||
 	      strong_pos.y != weak_pos.y)
 	    {
-	      dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
+              dir2 = (label->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
 	      cursor2 = &weak_pos;
 	    }
 	}
       else
 	{
-	  if (keymap_direction == widget_direction)
+	  GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
+	  PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
+	  
+	  dir1 = keymap_direction;
+
+	  if (keymap_direction == label->resolved_dir)
 	    cursor1 = &strong_pos;
 	  else
 	    cursor1 = &weak_pos;
@@ -1759,23 +1810,20 @@ gtk_label_draw_cursor (GtkLabel  *label,
       cursor_location.width = 0;
       cursor_location.height = PANGO_PIXELS (cursor1->height);
 
-      gc = _gtk_get_insertion_cursor_gc (widget, TRUE);
-      _gtk_draw_insertion_cursor (widget, widget->window, gc,
-				  &cursor_location, dir1,
-                                  dir2 != GTK_TEXT_DIR_NONE);
-      g_object_unref (gc);
+      draw_insertion_cursor (label,
+			     &cursor_location,
+			     dir1, TRUE, dir2 != PANGO_DIRECTION_NEUTRAL);
       
-      if (dir2 != GTK_TEXT_DIR_NONE)
+      if (dir2 != PANGO_DIRECTION_NEUTRAL)
 	{
 	  cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
 	  cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
 	  cursor_location.width = 0;
 	  cursor_location.height = PANGO_PIXELS (cursor2->height);
 
-	  gc = _gtk_get_insertion_cursor_gc (widget, FALSE);
-	  _gtk_draw_insertion_cursor (widget, widget->window, gc,
-				      &cursor_location, dir2, TRUE);
-	  g_object_unref (gc);
+	  draw_insertion_cursor (label,
+				 &cursor_location,
+				 dir2, FALSE, TRUE);
 	}
     }
 }
@@ -2784,10 +2832,7 @@ get_better_cursor (GtkLabel *label,
 		   gint      *y)
 {
   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
-  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 (label));
+  PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
   gboolean split_cursor;
   PangoRectangle strong_pos, weak_pos;
   
@@ -2807,7 +2852,7 @@ get_better_cursor (GtkLabel *label,
     }
   else
     {
-      if (keymap_direction == widget_direction)
+      if (keymap_direction == label->resolved_dir)
 	{
 	  *x = strong_pos.x / PANGO_SCALE;
 	  *y = strong_pos.y / PANGO_SCALE;
@@ -2875,13 +2920,14 @@ gtk_label_move_visually (GtkLabel *label
   
   while (count != 0)
     {
+      GtkWidget *widget= GTK_WIDGET (label);
       int new_index, new_trailing;
       gboolean split_cursor;
       gboolean strong;
 
       gtk_label_ensure_layout (label);
 
-      g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
+      g_object_get (gtk_widget_get_settings (widget),
 		    "gtk-split-cursor", &split_cursor,
 		    NULL);
 
@@ -2889,12 +2935,10 @@ gtk_label_move_visually (GtkLabel *label
 	strong = TRUE;
       else
 	{
-	  GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
-	  GtkTextDirection keymap_direction =
-	    (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
-	    GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
+	  GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
+	  PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
 
-	  strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (label));
+	  strong = keymap_direction == label->resolved_dir;
 	}
       
       if (count > 0)
Index: gtklabel.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklabel.h,v
retrieving revision 1.38
diff -u -p -r1.38 gtklabel.h
--- gtklabel.h	5 Oct 2002 23:15:36 -0000	1.38
+++ gtklabel.h	30 Jul 2003 19:52:29 -0000
@@ -60,6 +60,7 @@ struct _GtkLabel
   guint   wrap : 1;
   guint   use_underline : 1;
   guint   use_markup : 1;
+  guint   resolved_dir : 4; /* PangoDirection */
 
   guint   mnemonic_keyval;
   
