]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtksizerequest.c
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 "deprecated/gtkstyle.h"
33
34
35 #ifndef G_DISABLE_CHECKS
36 static GQuark recursion_check_quark = 0;
37 #endif /* G_DISABLE_CHECKS */
38
39 static void
40 push_recursion_check (GtkWidget       *widget,
41                       GtkOrientation   orientation,
42                       gint             for_size)
43 {
44 #ifndef G_DISABLE_CHECKS
45   const char *previous_method;
46   const char *method;
47
48   if (recursion_check_quark == 0)
49     recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
50
51   previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
52
53   if (orientation == GTK_ORIENTATION_HORIZONTAL)
54     {
55       method = for_size < 0 ? "get_width" : "get_width_for_height";
56     }
57   else
58     {
59       method = for_size < 0 ? "get_height" : "get_height_for_width";
60     }
61
62   if (previous_method != NULL)
63     {
64       g_warning ("%s %p: widget tried to gtk_widget_%s inside "
65                  " GtkWidget     ::%s implementation. "
66                  "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
67                  "directly rather than using gtk_widget_%s",
68                  G_OBJECT_TYPE_NAME (widget), widget,
69                  method, previous_method,
70                  method, method);
71     }
72
73   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
74 #endif /* G_DISABLE_CHECKS */
75 }
76
77 static void
78 pop_recursion_check (GtkWidget       *widget,
79                      GtkOrientation   orientation)
80 {
81 #ifndef G_DISABLE_CHECKS
82   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
83 #endif
84 }
85
86 static const char *
87 get_vfunc_name (GtkOrientation orientation,
88                 gint           for_size)
89 {
90   if (orientation == GTK_ORIENTATION_HORIZONTAL)
91     return for_size < 0 ? "get_preferred_width" : "get_preferred_width_for_height";
92   else
93     return for_size < 0 ? "get_preferred_height" : "get_preferred_height_for_width";
94 }
95
96 static void
97 gtk_widget_query_size_for_orientation (GtkWidget        *widget,
98                                        GtkOrientation    orientation,
99                                        gint              for_size,
100                                        gint             *minimum_size,
101                                        gint             *natural_size)
102 {
103   SizeRequestCache *cache;
104   gint min_size = 0;
105   gint nat_size = 0;
106   gboolean found_in_cache;
107
108   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
109     for_size = -1;
110
111   cache = _gtk_widget_peek_request_cache (widget);
112   found_in_cache = _gtk_size_request_cache_lookup (cache,
113                                                    orientation,
114                                                    for_size,
115                                                    &min_size,
116                                                    &nat_size);
117   
118   if (!found_in_cache)
119     {
120       gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
121
122       gtk_widget_ensure_style (widget);
123
124       if (orientation == GTK_ORIENTATION_HORIZONTAL)
125         {
126           if (for_size < 0)
127             {
128               push_recursion_check (widget, orientation, for_size);
129               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
130               pop_recursion_check (widget, orientation);
131             }
132           else
133             {
134               gint ignored_position = 0;
135               gint minimum_height;
136               gint natural_height;
137
138               /* Pull the base natural height from the cache as it's needed to adjust
139                * the proposed 'for_size' */
140               gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
141
142               /* convert for_size to unadjusted height (for_size is a proposed allocation) */
143               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
144                                                                      GTK_ORIENTATION_VERTICAL,
145                                                                      &minimum_height,
146                                                                      &natural_height,
147                                                                      &ignored_position,
148                                                                      &adjusted_for_size);
149
150               push_recursion_check (widget, orientation, for_size);
151               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, 
152                                                                              MAX (adjusted_for_size, minimum_height),
153                                                                              &min_size, &nat_size);
154               pop_recursion_check (widget, orientation);
155             }
156         }
157       else
158         {
159           if (for_size < 0)
160             {
161               push_recursion_check (widget, orientation, for_size);
162               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
163               pop_recursion_check (widget, orientation);
164             }
165           else
166             {
167               gint ignored_position = 0;
168               gint minimum_width;
169               gint natural_width;
170
171               /* Pull the base natural width from the cache as it's needed to adjust
172                * the proposed 'for_size' */
173               gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
174
175               /* convert for_size to unadjusted width (for_size is a proposed allocation) */
176               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
177                                                                      GTK_ORIENTATION_HORIZONTAL,
178                                                                      &minimum_width,
179                                                                      &natural_width,
180                                                                      &ignored_position,
181                                                                      &adjusted_for_size);
182
183               push_recursion_check (widget, orientation, for_size);
184               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, 
185                                                                              MAX (adjusted_for_size, minimum_width),
186                                                                              &min_size, &nat_size);
187               pop_recursion_check (widget, orientation);
188             }
189         }
190
191       if (min_size > nat_size)
192         {
193           g_warning ("%s %p reported min size %d and natural size %d in %s(); natural size must be >= min size",
194                      G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size, get_vfunc_name (orientation, for_size));
195         }
196
197       adjusted_min     = min_size;
198       adjusted_natural = nat_size;
199       GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
200                                                           orientation,
201                                                           &adjusted_min,
202                                                           &adjusted_natural);
203
204       if (adjusted_min < min_size ||
205           adjusted_natural < nat_size)
206         {
207           g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
208                      G_OBJECT_TYPE_NAME (widget), widget,
209                      orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
210                      adjusted_min, adjusted_natural,
211                      min_size, nat_size);
212           /* don't use the adjustment */
213         }
214       else if (adjusted_min > adjusted_natural)
215         {
216           g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
217                      G_OBJECT_TYPE_NAME (widget), widget,
218                      orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
219                      adjusted_min, adjusted_natural,
220                      min_size, nat_size);
221           /* don't use the adjustment */
222         }
223       else
224         {
225           /* adjustment looks good */
226           min_size = adjusted_min;
227           nat_size = adjusted_natural;
228         }
229
230       _gtk_size_request_cache_commit (cache,
231                                       orientation,
232                                       for_size,
233                                       min_size,
234                                       nat_size);
235     }
236
237   if (minimum_size)
238     *minimum_size = min_size;
239
240   if (natural_size)
241     *natural_size = nat_size;
242
243   g_assert (min_size <= nat_size);
244
245   GTK_NOTE (SIZE_REQUEST,
246             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
247                      widget, G_OBJECT_TYPE_NAME (widget),
248                      orientation == GTK_ORIENTATION_HORIZONTAL ?
249                      "width for height" : "height for width" ,
250                      for_size, min_size, nat_size,
251                      found_in_cache ? "yes" : "no"));
252 }
253
254 /* This is the main function that checks for a cached size and
255  * possibly queries the widget class to compute the size if it's
256  * not cached. If the for_size here is -1, then get_preferred_width()
257  * or get_preferred_height() will be used.
258  */
259 void
260 _gtk_widget_compute_size_for_orientation (GtkWidget        *widget,
261                                           GtkOrientation    orientation,
262                                           gint              for_size,
263                                           gint             *minimum,
264                                           gint             *natural)
265 {
266   GHashTable *widgets;
267   GHashTableIter iter;
268   gpointer key;
269   gint    min_result = 0, nat_result = 0;
270
271   if (!gtk_widget_get_visible (widget) && !gtk_widget_is_toplevel (widget))
272     {
273       if (minimum)
274         *minimum = 0;
275       if (natural)
276         *natural = 0;
277       return;
278     }
279
280   if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
281     {
282       gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural);
283       return;
284     }
285
286   widgets = _gtk_size_group_get_widget_peers (widget, orientation);
287
288   g_hash_table_foreach (widgets, (GHFunc) g_object_ref, NULL);
289   
290   g_hash_table_iter_init (&iter, widgets);
291   while (g_hash_table_iter_next (&iter, &key, NULL))
292     {
293       GtkWidget *tmp_widget = key;
294       gint min_dimension, nat_dimension;
295
296       gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size, &min_dimension, &nat_dimension);
297
298       min_result = MAX (min_result, min_dimension);
299       nat_result = MAX (nat_result, nat_dimension);
300     }
301
302   g_hash_table_foreach (widgets, (GHFunc) g_object_unref, NULL);
303
304   g_hash_table_destroy (widgets);
305
306   if (minimum)
307     *minimum = min_result;
308
309   if (natural)
310     *natural = nat_result;
311 }
312
313 /**
314  * gtk_widget_get_request_mode:
315  * @widget: a #GtkWidget instance
316  *
317  * Gets whether the widget prefers a height-for-width layout
318  * or a width-for-height layout.
319  *
320  * <note><para>#GtkBin widgets generally propagate the preference of
321  * their child, container widgets need to request something either in
322  * context of their children or in context of their allocation
323  * capabilities.</para></note>
324  *
325  * Returns: The #GtkSizeRequestMode preferred by @widget.
326  *
327  * Since: 3.0
328  */
329 GtkSizeRequestMode
330 gtk_widget_get_request_mode (GtkWidget *widget)
331 {
332   SizeRequestCache *cache;
333
334   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
335
336   cache = _gtk_widget_peek_request_cache (widget);
337
338   if (!cache->request_mode_valid)
339     {
340       cache->request_mode = GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
341       cache->request_mode_valid = TRUE;
342     }
343
344   return cache->request_mode;
345 }
346
347 /**
348  * gtk_widget_get_preferred_width:
349  * @widget: a #GtkWidget instance
350  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
351  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
352  *
353  * Retrieves a widget's initial minimum and natural width.
354  *
355  * <note><para>This call is specific to height-for-width
356  * requests.</para></note>
357  *
358  * The returned request will be modified by the
359  * GtkWidgetClass::adjust_size_request virtual method and by any
360  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
361  * is the one that should be used for layout, not necessarily the one
362  * returned by the widget itself.
363  *
364  * Since: 3.0
365  */
366 void
367 gtk_widget_get_preferred_width (GtkWidget *widget,
368                                 gint      *minimum_width,
369                                 gint      *natural_width)
370 {
371   g_return_if_fail (GTK_IS_WIDGET (widget));
372   g_return_if_fail (minimum_width != NULL || natural_width != NULL);
373
374   _gtk_widget_compute_size_for_orientation (widget,
375                                             GTK_ORIENTATION_HORIZONTAL,
376                                             -1,
377                                             minimum_width,
378                                             natural_width);
379 }
380
381
382 /**
383  * gtk_widget_get_preferred_height:
384  * @widget: a #GtkWidget instance
385  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
386  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
387  *
388  * Retrieves a widget's initial minimum and natural height.
389  *
390  * <note><para>This call is specific to width-for-height requests.</para></note>
391  *
392  * The returned request will be modified by the
393  * GtkWidgetClass::adjust_size_request virtual method and by any
394  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
395  * is the one that should be used for layout, not necessarily the one
396  * returned by the widget itself.
397  *
398  * Since: 3.0
399  */
400 void
401 gtk_widget_get_preferred_height (GtkWidget *widget,
402                                  gint      *minimum_height,
403                                  gint      *natural_height)
404 {
405   g_return_if_fail (GTK_IS_WIDGET (widget));
406   g_return_if_fail (minimum_height != NULL || natural_height != NULL);
407
408   _gtk_widget_compute_size_for_orientation (widget,
409                                             GTK_ORIENTATION_VERTICAL,
410                                             -1,
411                                             minimum_height,
412                                             natural_height);
413 }
414
415
416
417 /**
418  * gtk_widget_get_preferred_width_for_height:
419  * @widget: a #GtkWidget instance
420  * @height: the height which is available for allocation
421  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
422  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
423  *
424  * Retrieves a widget's minimum and natural width if it would be given
425  * the specified @height.
426  *
427  * The returned request will be modified by the
428  * GtkWidgetClass::adjust_size_request virtual method and by any
429  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
430  * is the one that should be used for layout, not necessarily the one
431  * returned by the widget itself.
432  *
433  * Since: 3.0
434  */
435 void
436 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
437                                            gint       height,
438                                            gint      *minimum_width,
439                                            gint      *natural_width)
440 {
441   g_return_if_fail (GTK_IS_WIDGET (widget));
442   g_return_if_fail (minimum_width != NULL || natural_width != NULL);
443   g_return_if_fail (height >= 0);
444
445   _gtk_widget_compute_size_for_orientation (widget,
446                                             GTK_ORIENTATION_HORIZONTAL,
447                                             height,
448                                             minimum_width,
449                                             natural_width);
450 }
451
452 /**
453  * gtk_widget_get_preferred_height_for_width:
454  * @widget: a #GtkWidget instance
455  * @width: the width which is available for allocation
456  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
457  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
458  *
459  * Retrieves a widget's minimum and natural height if it would be given
460  * the specified @width.
461  *
462  * The returned request will be modified by the
463  * GtkWidgetClass::adjust_size_request virtual method and by any
464  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
465  * is the one that should be used for layout, not necessarily the one
466  * returned by the widget itself.
467  *
468  * Since: 3.0
469  */
470 void
471 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
472                                            gint       width,
473                                            gint      *minimum_height,
474                                            gint      *natural_height)
475 {
476   g_return_if_fail (GTK_IS_WIDGET (widget));
477   g_return_if_fail (minimum_height != NULL || natural_height != NULL);
478   g_return_if_fail (width >= 0);
479
480   _gtk_widget_compute_size_for_orientation (widget,
481                                             GTK_ORIENTATION_VERTICAL,
482                                             width,
483                                             minimum_height,
484                                             natural_height);
485 }
486
487 /**
488  * gtk_widget_get_preferred_size:
489  * @widget: a #GtkWidget instance
490  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
491  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
492  *
493  * Retrieves the minimum and natural size of a widget, taking
494  * into account the widget's preference for height-for-width management.
495  *
496  * This is used to retrieve a suitable size by container widgets which do
497  * not impose any restrictions on the child placement. It can be used
498  * to deduce toplevel window and menu sizes as well as child widgets in
499  * free-form containers such as GtkLayout.
500  *
501  * <note><para>Handle with care. Note that the natural height of a height-for-width
502  * widget will generally be a smaller size than the minimum height, since the required
503  * height for the natural width is generally smaller than the required height for
504  * the minimum width.</para></note>
505  *
506  * Since: 3.0
507  */
508 void
509 gtk_widget_get_preferred_size (GtkWidget      *widget,
510                                GtkRequisition *minimum_size,
511                                GtkRequisition *natural_size)
512 {
513   gint min_width, nat_width;
514   gint min_height, nat_height;
515
516   g_return_if_fail (GTK_IS_WIDGET (widget));
517
518   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
519     {
520       gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
521
522       if (minimum_size)
523         {
524           minimum_size->width = min_width;
525           gtk_widget_get_preferred_height_for_width (widget, min_width,
526                                                      &minimum_size->height, NULL);
527         }
528
529       if (natural_size)
530         {
531           natural_size->width = nat_width;
532           gtk_widget_get_preferred_height_for_width (widget, nat_width,
533                                                      NULL, &natural_size->height);
534         }
535     }
536   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
537     {
538       gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
539
540       if (minimum_size)
541         {
542           minimum_size->height = min_height;
543           gtk_widget_get_preferred_width_for_height (widget, min_height,
544                                                      &minimum_size->width, NULL);
545         }
546
547       if (natural_size)
548         {
549           natural_size->height = nat_height;
550           gtk_widget_get_preferred_width_for_height (widget, nat_height,
551                                                      NULL, &natural_size->width);
552         }
553     }
554 }
555
556
557 static gint
558 compare_gap (gconstpointer p1,
559              gconstpointer p2,
560              gpointer      data)
561 {
562   GtkRequestedSize *sizes = data;
563   const guint *c1 = p1;
564   const guint *c2 = p2;
565
566   const gint d1 = MAX (sizes[*c1].natural_size -
567                        sizes[*c1].minimum_size,
568                        0);
569   const gint d2 = MAX (sizes[*c2].natural_size -
570                        sizes[*c2].minimum_size,
571                        0);
572
573   gint delta = (d2 - d1);
574
575   if (0 == delta)
576     delta = (*c2 - *c1);
577
578   return delta;
579 }
580
581 /**
582  * gtk_distribute_natural_allocation:
583  * @extra_space: Extra space to redistribute among children after subtracting
584  *               minimum sizes and any child padding from the overall allocation
585  * @n_requested_sizes: Number of requests to fit into the allocation
586  * @sizes: An array of structs with a client pointer and a minimum/natural size
587  *         in the orientation of the allocation.
588  *
589  * Distributes @extra_space to child @sizes by bringing smaller
590  * children up to natural size first.
591  *
592  * The remaining space will be added to the @minimum_size member of the
593  * GtkRequestedSize struct. If all sizes reach their natural size then
594  * the remaining space is returned.
595  *
596  * Returns: The remainder of @extra_space after redistributing space
597  * to @sizes.
598  */
599 gint
600 gtk_distribute_natural_allocation (gint              extra_space,
601                                    guint             n_requested_sizes,
602                                    GtkRequestedSize *sizes)
603 {
604   guint *spreading;
605   gint   i;
606
607   g_return_val_if_fail (extra_space >= 0, 0);
608
609   spreading = g_newa (guint, n_requested_sizes);
610
611   for (i = 0; i < n_requested_sizes; i++)
612     spreading[i] = i;
613
614   /* Distribute the container's extra space c_gap. We want to assign
615    * this space such that the sum of extra space assigned to children
616    * (c^i_gap) is equal to c_cap. The case that there's not enough
617    * space for all children to take their natural size needs some
618    * attention. The goals we want to achieve are:
619    *
620    *   a) Maximize number of children taking their natural size.
621    *   b) The allocated size of children should be a continuous
622    *   function of c_gap.  That is, increasing the container size by
623    *   one pixel should never make drastic changes in the distribution.
624    *   c) If child i takes its natural size and child j doesn't,
625    *   child j should have received at least as much gap as child i.
626    *
627    * The following code distributes the additional space by following
628    * these rules.
629    */
630
631   /* Sort descending by gap and position. */
632   g_qsort_with_data (spreading,
633                      n_requested_sizes, sizeof (guint),
634                      compare_gap, sizes);
635
636   /* Distribute available space.
637    * This master piece of a loop was conceived by Behdad Esfahbod.
638    */
639   for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
640     {
641       /* Divide remaining space by number of remaining children.
642        * Sort order and reducing remaining space by assigned space
643        * ensures that space is distributed equally.
644        */
645       gint glue = (extra_space + i) / (i + 1);
646       gint gap = sizes[(spreading[i])].natural_size
647         - sizes[(spreading[i])].minimum_size;
648
649       gint extra = MIN (glue, gap);
650
651       sizes[spreading[i]].minimum_size += extra;
652
653       extra_space -= extra;
654     }
655
656   return extra_space;
657 }