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 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23
24 #include "gtksizerequest.h"
25
26 #include "gtkdebug.h"
27 #include "gtkintl.h"
28 #include "gtkprivate.h"
29 #include "gtksizegroup-private.h"
30 #include "gtksizerequestcacheprivate.h"
31 #include "gtkwidgetprivate.h"
32 #include "gtkcssnodeprivate.h"
33 #include "gtkcssnumbervalueprivate.h"
34 #include "gtklayoutmanagerprivate.h"
35
36
37 #ifdef G_ENABLE_CONSISTENCY_CHECKS
38 static GQuark recursion_check_quark = 0;
39
40 static void
push_recursion_check(GtkWidget * widget,GtkOrientation orientation)41 push_recursion_check (GtkWidget *widget,
42 GtkOrientation orientation)
43 {
44 gboolean in_measure = FALSE;
45
46 if (recursion_check_quark == 0)
47 recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
48
49 in_measure = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), recursion_check_quark));
50
51 if (in_measure)
52 {
53 g_warning ("%s %p: widget tried to gtk_widget_measure inside "
54 " GtkWidget::measure implementation. "
55 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->measure "
56 "directly rather than using gtk_widget_measure",
57 G_OBJECT_TYPE_NAME (widget), widget);
58 }
59
60 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, GINT_TO_POINTER(TRUE));
61 }
62
63 static void
pop_recursion_check(GtkWidget * widget,GtkOrientation orientation)64 pop_recursion_check (GtkWidget *widget,
65 GtkOrientation orientation)
66 {
67 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
68 }
69 #else
70 #define push_recursion_check(widget, orientation)
71 #define pop_recursion_check(widget, orientation)
72 #endif /* G_ENABLE_CONSISTENCY_CHECKS */
73
74 static GtkSizeRequestMode
fetch_request_mode(GtkWidget * widget)75 fetch_request_mode (GtkWidget *widget)
76 {
77 GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);
78
79 if (layout_manager != NULL)
80 return gtk_layout_manager_get_request_mode (layout_manager);
81 else
82 return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
83 }
84
85 static int
get_number(GtkCssValue * value)86 get_number (GtkCssValue *value)
87 {
88 double d = _gtk_css_number_value_get (value, 100);
89
90 if (d < 1)
91 return ceil (d);
92 else
93 return floor (d);
94 }
95
96 /* Special-case min-width|height to round upwards, to avoid underalloc by 1px */
97 static int
get_number_ceil(GtkCssValue * value)98 get_number_ceil (GtkCssValue *value)
99 {
100 return ceil (_gtk_css_number_value_get (value, 100));
101 }
102
103 static void
get_box_margin(GtkCssStyle * style,GtkBorder * margin)104 get_box_margin (GtkCssStyle *style,
105 GtkBorder *margin)
106 {
107 margin->top = get_number (style->size->margin_top);
108 margin->left = get_number (style->size->margin_left);
109 margin->bottom = get_number (style->size->margin_bottom);
110 margin->right = get_number (style->size->margin_right);
111 }
112
113 static void
get_box_border(GtkCssStyle * style,GtkBorder * border)114 get_box_border (GtkCssStyle *style,
115 GtkBorder *border)
116 {
117 border->top = get_number (style->border->border_top_width);
118 border->left = get_number (style->border->border_left_width);
119 border->bottom = get_number (style->border->border_bottom_width);
120 border->right = get_number (style->border->border_right_width);
121 }
122
123 static void
get_box_padding(GtkCssStyle * style,GtkBorder * border)124 get_box_padding (GtkCssStyle *style,
125 GtkBorder *border)
126 {
127 border->top = get_number (style->size->padding_top);
128 border->left = get_number (style->size->padding_left);
129 border->bottom = get_number (style->size->padding_bottom);
130 border->right = get_number (style->size->padding_right);
131 }
132
133 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)134 gtk_widget_query_size_for_orientation (GtkWidget *widget,
135 GtkOrientation orientation,
136 int for_size,
137 int *minimum,
138 int *natural,
139 int *minimum_baseline,
140 int *natural_baseline)
141 {
142 const gboolean baselines_requested = (minimum_baseline != NULL || natural_baseline != NULL);
143 SizeRequestCache *cache;
144 int min_size = 0;
145 int nat_size = 0;
146 int min_baseline = -1;
147 int nat_baseline = -1;
148 gboolean found_in_cache;
149
150 gtk_widget_ensure_resize (widget);
151
152 /* We check the request mode first, to determine whether the widget even does
153 * any wfh/hfw handling. If it doesn't, we reset for_size to -1 and ensure
154 * that we only cache one size for the widget (i.e. a lot more cache hits). */
155 cache = _gtk_widget_peek_request_cache (widget);
156 if (G_UNLIKELY (!cache->request_mode_valid))
157 {
158 cache->request_mode = fetch_request_mode (widget);
159 cache->request_mode_valid = TRUE;
160 }
161
162 if (cache->request_mode == GTK_SIZE_REQUEST_CONSTANT_SIZE)
163 for_size = -1;
164
165 found_in_cache = _gtk_size_request_cache_lookup (cache,
166 orientation,
167 for_size,
168 &min_size,
169 &nat_size,
170 &min_baseline,
171 &nat_baseline);
172
173 if (!found_in_cache)
174 {
175 GtkWidgetClass *widget_class;
176 GtkCssStyle *style;
177 GtkBorder margin, border, padding;
178 int adjusted_min, adjusted_natural;
179 int reported_min_size = 0;
180 int reported_nat_size = 0;
181 int css_min_size;
182 int css_min_for_size;
183 int css_extra_for_size;
184 int css_extra_size;
185 int widget_margins_for_size;
186
187 style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
188 get_box_margin (style, &margin);
189 get_box_border (style, &border);
190 get_box_padding (style, &padding);
191
192 widget_class = GTK_WIDGET_GET_CLASS (widget);
193
194 if (orientation == GTK_ORIENTATION_HORIZONTAL)
195 {
196 css_extra_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
197 css_extra_for_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
198 css_min_size = get_number_ceil (style->size->min_width);
199 css_min_for_size = get_number_ceil (style->size->min_height);
200 widget_margins_for_size = widget->priv->margin.top + widget->priv->margin.bottom;
201 }
202 else
203 {
204 css_extra_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
205 css_extra_for_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
206 css_min_size = get_number_ceil (style->size->min_height);
207 css_min_for_size = get_number_ceil (style->size->min_width);
208 widget_margins_for_size = widget->priv->margin.left + widget->priv->margin.right;
209 }
210
211 GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);
212
213 if (layout_manager != NULL)
214 {
215 if (for_size < 0)
216 {
217 push_recursion_check (widget, orientation);
218 gtk_layout_manager_measure (layout_manager, widget,
219 orientation, -1,
220 &reported_min_size, &reported_nat_size,
221 &min_baseline, &nat_baseline);
222 pop_recursion_check (widget, orientation);
223 }
224 else
225 {
226 int adjusted_for_size;
227 int minimum_for_size = 0;
228 int natural_for_size = 0;
229
230 /* Pull the minimum for_size from the cache as it's needed to adjust
231 * the proposed 'for_size' */
232 gtk_layout_manager_measure (layout_manager, widget,
233 OPPOSITE_ORIENTATION (orientation), -1,
234 &minimum_for_size, &natural_for_size,
235 NULL, NULL);
236
237 if (minimum_for_size < css_min_for_size)
238 minimum_for_size = css_min_for_size;
239
240 if (for_size < minimum_for_size)
241 for_size = minimum_for_size;
242
243 adjusted_for_size = for_size - widget_margins_for_size;
244 adjusted_for_size -= css_extra_for_size;
245 if (adjusted_for_size < 0)
246 adjusted_for_size = minimum_for_size;
247
248 push_recursion_check (widget, orientation);
249 gtk_layout_manager_measure (layout_manager, widget,
250 orientation,
251 adjusted_for_size,
252 &reported_min_size, &reported_nat_size,
253 &min_baseline, &nat_baseline);
254 pop_recursion_check (widget, orientation);
255 }
256 }
257 else
258 {
259 if (for_size < 0)
260 {
261 push_recursion_check (widget, orientation);
262 widget_class->measure (widget, orientation, -1,
263 &reported_min_size, &reported_nat_size,
264 &min_baseline, &nat_baseline);
265 pop_recursion_check (widget, orientation);
266 }
267 else
268 {
269 int adjusted_for_size;
270 int minimum_for_size = 0;
271 int natural_for_size = 0;
272
273 /* Pull the minimum for_size from the cache as it's needed to adjust
274 * the proposed 'for_size' */
275 gtk_widget_measure (widget, OPPOSITE_ORIENTATION (orientation), -1,
276 &minimum_for_size, &natural_for_size, NULL, NULL);
277
278 if (minimum_for_size < css_min_for_size)
279 minimum_for_size = css_min_for_size;
280
281 if (for_size < minimum_for_size)
282 for_size = minimum_for_size;
283
284 adjusted_for_size = for_size - widget_margins_for_size;
285 adjusted_for_size -= css_extra_for_size;
286 if (adjusted_for_size < 0)
287 adjusted_for_size = minimum_for_size;
288
289 push_recursion_check (widget, orientation);
290 widget_class->measure (widget,
291 orientation,
292 adjusted_for_size,
293 &reported_min_size, &reported_nat_size,
294 &min_baseline, &nat_baseline);
295 pop_recursion_check (widget, orientation);
296 }
297 }
298
299 min_size = MAX (0, MAX (reported_min_size, css_min_size)) + css_extra_size;
300 nat_size = MAX (0, MAX (reported_nat_size, css_min_size)) + css_extra_size;
301
302 if (G_UNLIKELY (min_size > nat_size))
303 {
304 if (orientation == GTK_ORIENTATION_HORIZONTAL)
305 {
306 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",
307 G_OBJECT_TYPE_NAME (widget), widget,
308 g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
309 min_size, nat_size, for_size);
310 }
311 else
312 {
313 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",
314 G_OBJECT_TYPE_NAME (widget), widget,
315 g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
316 min_size, nat_size, for_size);
317
318 }
319
320 nat_size = min_size;
321 }
322 else if (G_UNLIKELY (min_size < 0))
323 {
324 g_warning ("%s %p (%s) reported min %s %d, but sizes must be >= 0",
325 G_OBJECT_TYPE_NAME (widget), widget,
326 g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
327 orientation == GTK_ORIENTATION_HORIZONTAL ? "width" : "height",
328 min_size);
329 min_size = 0;
330 nat_size = MAX (0, min_size);
331 }
332
333 adjusted_min = min_size;
334 adjusted_natural = nat_size;
335 gtk_widget_adjust_size_request (widget, orientation,
336 &adjusted_min, &adjusted_natural);
337
338 if (adjusted_min < min_size ||
339 adjusted_natural < nat_size)
340 {
341 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
342 G_OBJECT_TYPE_NAME (widget), widget,
343 orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
344 adjusted_min, adjusted_natural,
345 min_size, nat_size);
346 /* don't use the adjustment */
347 }
348 else if (adjusted_min > adjusted_natural)
349 {
350 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
351 G_OBJECT_TYPE_NAME (widget), widget,
352 orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
353 adjusted_min, adjusted_natural,
354 min_size, nat_size);
355 /* don't use the adjustment */
356 }
357 else
358 {
359 /* adjustment looks good */
360 min_size = adjusted_min;
361 nat_size = adjusted_natural;
362 }
363
364 if (baselines_requested && (min_baseline != -1 || nat_baseline != -1))
365 {
366 if (orientation == GTK_ORIENTATION_HORIZONTAL)
367 {
368 g_warning ("%s %p reported a horizontal baseline",
369 G_OBJECT_TYPE_NAME (widget), widget);
370 min_baseline = -1;
371 nat_baseline = -1;
372 }
373 else if (min_baseline == -1 || nat_baseline == -1)
374 {
375 g_warning ("%s %p reported baseline for only one of min/natural (min: %d, natural: %d)",
376 G_OBJECT_TYPE_NAME (widget), widget,
377 min_baseline, nat_baseline);
378 min_baseline = -1;
379 nat_baseline = -1;
380 }
381 else if (min_baseline > reported_min_size ||
382 nat_baseline > reported_nat_size ||
383 min_baseline < 0 ||
384 nat_baseline < 0)
385 {
386 g_warning ("%s %p reported baselines of minimum %d and natural %d, but sizes of "
387 "minimum %d and natural %d. Baselines must be inside the widget size.",
388 G_OBJECT_TYPE_NAME (widget), widget, min_baseline, nat_baseline,
389 reported_min_size, reported_nat_size);
390 min_baseline = -1;
391 nat_baseline = -1;
392 }
393 else
394 {
395 if (css_min_size > reported_min_size)
396 {
397 min_baseline += (css_min_size - reported_min_size) / 2;
398 nat_baseline += (css_min_size - reported_min_size) / 2;
399 }
400
401 min_baseline += margin.top + border.top + padding.top;
402 nat_baseline += margin.top + border.top + padding.top;
403
404 gtk_widget_adjust_baseline_request (widget, &min_baseline, &nat_baseline);
405 }
406 }
407
408 _gtk_size_request_cache_commit (cache,
409 orientation,
410 for_size,
411 min_size,
412 nat_size,
413 min_baseline,
414 nat_baseline);
415 }
416
417 if (minimum)
418 *minimum = min_size;
419
420 if (natural)
421 *natural = nat_size;
422
423 if (minimum_baseline)
424 *minimum_baseline = min_baseline;
425
426 if (natural_baseline)
427 *natural_baseline = nat_baseline;
428
429 g_assert (min_size <= nat_size);
430
431 GTK_DISPLAY_NOTE (_gtk_widget_get_display (widget), SIZE_REQUEST, {
432 GString *s;
433
434 s = g_string_new ("");
435 g_string_append_printf (s, "[%p] %s\t%s: %d is minimum %d and natural: %d",
436 widget, G_OBJECT_TYPE_NAME (widget),
437 orientation == GTK_ORIENTATION_HORIZONTAL
438 ? "width for height"
439 : "height for width",
440 for_size, min_size, nat_size);
441 if (min_baseline != -1 || nat_baseline != -1)
442 {
443 g_string_append_printf (s, ", baseline %d/%d",
444 min_baseline, nat_baseline);
445 }
446 g_string_append_printf (s, " (hit cache: %s)\n",
447 found_in_cache ? "yes" : "no");
448 g_message ("%s", s->str);
449 g_string_free (s, TRUE);
450 });
451 }
452
453 /**
454 * gtk_widget_measure:
455 * @widget: A `GtkWidget` instance
456 * @orientation: the orientation to measure
457 * @for_size: Size for the opposite of @orientation, i.e.
458 * if @orientation is %GTK_ORIENTATION_HORIZONTAL, this is
459 * the height the widget should be measured with. The %GTK_ORIENTATION_VERTICAL
460 * case is analogous. This way, both height-for-width and width-for-height
461 * requests can be implemented. If no size is known, -1 can be passed.
462 * @minimum: (out) (optional): location to store the minimum size
463 * @natural: (out) (optional): location to store the natural size
464 * @minimum_baseline: (out) (optional): location to store the baseline
465 * position for the minimum size
466 * @natural_baseline: (out) (optional): location to store the baseline
467 * position for the natural size
468 *
469 * Measures @widget in the orientation @orientation and for the given @for_size.
470 *
471 * As an example, if @orientation is %GTK_ORIENTATION_HORIZONTAL and @for_size
472 * is 300, this functions will compute the minimum and natural width of @widget
473 * if it is allocated at a height of 300 pixels.
474 *
475 * See [GtkWidget’s geometry management section](class.Widget.html#height-for-width-geometry-management) for
476 * a more details on implementing `GtkWidgetClass.measure()`.
477 */
478 void
gtk_widget_measure(GtkWidget * widget,GtkOrientation orientation,int for_size,int * minimum,int * natural,int * minimum_baseline,int * natural_baseline)479 gtk_widget_measure (GtkWidget *widget,
480 GtkOrientation orientation,
481 int for_size,
482 int *minimum,
483 int *natural,
484 int *minimum_baseline,
485 int *natural_baseline)
486 {
487 g_return_if_fail (GTK_IS_WIDGET (widget));
488 g_return_if_fail (for_size >= -1);
489 g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL ||
490 orientation == GTK_ORIENTATION_VERTICAL);
491
492 /* This is the main function that checks for a cached size and
493 * possibly queries the widget class to compute the size if it's
494 * not cached.
495 */
496 if (!_gtk_widget_get_visible (widget) && !GTK_IS_ROOT (widget))
497 {
498 if (minimum)
499 *minimum = 0;
500 if (natural)
501 *natural = 0;
502 if (minimum_baseline)
503 *minimum_baseline = -1;
504 if (natural_baseline)
505 *natural_baseline = -1;
506 return;
507 }
508
509 if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
510 {
511 gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural,
512 minimum_baseline, natural_baseline);
513 }
514 else
515 {
516 GHashTable *widgets;
517 GHashTableIter iter;
518 gpointer key;
519 int min_result = 0, nat_result = 0;
520
521 widgets = _gtk_size_group_get_widget_peers (widget, orientation);
522
523 g_hash_table_iter_init (&iter, widgets);
524 while (g_hash_table_iter_next (&iter, &key, NULL))
525 {
526 GtkWidget *tmp_widget = key;
527 int min_dimension, nat_dimension;
528
529 gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size,
530 &min_dimension, &nat_dimension, NULL, NULL);
531
532 min_result = MAX (min_result, min_dimension);
533 nat_result = MAX (nat_result, nat_dimension);
534 }
535
536 g_hash_table_destroy (widgets);
537
538 /* Baselines make no sense with sizegroups really */
539 if (minimum_baseline)
540 *minimum_baseline = -1;
541
542 if (natural_baseline)
543 *natural_baseline = -1;
544
545 if (minimum)
546 *minimum = min_result;
547
548 if (natural)
549 *natural = nat_result;
550 }
551 }
552
553 /**
554 * gtk_widget_get_request_mode:
555 * @widget: a `GtkWidget` instance
556 *
557 * Gets whether the widget prefers a height-for-width layout
558 * or a width-for-height layout.
559 *
560 * Single-child widgets generally propagate the preference of
561 * their child, more complex widgets need to request something
562 * either in context of their children or in context of their
563 * allocation capabilities.
564 *
565 * Returns: The `GtkSizeRequestMode` preferred by @widget.
566 */
567 GtkSizeRequestMode
gtk_widget_get_request_mode(GtkWidget * widget)568 gtk_widget_get_request_mode (GtkWidget *widget)
569 {
570 SizeRequestCache *cache;
571
572 cache = _gtk_widget_peek_request_cache (widget);
573
574 if (G_UNLIKELY (!cache->request_mode_valid))
575 {
576 cache->request_mode = fetch_request_mode (widget);
577 cache->request_mode_valid = TRUE;
578 }
579
580 return cache->request_mode;
581 }
582
583 /**
584 * gtk_widget_get_preferred_size:
585 * @widget: a `GtkWidget` instance
586 * @minimum_size: (out) (optional): location for storing the minimum size
587 * @natural_size: (out) (optional): location for storing the natural size
588 *
589 * Retrieves the minimum and natural size of a widget, taking
590 * into account the widget’s preference for height-for-width management.
591 *
592 * This is used to retrieve a suitable size by container widgets which do
593 * not impose any restrictions on the child placement. It can be used
594 * to deduce toplevel window and menu sizes as well as child widgets in
595 * free-form containers such as `GtkFixed`.
596 *
597 * Handle with care. Note that the natural height of a height-for-width
598 * widget will generally be a smaller size than the minimum height, since
599 * the required height for the natural width is generally smaller than the
600 * required height for the minimum width.
601 *
602 * Use [id@gtk_widget_measure] if you want to support baseline alignment.
603 */
604 void
gtk_widget_get_preferred_size(GtkWidget * widget,GtkRequisition * minimum_size,GtkRequisition * natural_size)605 gtk_widget_get_preferred_size (GtkWidget *widget,
606 GtkRequisition *minimum_size,
607 GtkRequisition *natural_size)
608 {
609 int min_width, nat_width;
610 int min_height, nat_height;
611
612 g_return_if_fail (GTK_IS_WIDGET (widget));
613
614 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
615 {
616 gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
617 &min_width, &nat_width, NULL, NULL);
618
619 if (minimum_size)
620 {
621 minimum_size->width = min_width;
622 gtk_widget_measure (widget,
623 GTK_ORIENTATION_VERTICAL, min_width,
624 &minimum_size->height, NULL, NULL, NULL);
625 }
626
627 if (natural_size)
628 {
629 natural_size->width = nat_width;
630 gtk_widget_measure (widget,
631 GTK_ORIENTATION_VERTICAL, nat_width,
632 NULL, &natural_size->height, NULL, NULL);
633 }
634 }
635 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
636 {
637 gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
638 -1, &min_height, &nat_height, NULL, NULL);
639
640 if (minimum_size)
641 {
642 minimum_size->height = min_height;
643 gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, min_height,
644 &minimum_size->width, NULL, NULL, NULL);
645 }
646
647 if (natural_size)
648 {
649 natural_size->height = nat_height;
650 gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, nat_height,
651 NULL, &natural_size->width, NULL, NULL);
652 }
653 }
654 }
655
656 static int
compare_gap(gconstpointer p1,gconstpointer p2,gpointer data)657 compare_gap (gconstpointer p1,
658 gconstpointer p2,
659 gpointer data)
660 {
661 GtkRequestedSize *sizes = data;
662 const guint *c1 = p1;
663 const guint *c2 = p2;
664
665 const int d1 = MAX (sizes[*c1].natural_size -
666 sizes[*c1].minimum_size,
667 0);
668 const int d2 = MAX (sizes[*c2].natural_size -
669 sizes[*c2].minimum_size,
670 0);
671
672 int delta = (d2 - d1);
673
674 if (0 == delta)
675 delta = (*c2 - *c1);
676
677 return delta;
678 }
679
680 /**
681 * gtk_distribute_natural_allocation:
682 * @extra_space: Extra space to redistribute among children after subtracting
683 * minimum sizes and any child padding from the overall allocation
684 * @n_requested_sizes: Number of requests to fit into the allocation
685 * @sizes: (array length=n_requested_sizes): An array of structs with a client pointer and a minimum/natural size
686 * in the orientation of the allocation.
687 *
688 * Distributes @extra_space to child @sizes by bringing smaller
689 * children up to natural size first.
690 *
691 * The remaining space will be added to the @minimum_size member of the
692 * `GtkRequestedSize` struct. If all sizes reach their natural size then
693 * the remaining space is returned.
694 *
695 * Returns: The remainder of @extra_space after redistributing space
696 * to @sizes.
697 */
698 int
gtk_distribute_natural_allocation(int extra_space,guint n_requested_sizes,GtkRequestedSize * sizes)699 gtk_distribute_natural_allocation (int extra_space,
700 guint n_requested_sizes,
701 GtkRequestedSize *sizes)
702 {
703 guint *spreading;
704 int i;
705
706 g_return_val_if_fail (extra_space >= 0, 0);
707
708 if (n_requested_sizes == 0)
709 return extra_space;
710
711 spreading = g_newa (guint, n_requested_sizes);
712
713 for (i = 0; i < n_requested_sizes; i++)
714 spreading[i] = i;
715
716 /* Distribute the container's extra space c_gap. We want to assign
717 * this space such that the sum of extra space assigned to children
718 * (c^i_gap) is equal to c_cap. The case that there's not enough
719 * space for all children to take their natural size needs some
720 * attention. The goals we want to achieve are:
721 *
722 * a) Maximize number of children taking their natural size.
723 * b) The allocated size of children should be a continuous
724 * function of c_gap. That is, increasing the container size by
725 * one pixel should never make drastic changes in the distribution.
726 * c) If child i takes its natural size and child j doesn't,
727 * child j should have received at least as much gap as child i.
728 *
729 * The following code distributes the additional space by following
730 * these rules.
731 */
732
733 /* Sort descending by gap and position. */
734 g_qsort_with_data (spreading,
735 n_requested_sizes, sizeof (guint),
736 compare_gap, sizes);
737
738 /* Distribute available space.
739 * This masterpiece of a loop was conceived by Behdad Esfahbod.
740 */
741 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
742 {
743 /* Divide remaining space by number of remaining children.
744 * Sort order and reducing remaining space by assigned space
745 * ensures that space is distributed equally.
746 */
747 int glue = (extra_space + i) / (i + 1);
748 int gap = sizes[(spreading[i])].natural_size
749 - sizes[(spreading[i])].minimum_size;
750
751 int extra = MIN (glue, gap);
752
753 sizes[spreading[i]].minimum_size += extra;
754
755 extra_space -= extra;
756 }
757
758 return extra_space;
759 }
760