gtksizerequest.c 29 KB
Newer Older
1
/* gtksizerequest.c
2
 * Copyright (C) 2007-2010 Openismus GmbH
3
 *
4
 * Authors:
5
 *      Mathias Hasselmann <mathias@openismus.com>
6
 *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21 22
 */

#include <config.h>
Emmanuele Bassi's avatar
Emmanuele Bassi committed
23

24
#include "gtksizerequest.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
25

26
#include "gtkdebug.h"
27
#include "gtkintl.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
28 29 30
#include "gtkprivate.h"
#include "gtksizegroup-private.h"
#include "gtkwidgetprivate.h"
31
#include "deprecated/gtkstyle.h"
32

33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#ifndef G_DISABLE_CHECKS
static GQuark recursion_check_quark = 0;
#endif /* G_DISABLE_CHECKS */

static void
push_recursion_check (GtkWidget       *widget,
                      GtkSizeGroupMode orientation,
                      gint             for_size)
{
#ifndef G_DISABLE_CHECKS
  const char *previous_method;
  const char *method;

  if (recursion_check_quark == 0)
    recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");

  previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);

  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
    {
      method = for_size < 0 ? "get_width" : "get_width_for_height";
    }
  else
    {
      method = for_size < 0 ? "get_height" : "get_height_for_width";
    }

  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);
    }

  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
#endif /* G_DISABLE_CHECKS */
}

static void
pop_recursion_check (GtkWidget       *widget,
                     GtkSizeGroupMode orientation)
{
#ifndef G_DISABLE_CHECKS
  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
#endif
}


static void
clear_cache (SizeRequestCache   *cache,
	     GtkSizeGroupMode    orientation)
{
  SizeRequest **sizes;
  gint          i;

  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
    {
      sizes = cache->widths;

      cache->widths            = NULL;
      cache->cached_widths     = 0;
      cache->last_cached_width = 0;
      cache->cached_base_width = FALSE;
    }
  else
    {
      sizes = cache->heights;

      cache->heights            = NULL;
      cache->cached_heights     = 0;
      cache->last_cached_height = 0; 
      cache->cached_base_height = FALSE;
   }

  if (sizes)
    {
114
      for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES && sizes[i] != NULL; i++)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	g_slice_free (SizeRequest, sizes[i]);
      
      g_slice_free1 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES, sizes);
    }
}

void
_gtk_widget_free_cached_sizes (GtkWidget *widget)
{
  SizeRequestCache   *cache;

  cache = _gtk_widget_peek_request_cache (widget);

  clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
  clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);
}
131 132 133 134 135 136

/* This function checks if 'request_needed' flag is present
 * and resets the cache state if a request is needed for
 * a given orientation.
 */
static SizeRequestCache *
137
init_cache (GtkWidget        *widget)
138
{
139
  SizeRequestCache *cache;
140 141 142

  cache = _gtk_widget_peek_request_cache (widget);

143 144
  if (_gtk_widget_get_width_request_needed (widget))
    clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
145 146
  
  if (_gtk_widget_get_height_request_needed (widget))
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);

  return cache;
}

/* 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 originally derived from
 * the Clutter toolkit but has evolved for other GTK+ requirements.
 */
static gboolean
get_cached_size (GtkWidget         *widget,
		 GtkSizeGroupMode   orientation,
		 gint               for_size,
		 CachedSize       **result)
{
  SizeRequestCache  *cache;
  SizeRequest      **cached_sizes;
  guint              i, n_sizes;

  cache = init_cache (widget);
169

170 171 172
  if (for_size < 0)
    {
      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
173
	{
174 175
	  *result = &cache->cached_width;
	  return cache->cached_base_width;
176 177 178
	}
      else
	{
179 180
	  *result = &cache->cached_height;
	  return cache->cached_base_height;
181
	}
182
    }
183

184 185 186 187
  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
    {
      cached_sizes = cache->widths;
      n_sizes      = cache->cached_widths;
188
    }
189
  else
190
    {
191 192 193
      cached_sizes = cache->heights;
      n_sizes      = cache->cached_heights;
    }
194

195 196 197 198 199
  /* Search for an already cached size */
  for (i = 0; i < n_sizes; i++)
    {
      if (cached_sizes[i]->lower_for_size <= for_size &&
	  cached_sizes[i]->upper_for_size >= for_size)
200
	{
201 202
	  *result = &cached_sizes[i]->cached_size;
	  return TRUE;
203 204 205
	}
    }

206
  return FALSE;
207 208
}

209 210 211 212 213 214
static void
commit_cached_size (GtkWidget         *widget,
		    GtkSizeGroupMode   orientation,
		    gint               for_size,
		    gint               minimum_size,
		    gint               natural_size)
215
{
216 217 218
  SizeRequestCache  *cache;
  SizeRequest      **cached_sizes;
  guint              i, n_sizes;
219

220
  cache = _gtk_widget_peek_request_cache (widget);
221

222
  /* First handle caching of the base requests */
223 224 225
  if (for_size < 0)
    {
      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
226 227 228 229 230
	{
	  cache->cached_width.minimum_size = minimum_size;
	  cache->cached_width.natural_size = natural_size;
	  cache->cached_base_width = TRUE;
	}
231
      else
232 233 234 235 236 237
	{
	  cache->cached_height.minimum_size = minimum_size;
	  cache->cached_height.natural_size = natural_size;
	  cache->cached_base_height = TRUE;
	}
      return;
238
    }
239

240 241 242 243
  /* Check if the minimum_size and natural_size is already
   * in the cache and if this result can be used to extend
   * that cache entry 
   */
244 245
  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
    {
246 247
      cached_sizes = cache->widths;
      n_sizes = cache->cached_widths;
248 249 250
    }
  else
    {
251 252
      cached_sizes = cache->heights;
      n_sizes = cache->cached_heights;
253
    }
254

255
  for (i = 0; i < n_sizes; i++)
256
    {
257 258
      if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
	  cached_sizes[i]->cached_size.natural_size == natural_size)
259
	{
260 261 262
	  cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
	  cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
	  return;
263 264
	}
    }
265

266 267 268 269 270
  /* If not found, pull a new size from the cache, the returned size cache
   * will immediately be used to cache the new computed size so we go ahead
   * and increment the last_cached_width/height right away */
  if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
    {
271
      if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
272
	{
273 274
	  cache->cached_widths++;
	  cache->last_cached_width = cache->cached_widths - 1;
275 276 277
	}
      else
	{
278 279
	  if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
	    cache->last_cached_width = 0;
280
	}
281

282 283 284 285 286 287 288 289 290 291
      if (!cache->widths)
	cache->widths = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);

      if (!cache->widths[cache->last_cached_width])
	cache->widths[cache->last_cached_width] = g_slice_new (SizeRequest);

      cache->widths[cache->last_cached_width]->lower_for_size = for_size;
      cache->widths[cache->last_cached_width]->upper_for_size = for_size;
      cache->widths[cache->last_cached_width]->cached_size.minimum_size = minimum_size;
      cache->widths[cache->last_cached_width]->cached_size.natural_size = natural_size;
292 293 294
    }
  else /* GTK_SIZE_GROUP_VERTICAL */
    {
295
      if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
296
	{
297 298
	  cache->cached_heights++;
	  cache->last_cached_height = cache->cached_heights - 1;
299 300 301
	}
      else
	{
302 303
	  if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
	    cache->last_cached_height = 0;
304 305
	}

306 307
      if (!cache->heights)
	cache->heights = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
308

309 310
      if (!cache->heights[cache->last_cached_height])
	cache->heights[cache->last_cached_height] = g_slice_new (SizeRequest);
311

312 313 314 315
      cache->heights[cache->last_cached_height]->lower_for_size = for_size;
      cache->heights[cache->last_cached_height]->upper_for_size = for_size;
      cache->heights[cache->last_cached_height]->cached_size.minimum_size = minimum_size;
      cache->heights[cache->last_cached_height]->cached_size.natural_size = natural_size;
316 317 318
    }
}

319 320 321 322 323 324 325 326 327 328
static const char *
get_vfunc_name (GtkSizeGroupMode orientation,
                gint             for_size)
{
  if (orientation == GTK_SIZE_GROUP_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";
}

329
static void
330 331 332 333 334
gtk_widget_query_size_for_orientation (GtkWidget        *widget,
                                       GtkSizeGroupMode  orientation,
                                       gint              for_size,
                                       gint             *minimum_size,
                                       gint             *natural_size)
335
{
336 337 338 339
  CachedSize *cached_size;
  gboolean    found_in_cache = FALSE;
  gint        min_size = 0;
  gint        nat_size = 0;
340

341
  found_in_cache = get_cached_size (widget, orientation, for_size, &cached_size);
342

343 344
  if (!found_in_cache)
    {
345
      gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
346

347 348
      gtk_widget_ensure_style (widget);

349
      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
350
        {
351
          if (for_size < 0 || gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
352
            {
353 354 355
	      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);
356
            }
357
          else
358
            {
359 360 361
              gint ignored_position = 0;
              gint minimum_height;
              gint natural_height;
362

363
	      /* Pull the base natural height from the cache as it's needed to adjust
364
	       * the proposed 'for_size' */
365
	      gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
366 367

              /* convert for_size to unadjusted height (for_size is a proposed allocation) */
368 369
              GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
                                                                     GTK_ORIENTATION_VERTICAL,
370 371
                                                                     &minimum_height,
								     &natural_height,
372
                                                                     &ignored_position,
373
                                                                     &adjusted_for_size);
374 375

	      push_recursion_check (widget, orientation, for_size);
376
              GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, 
377
									     MAX (adjusted_for_size, minimum_height),
378
									     &min_size, &nat_size);
379
	      pop_recursion_check (widget, orientation);
380
            }
381
        }
382
      else
383
        {
384
          if (for_size < 0 || gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
385
            {
386 387 388
	      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);
389
            }
390
          else
391
            {
392 393 394
              gint ignored_position = 0;
              gint minimum_width;
              gint natural_width;
395

396
	      /* Pull the base natural width from the cache as it's needed to adjust
397
	       * the proposed 'for_size' */
398
	      gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
399 400

              /* convert for_size to unadjusted width (for_size is a proposed allocation) */
401 402
              GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
                                                                     GTK_ORIENTATION_HORIZONTAL,
403
								     &minimum_width,
404 405
                                                                     &natural_width,
                                                                     &ignored_position,
406
                                                                     &adjusted_for_size);
407 408

	      push_recursion_check (widget, orientation, for_size);
409
              GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, 
410
									     MAX (adjusted_for_size, minimum_width),
411
									     &min_size, &nat_size);
412
	      pop_recursion_check (widget, orientation);
413
            }
414
        }
415

416 417
      if (min_size > nat_size)
        {
418 419
          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));
420 421
        }

422
      if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
423
          _gtk_widget_set_width_request_needed (widget, FALSE);
424
      else
425
          _gtk_widget_set_height_request_needed (widget, FALSE);
426

427 428
      adjusted_min     = min_size;
      adjusted_natural = nat_size;
429 430 431 432 433 434
      GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
                                                          orientation == GTK_SIZE_GROUP_HORIZONTAL ?
                                                          GTK_ORIENTATION_HORIZONTAL :
                                                          GTK_ORIENTATION_VERTICAL,
                                                          &adjusted_min,
                                                          &adjusted_natural);
435

436 437
      if (adjusted_min < min_size ||
          adjusted_natural < nat_size)
438 439
        {
          g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
440
                     G_OBJECT_TYPE_NAME (widget), widget,
441 442
                     orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
                     adjusted_min, adjusted_natural,
443
                     min_size, nat_size);
444 445 446 447 448
          /* 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",
449
                     G_OBJECT_TYPE_NAME (widget), widget,
450 451
                     orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
                     adjusted_min, adjusted_natural,
452
                     min_size, nat_size);
453 454 455 456 457
          /* don't use the adjustment */
        }
      else
        {
          /* adjustment looks good */
458 459
          min_size = adjusted_min;
          nat_size = adjusted_natural;
460 461
        }

462 463 464 465 466 467
      commit_cached_size (widget, orientation, for_size, min_size, nat_size);
    }
  else
    {
      min_size = cached_size->minimum_size;
      nat_size = cached_size->natural_size;
468 469 470
    }

  if (minimum_size)
471
    *minimum_size = min_size;
472

473
  if (natural_size)
474
    *natural_size = nat_size;
475

476
  g_assert (min_size <= nat_size);
477

478
  GTK_NOTE (SIZE_REQUEST,
479
            g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
480
                     widget, G_OBJECT_TYPE_NAME (widget),
481 482
                     orientation == GTK_SIZE_GROUP_HORIZONTAL ?
                     "width for height" : "height for width" ,
483
                     for_size, min_size, nat_size,
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
                     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,
                                          GtkSizeGroupMode  mode,
                                          gint              for_size,
                                          gint             *minimum,
                                          gint             *natural)
{
  GHashTable *widgets;
  GHashTableIter iter;
  gpointer key;
  gint    min_result = 0, nat_result = 0;

  if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
    {
      gtk_widget_query_size_for_orientation (widget, mode, for_size, minimum, natural);
      return;
    }
510

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  widgets = _gtk_size_group_get_widget_peers (widget, mode);

  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, mode, 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);

  *minimum = min_result;
  *natural = nat_result;
533 534
}

535
/**
536
 * gtk_widget_get_request_mode:
537
 * @widget: a #GtkWidget instance
538 539
 *
 * Gets whether the widget prefers a height-for-width layout
540 541 542 543 544 545
 * or a width-for-height layout.
 *
 * <note><para>#GtkBin widgets generally propagate the preference of
 * their child, container widgets need to request something either in
 * context of their children or in context of their allocation
 * capabilities.</para></note>
546
 *
547
 * Returns: The #GtkSizeRequestMode preferred by @widget.
548 549 550
 *
 * Since: 3.0
 */
551
GtkSizeRequestMode
552
gtk_widget_get_request_mode (GtkWidget *widget)
553
{
554
  g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
555

556
  return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
557 558
}

559
/**
560 561
 * gtk_widget_get_preferred_width:
 * @widget: a #GtkWidget instance
562 563
 * @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
564
 *
565
 * Retrieves a widget's initial minimum and natural width.
566
 *
567 568
 * <note><para>This call is specific to height-for-width
 * requests.</para></note>
569
 *
570 571
 * The returned request will be modified by the
 * GtkWidgetClass::adjust_size_request virtual method and by any
572
 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
573 574 575
 * is the one that should be used for layout, not necessarily the one
 * returned by the widget itself.
 *
576 577 578
 * Since: 3.0
 */
void
579 580 581
gtk_widget_get_preferred_width (GtkWidget *widget,
                                gint      *minimum_width,
                                gint      *natural_width)
582
{
583 584 585
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (minimum_width != NULL || natural_width != NULL);

586 587 588 589 590
  _gtk_widget_compute_size_for_orientation (widget,
                                            GTK_SIZE_GROUP_HORIZONTAL,
                                            -1,
                                            minimum_width,
                                            natural_width);
591 592 593 594
}


/**
595 596
 * gtk_widget_get_preferred_height:
 * @widget: a #GtkWidget instance
597 598
 * @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
599
 *
600
 * Retrieves a widget's initial minimum and natural height.
601
 *
602
 * <note><para>This call is specific to width-for-height requests.</para></note>
603
 *
604 605
 * The returned request will be modified by the
 * GtkWidgetClass::adjust_size_request virtual method and by any
606
 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
607 608 609
 * is the one that should be used for layout, not necessarily the one
 * returned by the widget itself.
 *
610 611 612
 * Since: 3.0
 */
void
613 614 615
gtk_widget_get_preferred_height (GtkWidget *widget,
                                 gint      *minimum_height,
                                 gint      *natural_height)
616
{
617 618 619
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (minimum_height != NULL || natural_height != NULL);

620 621 622 623 624
  _gtk_widget_compute_size_for_orientation (widget,
                                            GTK_SIZE_GROUP_VERTICAL,
                                            -1,
                                            minimum_height,
                                            natural_height);
625 626
}

627 628


629
/**
630 631
 * gtk_widget_get_preferred_width_for_height:
 * @widget: a #GtkWidget instance
632
 * @height: the height which is available for allocation
633 634
 * @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
635
 *
636
 * Retrieves a widget's minimum and natural width if it would be given
637
 * the specified @height.
638
 *
639 640
 * The returned request will be modified by the
 * GtkWidgetClass::adjust_size_request virtual method and by any
641
 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
642 643 644
 * is the one that should be used for layout, not necessarily the one
 * returned by the widget itself.
 *
645
 * Since: 3.0
646 647
 */
void
648 649 650 651
gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
                                           gint       height,
                                           gint      *minimum_width,
                                           gint      *natural_width)
652
{
653 654 655 656
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (minimum_width != NULL || natural_width != NULL);
  g_return_if_fail (height >= 0);

657 658 659 660 661
  _gtk_widget_compute_size_for_orientation (widget,
                                            GTK_SIZE_GROUP_HORIZONTAL,
                                            height,
                                            minimum_width,
                                            natural_width);
662 663 664
}

/**
665 666
 * gtk_widget_get_preferred_height_for_width:
 * @widget: a #GtkWidget instance
667
 * @width: the width which is available for allocation
668 669
 * @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
670
 *
671
 * Retrieves a widget's minimum and natural height if it would be given
672
 * the specified @width.
673
 *
674 675
 * The returned request will be modified by the
 * GtkWidgetClass::adjust_size_request virtual method and by any
676
 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
677 678 679
 * is the one that should be used for layout, not necessarily the one
 * returned by the widget itself.
 *
680
 * Since: 3.0
681 682
 */
void
683 684 685 686
gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
                                           gint       width,
                                           gint      *minimum_height,
                                           gint      *natural_height)
687
{
688 689 690 691
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (minimum_height != NULL || natural_height != NULL);
  g_return_if_fail (width >= 0);

692 693 694 695 696
  _gtk_widget_compute_size_for_orientation (widget,
                                            GTK_SIZE_GROUP_VERTICAL,
                                            width,
                                            minimum_height,
                                            natural_height);
697 698
}

699
/**
700 701
 * gtk_widget_get_preferred_size:
 * @widget: a #GtkWidget instance
702 703
 * @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
704
 *
705
 * Retrieves the minimum and natural size of a widget, taking
706 707
 * into account the widget's preference for height-for-width management.
 *
708
 * This is used to retrieve a suitable size by container widgets which do
709 710
 * 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
711
 * free-form containers such as GtkLayout.
712
 *
713
 * <note><para>Handle with care. Note that the natural height of a height-for-width
714 715 716
 * 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>
717 718 719 720
 *
 * Since: 3.0
 */
void
721 722 723
gtk_widget_get_preferred_size (GtkWidget      *widget,
                               GtkRequisition *minimum_size,
                               GtkRequisition *natural_size)
724 725 726 727
{
  gint min_width, nat_width;
  gint min_height, nat_height;

728
  g_return_if_fail (GTK_IS_WIDGET (widget));
729

730
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
731
    {
732
      gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
733 734 735 736

      if (minimum_size)
	{
	  minimum_size->width = min_width;
737 738
	  gtk_widget_get_preferred_height_for_width (widget, min_width,
                                                     &minimum_size->height, NULL);
739 740 741 742 743
	}

      if (natural_size)
	{
	  natural_size->width = nat_width;
744 745
	  gtk_widget_get_preferred_height_for_width (widget, nat_width,
                                                     NULL, &natural_size->height);
746
	}
747
    }
748
  else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
749
    {
750
      gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
751 752 753 754

      if (minimum_size)
	{
	  minimum_size->height = min_height;
755 756
	  gtk_widget_get_preferred_width_for_height (widget, min_height,
                                                     &minimum_size->width, NULL);
757 758 759 760 761
	}

      if (natural_size)
	{
	  natural_size->height = nat_height;
762 763
	  gtk_widget_get_preferred_width_for_height (widget, nat_height,
                                                     NULL, &natural_size->width);
764
	}
765 766
    }
}
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793


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;
}

/**
794
 * gtk_distribute_natural_allocation:
795 796 797 798 799 800
 * @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.
 *
801
 * Distributes @extra_space to child @sizes by bringing smaller
802 803 804 805 806 807 808 809 810
 * 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.
 */
811
gint
812 813 814 815
gtk_distribute_natural_allocation (gint              extra_space,
				   guint             n_requested_sizes,
				   GtkRequestedSize *sizes)
{
816
  guint *spreading;
817 818
  gint   i;

819 820
  g_return_val_if_fail (extra_space >= 0, 0);

821 822
  spreading = g_newa (guint, n_requested_sizes);

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  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.
   */
842

843 844 845 846
  /* Sort descending by gap and position. */
  g_qsort_with_data (spreading,
		     n_requested_sizes, sizeof (guint),
		     compare_gap, sizes);
847

848 849 850 851 852 853 854 855 856 857 858 859
  /* 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;
860

861
      gint extra = MIN (glue, gap);
862

863
      sizes[spreading[i]].minimum_size += extra;
864

865 866 867 868 869
      extra_space -= extra;
    }

  return extra_space;
}