]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtksizerequest.c
GtkEntry: Sanity check the end_pos value in _get_display_text()
[~andy/gtk] / gtk / gtksizerequest.c
index d0adb8b219e0a11d2011baf836e3394014fb6844..0ce86bd66fc95b849c98347619791f28898604b8 100644 (file)
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
-
-/**
- * SECTION:gtksizerequest
- * @Short_Description: Height-for-width geometry management
- * @Title: GtkSizeRequest
- *
- * The GtkSizeRequest interface is GTK+'s height-for-width (and width-for-height)
- * geometry management system. Height-for-width means that a widget can
- * change how much vertical space it needs, depending on the amount
- * of horizontal space that it is given (and similar for width-for-height).
- * The most common example is a label that reflows to fill up the available
- * width, wraps to fewer lines, and therefore needs less height.
- *
- * GTK+'s traditional two-pass <link linkend="size-allocation">size-allocation</link>
- * algorithm does not allow this flexibility. #GtkWidget provides a default
- * implementation of the #GtkSizeRequest interface for existing widgets,
- * which always requests the same height, regardless of the available width.
- *
- * <refsect2>
- * <title>Implementing GtkSizeRequest</title>
- * <para>
- * Some important things to keep in mind when implementing
- * the GtkSizeRequest interface and when using it in container
- * implementations.
- *
- * The geometry management system will query a logical hierarchy in
- * only one orientation at a time. When widgets are initially queried
- * for their minimum sizes it is generally done in a dual pass
- * in the direction chosen by the toplevel.
- *
- * For instance when queried in the normal height-for-width mode:
- * First the default minimum and natural width for each widget
- * in the interface will computed and collectively returned to
- * the toplevel by way of gtk_size_request_get_desired_width().
- * Next, the toplevel will use the minimum width to query for the
- * minimum height contextual to that width using
- * gtk_size_request_get_height_for_width(), which will also be a
- * highly recursive operation. This minimum-for-minimum size can be
- * used to set the minimum size constraint on the toplevel.
- *
- * When allocating, each container can use the minimum and natural
- * sizes reported by their children to allocate natural sizes and
- * expose as much content as possible with the given allocation.
- *
- * That means that the request operation at allocation time will
- * usually fire again in contexts of different allocated sizes than
- * the ones originally queried for. #GtkSizeRequest caches a
- * small number of results to avoid re-querying for the same
- * allocated size in one allocation cycle.
- *
- * A widget that does not actually do height-for-width
- * or width-for-height size negotiations only has to implement
- * get_width() and get_height().
- *
- * If a widget does move content around to smartly use up the
- * allocated size, then it must support the request properly in
- * both orientations; even if the request only makes sense in
- * one orientation.
- *
- * For instance, a GtkLabel that does height-for-width word wrapping
- * will not expect to have get_height() called because that
- * call is specific to a width-for-height request, in this case the
- * label must return the heights contextual to its minimum possible
- * width. By following this rule any widget that handles height-for-width
- * or width-for-height requests will always be allocated at least
- * enough space to fit its own content.
- * </para>
- * </refsect2>
- */
-
-
 #include <config.h>
-#include "gtksizerequest.h"
-#include "gtksizegroup.h"
-#include "gtkprivate.h"
-#include "gtkintl.h"
 
-/* With GtkSizeRequest, a widget may be requested
- * its width for 2 or maximum 3 heights in one resize
- */
-#define N_CACHED_SIZES 3
-
-typedef struct
-{
-  guint  age;
-  gint   for_size;
-  gint   minimum_size;
-  gint   natural_size;
-} SizeRequest;
+#include "gtksizerequest.h"
 
-typedef struct {
-  SizeRequest widths[N_CACHED_SIZES];
-  SizeRequest heights[N_CACHED_SIZES];
-  guint8      cached_width_age;
-  guint8      cached_height_age;
-} SizeRequestCache;
+#include "gtkdebug.h"
+#include "gtkintl.h"
+#include "gtkprivate.h"
+#include "gtksizegroup-private.h"
+#include "gtksizerequestcacheprivate.h"
+#include "gtkwidgetprivate.h"
+#include "deprecated/gtkstyle.h"
 
-static GQuark quark_cache = 0;
 
+#ifndef G_DISABLE_CHECKS
+static GQuark recursion_check_quark = 0;
+#endif /* G_DISABLE_CHECKS */
 
-GType
-gtk_size_request_get_type (void)
+static void
+push_recursion_check (GtkWidget       *widget,
+                      GtkOrientation   orientation,
+                      gint             for_size)
 {
-  static GType size_request_type = 0;
+#ifndef G_DISABLE_CHECKS
+  const char *previous_method;
+  const char *method;
 
-  if (G_UNLIKELY(!size_request_type))
-    {
-      size_request_type =
-        g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkSizeRequest"),
-                                       sizeof (GtkSizeRequestIface),
-                                       NULL, 0, NULL, 0);
+  if (recursion_check_quark == 0)
+    recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
 
-      g_type_interface_add_prerequisite (size_request_type, GTK_TYPE_WIDGET);
+  previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
 
-      quark_cache = g_quark_from_static_string ("gtk-size-request-cache");
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      method = for_size < 0 ? "get_width" : "get_width_for_height";
     }
-
-  return size_request_type;
-}
-
-/* looks for a cached size request for this for_size. If not
- * found, returns the oldest entry so it can be overwritten
- *
- * Note that this caching code was directly derived from
- * the Clutter toolkit.
- */
-static gboolean
-get_cached_size (gint           for_size,
-                SizeRequest   *cached_sizes,
-                SizeRequest  **result)
-{
-  guint i;
-
-  *result = &cached_sizes[0];
-
-  for (i = 0; i < N_CACHED_SIZES; i++)
+  else
     {
-      SizeRequest *cs;
-
-      cs = &cached_sizes[i];
+      method = for_size < 0 ? "get_height" : "get_height_for_width";
+    }
 
-      if (cs->age > 0 && cs->for_size == for_size)
-        {
-          *result = cs;
-          return TRUE;
-        }
-      else if (cs->age < (*result)->age)
-        {
-          *result = cs;
-        }
+  if (previous_method != NULL)
+    {
+      g_warning ("%s %p: widget tried to gtk_widget_%s inside "
+                 " GtkWidget     ::%s implementation. "
+                 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
+                 "directly rather than using gtk_widget_%s",
+                 G_OBJECT_TYPE_NAME (widget), widget,
+                 method, previous_method,
+                 method, method);
     }
 
-  return FALSE;
+  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
+#endif /* G_DISABLE_CHECKS */
 }
 
 static void
-destroy_cache (SizeRequestCache *cache)
+pop_recursion_check (GtkWidget       *widget,
+                     GtkOrientation   orientation)
 {
-  g_slice_free (SizeRequestCache, cache);
-}
-
-static SizeRequestCache *
-get_cache (GtkSizeRequest *widget,
-           gboolean        create)
-{
-  SizeRequestCache *cache;
-
-  cache = g_object_get_qdata (G_OBJECT (widget), quark_cache);
-  if (!cache && create)
-    {
-      cache = g_slice_new0 (SizeRequestCache);
-
-      cache->cached_width_age  = 1;
-      cache->cached_height_age = 1;
-
-      g_object_set_qdata_full (G_OBJECT (widget), quark_cache, cache,
-                               (GDestroyNotify)destroy_cache);
-    }
-
-  return cache;
+#ifndef G_DISABLE_CHECKS
+  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
+#endif
 }
 
-
-static void
-do_size_request (GtkWidget *widget)
+static const char *
+get_vfunc_name (GtkOrientation orientation,
+                gint           for_size)
 {
-  if (GTK_WIDGET_REQUEST_NEEDED (widget))
-    {
-      gtk_widget_ensure_style (widget);
-      GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
-      g_signal_emit_by_name (widget,
-                             "size-request",
-                             &widget->requisition);
-    }
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    return for_size < 0 ? "get_preferred_width" : "get_preferred_width_for_height";
+  else
+    return for_size < 0 ? "get_preferred_height" : "get_preferred_height_for_width";
 }
 
 static void
-compute_size_for_orientation (GtkSizeRequest    *request,
-                              GtkSizeGroupMode   orientation,
-                              gint               for_size,
-                              gint              *minimum_size,
-                              gint              *natural_size)
+gtk_widget_query_size_for_orientation (GtkWidget        *widget,
+                                       GtkOrientation    orientation,
+                                       gint              for_size,
+                                       gint             *minimum_size,
+                                       gint             *natural_size)
 {
   SizeRequestCache *cache;
-  SizeRequest      *cached_size;
-  GtkWidget        *widget;
-  gboolean          found_in_cache = FALSE;
-
-  g_return_if_fail (GTK_IS_SIZE_REQUEST (request));
-  g_return_if_fail (minimum_size != NULL || natural_size != NULL);
-
-  widget = GTK_WIDGET (request);
-  cache  = get_cache (request, TRUE);
-
-  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
-    {
-      cached_size = &cache->widths[0];
-
-      if (!GTK_WIDGET_WIDTH_REQUEST_NEEDED (request))
-        found_in_cache = get_cached_size (for_size, cache->widths, &cached_size);
-      else
-        {
-          memset (cache->widths, 0, N_CACHED_SIZES * sizeof (SizeRequest));
-          cache->cached_width_age = 1;
-        }
-    }
-  else
-    {
-      cached_size = &cache->heights[0];
-
-      if (!GTK_WIDGET_HEIGHT_REQUEST_NEEDED (request))
-        found_in_cache = get_cached_size (for_size, cache->heights, &cached_size);
-      else
-        {
-          memset (cache->heights, 0, N_CACHED_SIZES * sizeof (SizeRequest));
-          cache->cached_height_age = 1;
-        }
-    }
-
+  gint min_size = 0;
+  gint nat_size = 0;
+  gboolean found_in_cache;
+
+  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
+    for_size = -1;
+
+  cache = _gtk_widget_peek_request_cache (widget);
+  found_in_cache = _gtk_size_request_cache_lookup (cache,
+                                                   orientation,
+                                                   for_size,
+                                                   &min_size,
+                                                   &nat_size);
+  
   if (!found_in_cache)
     {
-      gint min_size = 0, nat_size = 0;
-      gint group_size, requisition_size;
+      gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
 
-      /* Unconditional size request runs but is often unhandled. */
-      do_size_request (widget);
+      gtk_widget_ensure_style (widget);
 
-      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
+      if (orientation == GTK_ORIENTATION_HORIZONTAL)
         {
-          requisition_size = widget->requisition.width;
-
           if (for_size < 0)
-            GTK_SIZE_REQUEST_GET_IFACE (request)->get_width (request, &min_size, &nat_size);
+            {
+             push_recursion_check (widget, orientation, for_size);
+              GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
+             pop_recursion_check (widget, orientation);
+            }
           else
-            GTK_SIZE_REQUEST_GET_IFACE (request)->get_width_for_height (request, for_size, 
-                                                                       &min_size, &nat_size);
+            {
+              gint ignored_position = 0;
+              gint minimum_height;
+              gint natural_height;
+
+             /* Pull the base natural height from the cache as it's needed to adjust
+              * the proposed 'for_size' */
+             gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
+
+              /* convert for_size to unadjusted height (for_size is a proposed allocation) */
+              GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
+                                                                     GTK_ORIENTATION_VERTICAL,
+                                                                     &minimum_height,
+                                                                    &natural_height,
+                                                                     &ignored_position,
+                                                                     &adjusted_for_size);
+
+             push_recursion_check (widget, orientation, for_size);
+              GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, 
+                                                                            MAX (adjusted_for_size, minimum_height),
+                                                                            &min_size, &nat_size);
+             pop_recursion_check (widget, orientation);
+            }
         }
       else
         {
-          requisition_size = widget->requisition.height;
-
           if (for_size < 0)
-            GTK_SIZE_REQUEST_GET_IFACE (request)->get_height (request, &min_size, &nat_size);
+            {
+             push_recursion_check (widget, orientation, for_size);
+              GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
+             pop_recursion_check (widget, orientation);
+            }
           else
-            GTK_SIZE_REQUEST_GET_IFACE (request)->get_height_for_width (request, for_size, 
-                                                                       &min_size, &nat_size);
+            {
+              gint ignored_position = 0;
+              gint minimum_width;
+              gint natural_width;
+
+             /* Pull the base natural width from the cache as it's needed to adjust
+              * the proposed 'for_size' */
+             gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
+
+              /* convert for_size to unadjusted width (for_size is a proposed allocation) */
+              GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
+                                                                     GTK_ORIENTATION_HORIZONTAL,
+                                                                    &minimum_width,
+                                                                     &natural_width,
+                                                                     &ignored_position,
+                                                                     &adjusted_for_size);
+
+             push_recursion_check (widget, orientation, for_size);
+              GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, 
+                                                                            MAX (adjusted_for_size, minimum_width),
+                                                                            &min_size, &nat_size);
+             pop_recursion_check (widget, orientation);
+            }
         }
 
-      /* Support for dangling "size-request" signals and forward derived
-       * classes that will not default to a ->get_width() that
-       * returns the values in the ->requisition cache.
-       */
-      min_size = MAX (min_size, requisition_size);
-      nat_size = MAX (nat_size, requisition_size);
+      if (min_size > nat_size)
+        {
+          g_warning ("%s %p reported min size %d and natural size %d in %s(); natural size must be >= min size",
+                     G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size, get_vfunc_name (orientation, for_size));
+        }
 
-      cached_size->minimum_size = min_size;
-      cached_size->natural_size = nat_size;
-      cached_size->for_size     = for_size;
+      adjusted_min     = min_size;
+      adjusted_natural = nat_size;
+      GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
+                                                          orientation,
+                                                          &adjusted_min,
+                                                          &adjusted_natural);
 
-      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
+      if (adjusted_min < min_size ||
+          adjusted_natural < nat_size)
         {
-          cached_size->age = cache->cached_width_age;
-          cache->cached_width_age++;
-
-          GTK_PRIVATE_UNSET_FLAG (request, GTK_WIDTH_REQUEST_NEEDED);
+          g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
+                     G_OBJECT_TYPE_NAME (widget), widget,
+                     orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
+                     adjusted_min, adjusted_natural,
+                     min_size, nat_size);
+          /* don't use the adjustment */
+        }
+      else if (adjusted_min > adjusted_natural)
+        {
+          g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
+                     G_OBJECT_TYPE_NAME (widget), widget,
+                     orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
+                     adjusted_min, adjusted_natural,
+                     min_size, nat_size);
+          /* don't use the adjustment */
         }
       else
         {
-          cached_size->age = cache->cached_height_age;
-          cache->cached_height_age++;
-
-          GTK_PRIVATE_UNSET_FLAG (request, GTK_HEIGHT_REQUEST_NEEDED);
+          /* adjustment looks good */
+          min_size = adjusted_min;
+          nat_size = adjusted_natural;
         }
 
-      /* Get size groups to compute the base requisition once one
-       * of the values have been cached, then go ahead and update
-       * the cache with the sizegroup computed value.
-       *
-       * Note this is also where values from gtk_widget_set_size_request()
-       * are considered.
-       */
-      group_size =
-        _gtk_size_group_bump_requisition (GTK_WIDGET (request),
-                                          orientation, cached_size->minimum_size);
-
-      cached_size->minimum_size = MAX (cached_size->minimum_size, group_size);
-      cached_size->natural_size = MAX (cached_size->natural_size, group_size);
+      _gtk_size_request_cache_commit (cache,
+                                      orientation,
+                                      for_size,
+                                      min_size,
+                                      nat_size);
     }
 
   if (minimum_size)
-    *minimum_size = cached_size->minimum_size;
+    *minimum_size = min_size;
 
   if (natural_size)
-    *natural_size = cached_size->natural_size;
+    *natural_size = nat_size;
 
-  g_assert (cached_size->minimum_size <= cached_size->natural_size);
+  g_assert (min_size <= nat_size);
 
   GTK_NOTE (SIZE_REQUEST,
             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
-                     request, G_OBJECT_TYPE_NAME (request),
-                     orientation == GTK_SIZE_GROUP_HORIZONTAL ?
+                     widget, G_OBJECT_TYPE_NAME (widget),
+                     orientation == GTK_ORIENTATION_HORIZONTAL ?
                      "width for height" : "height for width" ,
-                     for_size,
-                     cached_size->minimum_size,
-                     cached_size->natural_size,
+                     for_size, min_size, nat_size,
                      found_in_cache ? "yes" : "no"));
+}
+
+/* This is the main function that checks for a cached size and
+ * possibly queries the widget class to compute the size if it's
+ * not cached. If the for_size here is -1, then get_preferred_width()
+ * or get_preferred_height() will be used.
+ */
+void
+_gtk_widget_compute_size_for_orientation (GtkWidget        *widget,
+                                          GtkOrientation    orientation,
+                                          gint              for_size,
+                                          gint             *minimum,
+                                          gint             *natural)
+{
+  GHashTable *widgets;
+  GHashTableIter iter;
+  gpointer key;
+  gint    min_result = 0, nat_result = 0;
+
+  if (!gtk_widget_get_visible (widget) && !gtk_widget_is_toplevel (widget))
+    {
+      if (minimum)
+        *minimum = 0;
+      if (natural)
+        *natural = 0;
+      return;
+    }
 
+  if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
+    {
+      gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural);
+      return;
+    }
+
+  widgets = _gtk_size_group_get_widget_peers (widget, orientation);
+
+  g_hash_table_foreach (widgets, (GHFunc) g_object_ref, NULL);
+  
+  g_hash_table_iter_init (&iter, widgets);
+  while (g_hash_table_iter_next (&iter, &key, NULL))
+    {
+      GtkWidget *tmp_widget = key;
+      gint min_dimension, nat_dimension;
+
+      gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size, &min_dimension, &nat_dimension);
+
+      min_result = MAX (min_result, min_dimension);
+      nat_result = MAX (nat_result, nat_dimension);
+    }
+
+  g_hash_table_foreach (widgets, (GHFunc) g_object_unref, NULL);
+
+  g_hash_table_destroy (widgets);
+
+  if (minimum)
+    *minimum = min_result;
+
+  if (natural)
+    *natural = nat_result;
 }
 
 /**
- * gtk_size_request_is_height_for_width:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_request_mode:
+ * @widget: a #GtkWidget instance
  *
  * Gets whether the widget prefers a height-for-width layout
  * or a width-for-height layout.
@@ -362,29 +322,31 @@ compute_size_for_orientation (GtkSizeRequest    *request,
  * context of their children or in context of their allocation
  * capabilities.</para></note>
  *
- * Returns: %TRUE if the widget prefers height-for-width, %FALSE if
- * the widget should be treated with a width-for-height preference.
+ * Returns: The #GtkSizeRequestMode preferred by @widget.
  *
  * Since: 3.0
  */
 GtkSizeRequestMode
-gtk_size_request_get_request_mode (GtkSizeRequest *widget)
+gtk_widget_get_request_mode (GtkWidget *widget)
 {
-  GtkSizeRequestIface *iface;
+  SizeRequestCache *cache;
 
-  g_return_val_if_fail (GTK_IS_SIZE_REQUEST (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
 
-  iface = GTK_SIZE_REQUEST_GET_IFACE (widget);
-  if (iface->get_request_mode)
-    return iface->get_request_mode (widget);
+  cache = _gtk_widget_peek_request_cache (widget);
 
-  /* By default widgets are height-for-width. */
-  return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
+  if (!cache->request_mode_valid)
+    {
+      cache->request_mode = GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
+      cache->request_mode_valid = TRUE;
+    }
+
+  return cache->request_mode;
 }
 
 /**
- * gtk_size_request_get_width:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_preferred_width:
+ * @widget: a #GtkWidget instance
  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
  *
@@ -393,21 +355,33 @@ gtk_size_request_get_request_mode (GtkSizeRequest *widget)
  * <note><para>This call is specific to height-for-width
  * requests.</para></note>
  *
+ * The returned request will be modified by the
+ * GtkWidgetClass::adjust_size_request virtual method and by any
+ * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
+ * is the one that should be used for layout, not necessarily the one
+ * returned by the widget itself.
+ *
  * Since: 3.0
  */
 void
-gtk_size_request_get_width (GtkSizeRequest *widget,
-                                       gint              *minimum_width,
-                                       gint              *natural_width)
+gtk_widget_get_preferred_width (GtkWidget *widget,
+                                gint      *minimum_width,
+                                gint      *natural_width)
 {
-  compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
-                                -1, minimum_width, natural_width);
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (minimum_width != NULL || natural_width != NULL);
+
+  _gtk_widget_compute_size_for_orientation (widget,
+                                            GTK_ORIENTATION_HORIZONTAL,
+                                            -1,
+                                            minimum_width,
+                                            natural_width);
 }
 
 
 /**
- * gtk_size_request_get_height:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_preferred_height:
+ * @widget: a #GtkWidget instance
  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
  *
@@ -415,121 +389,269 @@ gtk_size_request_get_width (GtkSizeRequest *widget,
  *
  * <note><para>This call is specific to width-for-height requests.</para></note>
  *
+ * The returned request will be modified by the
+ * GtkWidgetClass::adjust_size_request virtual method and by any
+ * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
+ * is the one that should be used for layout, not necessarily the one
+ * returned by the widget itself.
+ *
  * Since: 3.0
  */
 void
-gtk_size_request_get_height (GtkSizeRequest *widget,
-                            gint           *minimum_height,
-                            gint           *natural_height)
+gtk_widget_get_preferred_height (GtkWidget *widget,
+                                 gint      *minimum_height,
+                                 gint      *natural_height)
 {
-  compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
-                                -1, minimum_height, natural_height);
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (minimum_height != NULL || natural_height != NULL);
+
+  _gtk_widget_compute_size_for_orientation (widget,
+                                            GTK_ORIENTATION_VERTICAL,
+                                            -1,
+                                            minimum_height,
+                                            natural_height);
 }
 
 
 
 /**
- * gtk_size_request_get_width_for_height:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_preferred_width_for_height:
+ * @widget: a #GtkWidget instance
  * @height: the height which is available for allocation
  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
  *
- * Retrieves a widget's desired width if it would be given
+ * Retrieves a widget's minimum and natural width if it would be given
  * the specified @height.
  *
+ * The returned request will be modified by the
+ * GtkWidgetClass::adjust_size_request virtual method and by any
+ * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
+ * is the one that should be used for layout, not necessarily the one
+ * returned by the widget itself.
+ *
  * Since: 3.0
  */
 void
-gtk_size_request_get_width_for_height (GtkSizeRequest *widget,
-                                      gint            height,
-                                      gint           *minimum_width,
-                                      gint           *natural_width)
+gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
+                                           gint       height,
+                                           gint      *minimum_width,
+                                           gint      *natural_width)
 {
-  compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
-                                height, minimum_width, natural_width);
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (minimum_width != NULL || natural_width != NULL);
+  g_return_if_fail (height >= 0);
+
+  _gtk_widget_compute_size_for_orientation (widget,
+                                            GTK_ORIENTATION_HORIZONTAL,
+                                            height,
+                                            minimum_width,
+                                            natural_width);
 }
 
 /**
- * gtk_size_request_get_height_for_width:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_preferred_height_for_width:
+ * @widget: a #GtkWidget instance
  * @width: the width which is available for allocation
  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
  *
- * Retrieves a widget's desired height if it would be given
+ * Retrieves a widget's minimum and natural height if it would be given
  * the specified @width.
  *
+ * The returned request will be modified by the
+ * GtkWidgetClass::adjust_size_request virtual method and by any
+ * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
+ * is the one that should be used for layout, not necessarily the one
+ * returned by the widget itself.
+ *
  * Since: 3.0
  */
 void
-gtk_size_request_get_height_for_width (GtkSizeRequest *widget,
-                                      gint            width,
-                                      gint           *minimum_height,
-                                      gint           *natural_height)
+gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
+                                           gint       width,
+                                           gint      *minimum_height,
+                                           gint      *natural_height)
 {
-  compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
-                                width, minimum_height, natural_height);
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (minimum_height != NULL || natural_height != NULL);
+  g_return_if_fail (width >= 0);
+
+  _gtk_widget_compute_size_for_orientation (widget,
+                                            GTK_ORIENTATION_VERTICAL,
+                                            width,
+                                            minimum_height,
+                                            natural_height);
 }
 
 /**
- * gtk_size_request_get_size:
- * @widget: a #GtkSizeRequest instance
+ * gtk_widget_get_preferred_size:
+ * @widget: a #GtkWidget instance
  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
  *
- * Retrieves the minimum and natural size of a widget taking
+ * Retrieves the minimum and natural size of a widget, taking
  * into account the widget's preference for height-for-width management.
  *
  * This is used to retrieve a suitable size by container widgets which do
- * not impose any restrictions on the child placement.
+ * not impose any restrictions on the child placement. It can be used
+ * to deduce toplevel window and menu sizes as well as child widgets in
+ * free-form containers such as GtkLayout.
+ *
+ * <note><para>Handle with care. Note that the natural height of a height-for-width
+ * widget will generally be a smaller size than the minimum height, since the required
+ * height for the natural width is generally smaller than the required height for
+ * the minimum width.</para></note>
  *
  * Since: 3.0
  */
 void
-gtk_size_request_get_size (GtkSizeRequest    *widget,
-                          GtkRequisition    *minimum_size,
-                          GtkRequisition    *natural_size)
+gtk_widget_get_preferred_size (GtkWidget      *widget,
+                               GtkRequisition *minimum_size,
+                               GtkRequisition *natural_size)
 {
   gint min_width, nat_width;
   gint min_height, nat_height;
 
-  g_return_if_fail (GTK_IS_SIZE_REQUEST (widget));
+  g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  if (gtk_size_request_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
+  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
     {
-      gtk_size_request_get_width (widget, &min_width, &nat_width);
+      gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
 
       if (minimum_size)
        {
          minimum_size->width = min_width;
-         gtk_size_request_get_height_for_width (widget, min_width,
-                                                &minimum_size->height, NULL);
+         gtk_widget_get_preferred_height_for_width (widget, min_width,
+                                                     &minimum_size->height, NULL);
        }
 
       if (natural_size)
        {
          natural_size->width = nat_width;
-         gtk_size_request_get_height_for_width (widget, nat_width,
-                                                NULL, &natural_size->height);
+         gtk_widget_get_preferred_height_for_width (widget, nat_width,
+                                                     NULL, &natural_size->height);
        }
     }
-  else
+  else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
     {
-      gtk_size_request_get_height (widget, &min_height, &nat_height);
+      gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
 
       if (minimum_size)
        {
          minimum_size->height = min_height;
-         gtk_size_request_get_width_for_height (widget, min_height,
-                                                &minimum_size->width, NULL);
+         gtk_widget_get_preferred_width_for_height (widget, min_height,
+                                                     &minimum_size->width, NULL);
        }
 
       if (natural_size)
        {
          natural_size->height = nat_height;
-         gtk_size_request_get_width_for_height (widget, nat_height,
-                                                NULL, &natural_size->width);
+         gtk_widget_get_preferred_width_for_height (widget, nat_height,
+                                                     NULL, &natural_size->width);
        }
     }
 }
+
+
+static gint
+compare_gap (gconstpointer p1,
+            gconstpointer p2,
+            gpointer      data)
+{
+  GtkRequestedSize *sizes = data;
+  const guint *c1 = p1;
+  const guint *c2 = p2;
+
+  const gint d1 = MAX (sizes[*c1].natural_size -
+                       sizes[*c1].minimum_size,
+                       0);
+  const gint d2 = MAX (sizes[*c2].natural_size -
+                       sizes[*c2].minimum_size,
+                       0);
+
+  gint delta = (d2 - d1);
+
+  if (0 == delta)
+    delta = (*c2 - *c1);
+
+  return delta;
+}
+
+/**
+ * gtk_distribute_natural_allocation:
+ * @extra_space: Extra space to redistribute among children after subtracting
+ *               minimum sizes and any child padding from the overall allocation
+ * @n_requested_sizes: Number of requests to fit into the allocation
+ * @sizes: An array of structs with a client pointer and a minimum/natural size
+ *         in the orientation of the allocation.
+ *
+ * Distributes @extra_space to child @sizes by bringing smaller
+ * children up to natural size first.
+ *
+ * The remaining space will be added to the @minimum_size member of the
+ * GtkRequestedSize struct. If all sizes reach their natural size then
+ * the remaining space is returned.
+ *
+ * Returns: The remainder of @extra_space after redistributing space
+ * to @sizes.
+ */
+gint
+gtk_distribute_natural_allocation (gint              extra_space,
+                                  guint             n_requested_sizes,
+                                  GtkRequestedSize *sizes)
+{
+  guint *spreading;
+  gint   i;
+
+  g_return_val_if_fail (extra_space >= 0, 0);
+
+  spreading = g_newa (guint, n_requested_sizes);
+
+  for (i = 0; i < n_requested_sizes; i++)
+    spreading[i] = i;
+
+  /* Distribute the container's extra space c_gap. We want to assign
+   * this space such that the sum of extra space assigned to children
+   * (c^i_gap) is equal to c_cap. The case that there's not enough
+   * space for all children to take their natural size needs some
+   * attention. The goals we want to achieve are:
+   *
+   *   a) Maximize number of children taking their natural size.
+   *   b) The allocated size of children should be a continuous
+   *   function of c_gap.  That is, increasing the container size by
+   *   one pixel should never make drastic changes in the distribution.
+   *   c) If child i takes its natural size and child j doesn't,
+   *   child j should have received at least as much gap as child i.
+   *
+   * The following code distributes the additional space by following
+   * these rules.
+   */
+
+  /* Sort descending by gap and position. */
+  g_qsort_with_data (spreading,
+                    n_requested_sizes, sizeof (guint),
+                    compare_gap, sizes);
+
+  /* Distribute available space.
+   * This master piece of a loop was conceived by Behdad Esfahbod.
+   */
+  for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
+    {
+      /* Divide remaining space by number of remaining children.
+       * Sort order and reducing remaining space by assigned space
+       * ensures that space is distributed equally.
+       */
+      gint glue = (extra_space + i) / (i + 1);
+      gint gap = sizes[(spreading[i])].natural_size
+       - sizes[(spreading[i])].minimum_size;
+
+      gint extra = MIN (glue, gap);
+
+      sizes[spreading[i]].minimum_size += extra;
+
+      extra_space -= extra;
+    }
+
+  return extra_space;
+}