2008-09-04  Paolo Bonzini  <bonzini@gnu.org>

	* gtk/gtklabel.c: Set priv->wrap_width in the extended-layout
	height_for_width code.  Preserve it in get_natural_size
	because when label->wrap is set, gtk_label_size_request will
	invalidate the layout and hence modify priv->wrap_width; for
	the same reason, don't bother setting the layout's width to -1
	if the label is wrapping.  Add priv->wrap_at_container_size
	so that changing the style (e.g.  upon realization) does not
	invalidate the layout.
---
 ChangeLog.gtk-extended-layout |   11 +++++++++
 gtk/gtklabel.c                |   48 ++++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 2082d6a..fac4b7f 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -60,7 +60,9 @@ typedef struct
   gint width_chars;
   gint max_width_chars;
   gint baseline_offset;
-  gboolean full_size;
+
+  guint full_size : 1;
+  guint wrap_at_container_size : 1;
 }
 GtkLabelPrivate;
 
@@ -826,6 +828,8 @@ gtk_label_init (GtkLabel *label)
   priv->width_chars = -1;
   priv->max_width_chars = -1;
   priv->wrap_width = -1;
+  priv->wrap_at_container_size = FALSE;
+
   label->label = NULL;
 
   label->jtype = GTK_JUSTIFY_LEFT;
@@ -1987,6 +1991,7 @@ gtk_label_invalidate_wrap_width (GtkLabel *label)
   priv = GTK_LABEL_GET_PRIVATE (label);
 
   priv->wrap_width = -1;
+  priv->wrap_at_container_size = FALSE;
 }
 
 static gint
@@ -2366,6 +2371,7 @@ gtk_label_style_set (GtkWidget *widget,
 		     GtkStyle  *previous_style)
 {
   GtkLabel *label;
+  GtkLabelPrivate *priv;
   
   g_return_if_fail (GTK_IS_LABEL (widget));
   
@@ -2373,7 +2379,10 @@ gtk_label_style_set (GtkWidget *widget,
 
   /* We have to clear the layout, fonts etc. may have changed */
   gtk_label_clear_layout (label);
-  gtk_label_invalidate_wrap_width (label);
+
+  priv = GTK_LABEL_GET_PRIVATE (label);
+  if (!priv->wrap_at_container_size)
+    gtk_label_invalidate_wrap_width (label);
 }
 
 static void 
@@ -4379,19 +4388,26 @@ static gint
 gtk_label_extended_layout_get_size_for_allocation (GtkExtendedLayout *layout,
                                                    gint               size)
 {
-  GtkLabel *label = GTK_LABEL (layout);
-  gdouble angle = gtk_label_get_angle (label);
+  GtkLabel *label;
+  GtkLabelPrivate *priv;
+  gdouble angle;
   PangoLayout *tmp;
 
-  // this test is slightly more tolerant than the get_features test
+  label = GTK_LABEL (layout);
+  priv = GTK_LABEL_GET_PRIVATE (label);
+
+  /* This test is valid for both width_for_height, and height_for_width.  */
+  angle = gtk_label_get_angle (label);
   g_assert (0 == (((gint)angle) % 90));
 
   gtk_label_ensure_layout (label);
   tmp = pango_layout_copy (label->layout);
   pango_layout_set_width (tmp, PANGO_SCALE * size);
+  priv->wrap_width = PANGO_SCALE * size;
+  priv->wrap_at_container_size = TRUE;
+
   pango_layout_get_pixel_size (tmp, NULL, &size);
   g_object_unref (tmp);
-
   return size;
 }
 
@@ -4400,24 +4416,40 @@ gtk_label_extended_layout_get_natural_size (GtkExtendedLayout *layout,
                                             GtkRequisition    *requisition)
 {
   GtkLabel *label;
+  GtkLabelPrivate *priv;
   gboolean ellipsize;
+  gint wrap_width;
   PangoLayout *tmp;
 
   label = GTK_LABEL (layout);
+  priv = GTK_LABEL_GET_PRIVATE (label);
   gtk_label_ensure_layout (label);
 
   ellipsize = label->ellipsize;
   label->ellipsize = PANGO_ELLIPSIZE_NONE;
 
+  wrap_width = priv->wrap_width;
   tmp = label->layout;
-  label->layout = pango_layout_copy (tmp);
 
-  pango_layout_set_width (label->layout, -1);
+  /* See comment in gtk_label_size_request.  */
+
+  if (label->wrap)
+    {
+      g_object_ref (label->layout);
+      gtk_label_clear_layout (label);
+    }
+  else
+    {
+      label->layout = pango_layout_copy (tmp);
+      pango_layout_set_width (label->layout, -1);
+    }
+
   gtk_label_size_request (GTK_WIDGET (label), requisition);
 
   g_object_unref (label->layout);
   label->ellipsize = ellipsize;
   label->layout = tmp;
+  priv->wrap_width = wrap_width;
 }
 
 static gint
-- 
1.5.5

