summaryrefslogtreecommitdiff
path: root/gtk/gtksizerequest.c
blob: bad631c0224ff4f19d3f48b9489c2778241669f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* gtksizerequest.c
 * Copyright (C) 2007-2010 Openismus GmbH
 *
 * Authors:
 *      Mathias Hasselmann <mathias@openismus.com>
 *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
 *
 * 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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "gtksizerequest.h"

#include "gtkdebug.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtksizegroup-private.h"
#include "gtksizerequestcacheprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcssnumbervalueprivate.h"
#include "gtklayoutmanagerprivate.h"


#ifdef G_ENABLE_CONSISTENCY_CHECKS
static GQuark recursion_check_quark = 0;

static void
push_recursion_check (GtkWidget       *widget,
                      GtkOrientation   orientation)
{
  gboolean in_measure = FALSE;

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

  in_measure = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), recursion_check_quark));

  if (in_measure)
    {
      g_warning ("%s %p: widget tried to gtk_widget_measure inside "
                 " GtkWidget::measure implementation. "
                 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->measure "
                 "directly rather than using gtk_widget_measure",
                 G_OBJECT_TYPE_NAME (widget), widget);
    }

  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, GINT_TO_POINTER(TRUE));
}

static void
pop_recursion_check (GtkWidget       *widget,
                     GtkOrientation   orientation)
{
  g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
}
#else
#define push_recursion_check(widget, orientation)
#define pop_recursion_check(widget, orientation)
#endif /* G_ENABLE_CONSISTENCY_CHECKS */

static GtkSizeRequestMode
fetch_request_mode (GtkWidget *widget)
{
  GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);

  if (layout_manager != NULL)
    return gtk_layout_manager_get_request_mode (layout_manager);
  else
    return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
}

static int
get_number (GtkCssValue *value)
{
  double d = _gtk_css_number_value_get (value, 100);

  if (d < 1)
    return ceil (d);
  else
    return floor (d);
}

/* Special-case min-width|height to round upwards, to avoid underalloc by 1px */
static int
get_number_ceil (GtkCssValue *value)
{
  return ceil (_gtk_css_number_value_get (value, 100));
}

static void
get_box_margin (GtkCssStyle *style,
                GtkBorder   *margin)
{
  margin->top = get_number (style->size->margin_top);
  margin->left = get_number (style->size->margin_left);
  margin->bottom = get_number (style->size->margin_bottom);
  margin->right = get_number (style->size->margin_right);
}

static void
get_box_border (GtkCssStyle *style,
                GtkBorder   *border)
{
  border->top = get_number (style->border->border_top_width);
  border->left = get_number (style->border->border_left_width);
  border->bottom = get_number (style->border->border_bottom_width);
  border->right = get_number (style->border->border_right_width);
}

static void
get_box_padding (GtkCssStyle *style,
                 GtkBorder   *border)
{
  border->top = get_number (style->size->padding_top);
  border->left = get_number (style->size->padding_left);
  border->bottom = get_number (style->size->padding_bottom);
  border->right = get_number (style->size->padding_right);
}

static void
gtk_widget_query_size_for_orientation (GtkWidget        *widget,
                                       GtkOrientation    orientation,
                                       int               for_size,
                                       int              *minimum,
                                       int              *natural,
                                       int              *minimum_baseline,
                                       int              *natural_baseline)
{
  const gboolean baselines_requested = (minimum_baseline != NULL || natural_baseline != NULL);
  SizeRequestCache *cache;
  int min_size = 0;
  int nat_size = 0;
  int min_baseline = -1;
  int nat_baseline = -1;
  gboolean found_in_cache;

  gtk_widget_ensure_resize (widget);

  /* We check the request mode first, to determine whether the widget even does
   * any wfh/hfw handling. If it doesn't, we reset for_size to -1 and ensure
   * that we only cache one size for the widget (i.e. a lot more cache hits). */
  cache = _gtk_widget_peek_request_cache (widget);
  if (G_UNLIKELY (!cache->request_mode_valid))
    {
      cache->request_mode = fetch_request_mode (widget);
      cache->request_mode_valid = TRUE;
    }

  if (cache->request_mode == GTK_SIZE_REQUEST_CONSTANT_SIZE)
    for_size = -1;

  found_in_cache = _gtk_size_request_cache_lookup (cache,
                                                   orientation,
                                                   for_size,
                                                   &min_size,
                                                   &nat_size,
                                                   &min_baseline,
                                                   &nat_baseline);

  if (!found_in_cache)
    {
      GtkWidgetClass *widget_class;
      GtkCssStyle *style;
      GtkBorder margin, border, padding;
      int adjusted_min, adjusted_natural;
      int reported_min_size = 0;
      int reported_nat_size = 0;
      int css_min_size;
      int css_min_for_size;
      int css_extra_for_size;
      int css_extra_size;
      int widget_margins_for_size;

      style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
      get_box_margin (style, &margin);
      get_box_border (style, &border);
      get_box_padding (style, &padding);

      widget_class = GTK_WIDGET_GET_CLASS (widget);

      if (orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          css_extra_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
          css_extra_for_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
          css_min_size = get_number_ceil (style->size->min_width);
          css_min_for_size = get_number_ceil (style->size->min_height);
          widget_margins_for_size = widget->priv->margin.top + widget->priv->margin.bottom;
        }
      else
        {
          css_extra_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
          css_extra_for_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
          css_min_size = get_number_ceil (style->size->min_height);
          css_min_for_size = get_number_ceil (style->size->min_width);
          widget_margins_for_size = widget->priv->margin.left + widget->priv->margin.right;
        }

      GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);

      if (layout_manager != NULL)
        {
          if (for_size < 0)
            {
              push_recursion_check (widget, orientation);
              gtk_layout_manager_measure (layout_manager, widget,
                                          orientation, -1,
                                          &reported_min_size, &reported_nat_size,
                                          &min_baseline, &nat_baseline);
              pop_recursion_check (widget, orientation);
            }
          else
            {
              int adjusted_for_size;
              int minimum_for_size = 0;
              int natural_for_size = 0;

              /* Pull the minimum for_size from the cache as it's needed to adjust
               * the proposed 'for_size' */
              gtk_layout_manager_measure (layout_manager, widget,
                                          OPPOSITE_ORIENTATION (orientation), -1,
                                          &minimum_for_size, &natural_for_size,
                                          NULL, NULL);

              if (for_size < MAX (minimum_for_size, css_min_for_size))
                for_size = MAX (minimum_for_size, css_min_for_size);

              adjusted_for_size = for_size - widget_margins_for_size;
              adjusted_for_size -= css_extra_for_size;
              if (adjusted_for_size < 0)
                adjusted_for_size = MAX (minimum_for_size, css_min_for_size);

              push_recursion_check (widget, orientation);
              gtk_layout_manager_measure (layout_manager, widget,
                                          orientation,
                                          adjusted_for_size,
                                          &reported_min_size, &reported_nat_size,
                                          &min_baseline, &nat_baseline);
              pop_recursion_check (widget, orientation);
            }
        }
      else
        {
          if (for_size < 0)
            {
              push_recursion_check (widget, orientation);
              widget_class->measure (widget, orientation, -1,
                                     &reported_min_size, &reported_nat_size,
                                     &min_baseline, &nat_baseline);
              pop_recursion_check (widget, orientation);
            }
          else
            {
              int adjusted_for_size;
              int minimum_for_size = 0;
              int natural_for_size = 0;

              /* Pull the minimum for_size from the cache as it's needed to adjust
               * the proposed 'for_size' */
              gtk_widget_measure (widget, OPPOSITE_ORIENTATION (orientation), -1,
                                  &minimum_for_size, &natural_for_size, NULL, NULL);

              /* TODO: Warn if the given for_size is too small? */
              if (for_size < MAX (minimum_for_size, css_min_for_size))
                for_size = MAX (minimum_for_size, css_min_for_size);

              adjusted_for_size = for_size - widget_margins_for_size;

              adjusted_for_size -= css_extra_for_size;

              push_recursion_check (widget, orientation);
              widget_class->measure (widget,
                                     orientation,
                                     adjusted_for_size,
                                     &reported_min_size, &reported_nat_size,
                                     &min_baseline, &nat_baseline);
              pop_recursion_check (widget, orientation);
            }
        }

      min_size = MAX (0, MAX (reported_min_size, css_min_size)) + css_extra_size;
      nat_size = MAX (0, MAX (reported_nat_size, css_min_size)) + css_extra_size;

      if (G_UNLIKELY (min_size > nat_size))
        {
          if (orientation == GTK_ORIENTATION_HORIZONTAL)
            {
              g_warning ("%s %p (%s) reported min width %d and natural width %d in measure() with for_size=%d; natural size must be >= min size",
                         G_OBJECT_TYPE_NAME (widget), widget,
                         g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
                         min_size, nat_size, for_size);
            }
          else
            {
              g_warning ("%s %p (%s) reported min height %d and natural height %d in measure() with for_size=%d; natural size must be >= min size",
                         G_OBJECT_TYPE_NAME (widget), widget,
                         g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
                         min_size, nat_size, for_size);

            }

          nat_size = min_size;
        }
      else if (G_UNLIKELY (min_size < 0))
        {
          g_warning ("%s %p (%s) reported min %s %d, but sizes must be >= 0",
                     G_OBJECT_TYPE_NAME (widget), widget,
                     g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
                     orientation == GTK_ORIENTATION_HORIZONTAL ? "width" : "height",
                     min_size);
          min_size = 0;
          nat_size = MAX (0, min_size);
        }

      adjusted_min     = min_size;
      adjusted_natural = nat_size;
      gtk_widget_adjust_size_request (widget, orientation,
                                      &adjusted_min, &adjusted_natural);

      if (adjusted_min < min_size ||
          adjusted_natural < nat_size)
        {
          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
        {
          /* adjustment looks good */
          min_size = adjusted_min;
          nat_size = adjusted_natural;
        }

      if (baselines_requested && (min_baseline != -1 || nat_baseline != -1))
	{
	  if (orientation == GTK_ORIENTATION_HORIZONTAL)
	    {
	      g_warning ("%s %p reported a horizontal baseline",
			 G_OBJECT_TYPE_NAME (widget), widget);
	      min_baseline = -1;
	      nat_baseline = -1;
	    }
	  else if (min_baseline == -1 || nat_baseline == -1)
	    {
	      g_warning ("%s %p reported baseline for only one of min/natural (min: %d, natural: %d)",
			 G_OBJECT_TYPE_NAME (widget), widget,
			 min_baseline, nat_baseline);
	      min_baseline = -1;
	      nat_baseline = -1;
	    }
          else if (min_baseline > reported_min_size ||
                   nat_baseline > reported_nat_size ||
                   min_baseline < 0 ||
                   nat_baseline < 0)
            {
              g_warning ("%s %p reported baselines of minimum %d and natural %d, but sizes of "
                         "minimum %d and natural %d. Baselines must be inside the widget size.",
                         G_OBJECT_TYPE_NAME (widget), widget, min_baseline, nat_baseline,
                         reported_min_size, reported_nat_size);
              min_baseline = -1;
              nat_baseline = -1;
            }
	  else
            {
              if (css_min_size > reported_min_size)
                {
                  min_baseline += (css_min_size - reported_min_size) / 2;
                  nat_baseline += (css_min_size - reported_min_size) / 2;
                }

              min_baseline += margin.top + border.top + padding.top;
              nat_baseline += margin.top + border.top + padding.top;

              gtk_widget_adjust_baseline_request (widget, &min_baseline, &nat_baseline);
            }
        }

      _gtk_size_request_cache_commit (cache,
                                      orientation,
                                      for_size,
                                      min_size,
                                      nat_size,
				      min_baseline,
				      nat_baseline);
    }

  if (minimum)
    *minimum = min_size;

  if (natural)
    *natural = nat_size;

  if (minimum_baseline)
    *minimum_baseline = min_baseline;

  if (natural_baseline)
    *natural_baseline = nat_baseline;

  g_assert (min_size <= nat_size);

  GTK_DISPLAY_NOTE (_gtk_widget_get_display (widget), SIZE_REQUEST, {
            GString *s;

            s = g_string_new ("");
            g_string_append_printf (s, "[%p] %s\t%s: %d is minimum %d and natural: %d",
                                    widget, G_OBJECT_TYPE_NAME (widget),
                                    orientation == GTK_ORIENTATION_HORIZONTAL
                                    ? "width for height"
                                    : "height for width",
                                    for_size, min_size, nat_size);
	    if (min_baseline != -1 || nat_baseline != -1)
              {
                g_string_append_printf (s, ", baseline %d/%d",
                                        min_baseline, nat_baseline);
              }
	    g_string_append_printf (s, " (hit cache: %s)\n",
		                    found_in_cache ? "yes" : "no");
            g_message ("%s", s->str);
            g_string_free (s, TRUE);
	    });
}

/**
 * gtk_widget_measure:
 * @widget: A #GtkWidget instance
 * @orientation: the orientation to measure
 * @for_size: Size for the opposite of @orientation, i.e.
 *   if @orientation is %GTK_ORIENTATION_HORIZONTAL, this is
 *   the height the widget should be measured with. The %GTK_ORIENTATION_VERTICAL
 *   case is analogous. This way, both height-for-width and width-for-height
 *   requests can be implemented. If no size is known, -1 can be passed.
 * @minimum: (out) (optional): location to store the minimum size, or %NULL
 * @natural: (out) (optional): location to store the natural size, or %NULL
 * @minimum_baseline: (out) (optional): location to store the baseline
 *   position for the minimum size, or %NULL
 * @natural_baseline: (out) (optional): location to store the baseline
 *   position for the natural size, or %NULL
 *
 * Measures @widget in the orientation @orientation and for the given @for_size.
 *
 * As an example, if @orientation is %GTK_ORIENTATION_HORIZONTAL and @for_size
 * is 300, this functions will compute the minimum and natural width of @widget
 * if it is allocated at a height of 300 pixels.
 *
 * See [GtkWidget’s geometry management section](class.Widget.html#height-for-width-geometry-management) for
 * a more details on implementing #GtkWidgetClass.measure().
 */
void
gtk_widget_measure (GtkWidget        *widget,
                    GtkOrientation    orientation,
                    int               for_size,
                    int              *minimum,
                    int              *natural,
                    int              *minimum_baseline,
                    int              *natural_baseline)
{
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (for_size >= -1);
  g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL ||
                    orientation == GTK_ORIENTATION_VERTICAL);

  /* 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 (!_gtk_widget_get_visible (widget) && !GTK_IS_ROOT (widget))
    {
      if (minimum)
        *minimum = 0;
      if (natural)
        *natural = 0;
      if (minimum_baseline)
        *minimum_baseline = -1;
      if (natural_baseline)
        *natural_baseline = -1;
      return;
    }

  if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
    {
      gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural,
                                             minimum_baseline, natural_baseline);
    }
  else
    {
      GHashTable *widgets;
      GHashTableIter iter;
      gpointer key;
      int min_result = 0, nat_result = 0;

      widgets = _gtk_size_group_get_widget_peers (widget, orientation);

      g_hash_table_iter_init (&iter, widgets);
      while (g_hash_table_iter_next (&iter, &key, NULL))
        {
          GtkWidget *tmp_widget = key;
          int min_dimension, nat_dimension;

          gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size,
                                                 &min_dimension, &nat_dimension, NULL, NULL);

          min_result = MAX (min_result, min_dimension);
          nat_result = MAX (nat_result, nat_dimension);
        }

      g_hash_table_destroy (widgets);

      /* Baselines make no sense with sizegroups really */
      if (minimum_baseline)
        *minimum_baseline = -1;

      if (natural_baseline)
        *natural_baseline = -1;

      if (minimum)
        *minimum = min_result;

      if (natural)
        *natural = nat_result;
    }
}

/**
 * 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.
 *
 * Single-child widgets generally propagate the preference of
 * their child, more complex widgets need to request something
 * either in context of their children or in context of their
 * allocation capabilities.
 *
 * Returns: The `GtkSizeRequestMode` preferred by @widget.
 */
GtkSizeRequestMode
gtk_widget_get_request_mode (GtkWidget *widget)
{
  SizeRequestCache *cache;

  cache = _gtk_widget_peek_request_cache (widget);

  if (G_UNLIKELY (!cache->request_mode_valid))
    {
      cache->request_mode = fetch_request_mode (widget);
      cache->request_mode_valid = TRUE;
    }

  return cache->request_mode;
}

/**
 * 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
 * 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. It can be used
 * to deduce toplevel window and menu sizes as well as child widgets in
 * free-form containers such as `GtkFixed`.
 *
 * 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.
 *
 * Use [id@gtk_widget_measure] if you want to support baseline alignment.
 */
void
gtk_widget_get_preferred_size (GtkWidget      *widget,
                               GtkRequisition *minimum_size,
                               GtkRequisition *natural_size)
{
  int min_width, nat_width;
  int min_height, nat_height;

  g_return_if_fail (GTK_IS_WIDGET (widget));

  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
    {
      gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
                          &min_width, &nat_width, NULL, NULL);

      if (minimum_size)
        {
          minimum_size->width = min_width;
          gtk_widget_measure (widget,
                              GTK_ORIENTATION_VERTICAL, min_width,
                              &minimum_size->height, NULL, NULL, NULL);
        }

      if (natural_size)
        {
          natural_size->width = nat_width;
          gtk_widget_measure (widget,
                              GTK_ORIENTATION_VERTICAL, nat_width,
                              NULL, &natural_size->height, NULL, NULL);
        }
    }
  else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
    {
      gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
                          -1, &min_height, &nat_height, NULL, NULL);

      if (minimum_size)
        {
          minimum_size->height = min_height;
          gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, min_height,
                              &minimum_size->width, NULL, NULL, NULL);
        }

      if (natural_size)
        {
          natural_size->height = nat_height;
          gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, nat_height,
                              NULL, &natural_size->width, NULL, NULL);
        }
    }
}

static int
compare_gap (gconstpointer p1,
	     gconstpointer p2,
	     gpointer      data)
{
  GtkRequestedSize *sizes = data;
  const guint *c1 = p1;
  const guint *c2 = p2;

  const int d1 = MAX (sizes[*c1].natural_size -
                       sizes[*c1].minimum_size,
                       0);
  const int d2 = MAX (sizes[*c2].natural_size -
                       sizes[*c2].minimum_size,
                       0);

  int 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: (array length=n_requested_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.
 */
int
gtk_distribute_natural_allocation (int               extra_space,
				   guint             n_requested_sizes,
				   GtkRequestedSize *sizes)
{
  guint *spreading;
  int    i;

  g_return_val_if_fail (extra_space >= 0, 0);
  g_return_val_if_fail (n_requested_sizes > 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 masterpiece 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.
       */
      int glue = (extra_space + i) / (i + 1);
      int gap = sizes[(spreading[i])].natural_size
	- sizes[(spreading[i])].minimum_size;

      int extra = MIN (glue, gap);

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

      extra_space -= extra;
    }

  return extra_space;
}