]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellareaboxcontext.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcellareaboxcontext.c
1 /* gtkcellareaboxcontext.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.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 #include "gtkintl.h"
24 #include "gtkcellareabox.h"
25 #include "gtkcellareaboxcontextprivate.h"
26 #include "gtkorientable.h"
27
28 /* GObjectClass */
29 static void      _gtk_cell_area_box_context_finalize              (GObject               *object);
30
31 /* GtkCellAreaContextClass */
32 static void      _gtk_cell_area_box_context_reset                 (GtkCellAreaContext    *context);
33 static void      _gtk_cell_area_box_context_get_preferred_height_for_width (GtkCellAreaContext *context,
34                                                                            gint                width,
35                                                                            gint               *minimum_height,
36                                                                            gint               *natural_height);
37 static void      _gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
38                                                                            gint                height,
39                                                                            gint               *minimum_width,
40                                                                            gint               *natural_width);
41
42
43
44 /* Internal functions */
45 static void      _gtk_cell_area_box_context_sum                  (GtkCellAreaBoxContext  *context,
46                                                                  GtkOrientation          orientation,
47                                                                  gint                    for_size,
48                                                                  gint                   *minimum_size,
49                                                                  gint                   *natural_size);
50 static void      free_cache_array                                (GArray                *array);
51 static GArray   *group_array_new                                 (GtkCellAreaBoxContext *context);
52 static GArray   *get_array                                       (GtkCellAreaBoxContext *context,
53                                                                   GtkOrientation         orientation,
54                                                                   gint                   for_size);
55 static gboolean  group_expands                                   (GtkCellAreaBoxContext *context,
56                                                                   gint                   group_idx);
57 static gint      count_expand_groups                             (GtkCellAreaBoxContext *context);
58
59
60 /* CachedSize management */
61 typedef struct {
62   gint     min_size;
63   gint     nat_size;
64 } CachedSize;
65
66 struct _GtkCellAreaBoxContextPrivate
67 {
68   /* Table of per renderer CachedSizes */
69   GArray *base_widths;
70   GArray *base_heights;
71
72   /* Table of per height/width hash tables of per renderer CachedSizes */
73   GHashTable *widths;
74   GHashTable *heights;
75
76   /* Whether each group expands */
77   gboolean  *expand;
78
79   /* Whether each group is aligned */
80   gboolean  *align;
81 };
82
83 G_DEFINE_TYPE (GtkCellAreaBoxContext, _gtk_cell_area_box_context, GTK_TYPE_CELL_AREA_CONTEXT);
84
85 static void
86 free_cache_array (GArray *array)
87 {
88   g_array_free (array, TRUE);
89 }
90
91 static GArray *
92 group_array_new (GtkCellAreaBoxContext *context)
93 {
94   GtkCellAreaBoxContextPrivate *priv = context->priv;
95   GArray *group_array;
96
97   group_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
98   g_array_set_size (group_array, priv->base_widths->len);
99
100   return group_array;
101 }
102
103 static GArray *
104 get_array (GtkCellAreaBoxContext *context,
105            GtkOrientation         orientation,
106            gint                   for_size)
107 {
108   GtkCellAreaBoxContextPrivate *priv = context->priv;
109   GArray                       *array;
110
111   if (for_size < 0)
112     {
113       if (orientation == GTK_ORIENTATION_HORIZONTAL)
114         array = priv->base_widths;
115       else
116         array = priv->base_heights;
117     }
118   else
119     {
120       if (orientation == GTK_ORIENTATION_HORIZONTAL)
121         {
122           array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_size));
123
124           if (!array)
125             array = priv->base_widths;
126         }
127       else
128         {
129           array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_size));
130
131           if (!array)
132             array = priv->base_heights;
133         }
134     }
135
136   return array;
137 }
138
139 static gboolean 
140 group_expands (GtkCellAreaBoxContext *context,
141                gint                   group_idx)
142 {
143   GtkCellAreaBoxContextPrivate *priv = context->priv;
144
145   g_assert (group_idx >= 0 && group_idx < priv->base_widths->len);
146
147   return priv->expand[group_idx];
148 }
149
150 static gint
151 count_expand_groups (GtkCellAreaBoxContext *context)
152 {
153   GtkCellAreaBoxContextPrivate *priv = context->priv;
154   gint i, expand = 0;
155
156   for (i = 0; i < priv->base_widths->len; i++)
157     {
158       if (priv->expand[i])
159         expand++;
160     }
161   
162   return expand;
163 }
164
165 static void
166 _gtk_cell_area_box_context_init (GtkCellAreaBoxContext *box_context)
167 {
168   GtkCellAreaBoxContextPrivate *priv;
169
170   box_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (box_context,
171                                                    GTK_TYPE_CELL_AREA_BOX_CONTEXT,
172                                                    GtkCellAreaBoxContextPrivate);
173   priv = box_context->priv;
174
175   priv->base_widths  = g_array_new (FALSE, TRUE, sizeof (CachedSize));
176   priv->base_heights = g_array_new (FALSE, TRUE, sizeof (CachedSize));
177
178   priv->widths       = g_hash_table_new_full (g_direct_hash, g_direct_equal,
179                                               NULL, (GDestroyNotify)free_cache_array);
180   priv->heights      = g_hash_table_new_full (g_direct_hash, g_direct_equal,
181                                               NULL, (GDestroyNotify)free_cache_array);
182 }
183
184 static void 
185 _gtk_cell_area_box_context_class_init (GtkCellAreaBoxContextClass *class)
186 {
187   GObjectClass            *object_class = G_OBJECT_CLASS (class);
188   GtkCellAreaContextClass *context_class   = GTK_CELL_AREA_CONTEXT_CLASS (class);
189
190   /* GObjectClass */
191   object_class->finalize = _gtk_cell_area_box_context_finalize;
192
193   context_class->reset                          = _gtk_cell_area_box_context_reset;
194   context_class->get_preferred_height_for_width = _gtk_cell_area_box_context_get_preferred_height_for_width;
195   context_class->get_preferred_width_for_height = _gtk_cell_area_box_context_get_preferred_width_for_height;
196
197   g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxContextPrivate));
198 }
199
200 /*************************************************************
201  *                      GObjectClass                         *
202  *************************************************************/
203 static void
204 _gtk_cell_area_box_context_finalize (GObject *object)
205 {
206   GtkCellAreaBoxContext        *box_context = GTK_CELL_AREA_BOX_CONTEXT (object);
207   GtkCellAreaBoxContextPrivate *priv        = box_context->priv;
208
209   g_array_free (priv->base_widths, TRUE);
210   g_array_free (priv->base_heights, TRUE);
211   g_hash_table_destroy (priv->widths);
212   g_hash_table_destroy (priv->heights);
213
214   g_free (priv->expand);
215   g_free (priv->align);
216
217   G_OBJECT_CLASS (_gtk_cell_area_box_context_parent_class)->finalize (object);
218 }
219
220 /*************************************************************
221  *                    GtkCellAreaContextClass                *
222  *************************************************************/
223 static void
224 _gtk_cell_area_box_context_reset (GtkCellAreaContext *context)
225 {
226   GtkCellAreaBoxContext        *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
227   GtkCellAreaBoxContextPrivate *priv        = box_context->priv;
228   CachedSize                   *size;
229   gint                          i;
230
231   for (i = 0; i < priv->base_widths->len; i++)
232     {
233       size = &g_array_index (priv->base_widths, CachedSize, i);
234
235       size->min_size = 0;
236       size->nat_size = 0;
237
238       size = &g_array_index (priv->base_heights, CachedSize, i);
239
240       size->min_size = 0;
241       size->nat_size = 0;
242     }
243
244   /* Reset context sizes as well */
245   g_hash_table_remove_all (priv->widths);
246   g_hash_table_remove_all (priv->heights);
247
248   GTK_CELL_AREA_CONTEXT_CLASS
249     (_gtk_cell_area_box_context_parent_class)->reset (context);
250 }
251
252 static void
253 _gtk_cell_area_box_context_sum (GtkCellAreaBoxContext *context,
254                                GtkOrientation         orientation,
255                                gint                   for_size,
256                                gint                  *minimum_size,
257                                gint                  *natural_size)
258 {
259   GtkCellAreaBoxContextPrivate *priv = context->priv;
260   GtkCellAreaBox *area;
261   GtkOrientation  box_orientation;
262   GArray         *array;
263   gint            spacing, i, last_aligned_group_idx;
264   gint            min_size = 0, nat_size = 0;
265
266   area            = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (context));
267   spacing         = gtk_cell_area_box_get_spacing (area);
268   box_orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (area));
269   array           = get_array (context, orientation, for_size);
270
271   /* Get the last visible aligned group 
272    * (we need to get space at least up till this group) */
273   for (i = array->len - 1; i >= 0; i--)
274     {
275       if (priv->align[i] && 
276           _gtk_cell_area_box_group_visible (area, i))
277         break;
278     }
279   last_aligned_group_idx = i >= 0 ? i : 0;
280
281   for (i = 0; i < array->len; i++)
282     {
283       CachedSize *size = &g_array_index (array, CachedSize, i);
284
285       if (box_orientation == orientation)
286         {
287           if (i > last_aligned_group_idx &&
288               !_gtk_cell_area_box_group_visible (area, i))
289             continue;
290
291           /* Dont add spacing for 0 size groups, they can be 0 size because
292            * they contain only invisible cells for this round of requests
293            */
294           if (min_size > 0 && size->nat_size > 0)
295             {
296               min_size += spacing;
297               nat_size += spacing;
298             }
299
300           min_size += size->min_size;
301           nat_size += size->nat_size;
302         }
303       else
304         {
305           min_size = MAX (min_size, size->min_size);
306           nat_size = MAX (nat_size, size->nat_size);
307         }
308     }
309
310   if (for_size < 0)
311     {
312       if (orientation == GTK_ORIENTATION_HORIZONTAL)
313         gtk_cell_area_context_push_preferred_width (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
314       else
315         gtk_cell_area_context_push_preferred_height (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
316     }
317
318   if (minimum_size)
319     *minimum_size = min_size;
320   if (natural_size)
321     *natural_size = nat_size;
322 }
323
324 static void
325 _gtk_cell_area_box_context_get_preferred_height_for_width (GtkCellAreaContext *context,
326                                                           gint                width,
327                                                           gint               *minimum_height,
328                                                           gint               *natural_height)
329 {
330   _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_VERTICAL, 
331                                  width, minimum_height, natural_height);
332 }
333
334 static void
335 _gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
336                                                           gint                height,
337                                                           gint               *minimum_width,
338                                                           gint               *natural_width)
339 {
340   _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_HORIZONTAL, 
341                                  height, minimum_width, natural_width);
342 }
343
344 /*************************************************************
345  *                            API                            *
346  *************************************************************/
347 static void
348 copy_size_array (GArray *src_array,
349                  GArray *dest_array)
350 {
351   gint i;
352
353   for (i = 0; i < src_array->len; i++)
354     {
355       CachedSize *src  = &g_array_index (src_array, CachedSize, i);
356       CachedSize *dest = &g_array_index (dest_array, CachedSize, i);
357
358       memcpy (dest, src, sizeof (CachedSize));
359     }
360 }
361
362 static void
363 for_size_copy (gpointer    key,
364                GArray     *size_array,
365                GHashTable *dest_hash)
366 {
367   GArray *new_array;
368
369   new_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
370   g_array_set_size (new_array, size_array->len);
371
372   copy_size_array (size_array, new_array);
373
374   g_hash_table_insert (dest_hash, key, new_array);
375 }
376
377 GtkCellAreaBoxContext *
378 _gtk_cell_area_box_context_copy (GtkCellAreaBox        *box,
379                                 GtkCellAreaBoxContext *context)
380 {
381   GtkCellAreaBoxContext *copy;
382
383   copy = g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
384                        "area", box, NULL);
385
386   _gtk_cell_area_box_init_groups (copy,
387                                   context->priv->base_widths->len,
388                                   context->priv->expand,
389                                   context->priv->align);
390
391   /* Copy the base arrays */
392   copy_size_array (context->priv->base_widths,
393                    copy->priv->base_widths);
394   copy_size_array (context->priv->base_heights,
395                    copy->priv->base_heights);
396
397   /* Copy each for size */
398   g_hash_table_foreach (context->priv->heights,
399                         (GHFunc)for_size_copy, copy->priv->heights);
400   g_hash_table_foreach (context->priv->widths,
401                         (GHFunc)for_size_copy, copy->priv->widths);
402
403
404   return copy;
405 }
406
407 void
408 _gtk_cell_area_box_init_groups (GtkCellAreaBoxContext *box_context,
409                                 guint                  n_groups,
410                                 gboolean              *expand_groups,
411                                 gboolean              *align_groups)
412 {
413   GtkCellAreaBoxContextPrivate *priv;
414
415   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
416   g_return_if_fail (n_groups == 0 || expand_groups != NULL);
417
418   /* When the group dimensions change, all info must be reset
419    * Note this already clears the min/nat values on the CachedSizes
420    */
421   gtk_cell_area_context_reset (GTK_CELL_AREA_CONTEXT (box_context));
422
423   priv = box_context->priv;
424   g_array_set_size (priv->base_widths,  n_groups);
425   g_array_set_size (priv->base_heights, n_groups);
426
427   g_free (priv->expand);
428   priv->expand = g_memdup (expand_groups, n_groups * sizeof (gboolean));
429
430   g_free (priv->align);
431   priv->align = g_memdup (align_groups, n_groups * sizeof (gboolean));
432 }
433
434 void
435 _gtk_cell_area_box_context_push_group_width (GtkCellAreaBoxContext *box_context,
436                                             gint                   group_idx,
437                                             gint                   minimum_width,
438                                             gint                   natural_width)
439 {
440   GtkCellAreaBoxContextPrivate *priv;
441   CachedSize                   *size;
442   gboolean                      grew = FALSE;
443
444   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
445
446   priv = box_context->priv;
447   g_return_if_fail (group_idx < priv->base_widths->len);
448
449   size = &g_array_index (priv->base_widths, CachedSize, group_idx);
450   if (minimum_width > size->min_size)
451     {
452       size->min_size = minimum_width;
453       grew = TRUE;
454     }
455   if (natural_width > size->nat_size)
456     {
457       size->nat_size = natural_width;
458       grew = TRUE;
459     }
460
461   if (grew)
462     _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_HORIZONTAL, -1, NULL, NULL);
463 }
464
465 void
466 _gtk_cell_area_box_context_push_group_height_for_width  (GtkCellAreaBoxContext *box_context,
467                                                         gint                   group_idx,
468                                                         gint                   for_width,
469                                                         gint                   minimum_height,
470                                                         gint                   natural_height)
471 {
472   GtkCellAreaBoxContextPrivate *priv;
473   GArray                       *group_array;
474   CachedSize                   *size;
475
476   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
477
478   priv = box_context->priv;
479   g_return_if_fail (group_idx < priv->base_widths->len);
480
481   group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));
482   if (!group_array)
483     {
484       group_array = group_array_new (box_context);
485       g_hash_table_insert (priv->heights, GINT_TO_POINTER (for_width), group_array);
486     }
487
488   size = &g_array_index (group_array, CachedSize, group_idx);
489   size->min_size = MAX (size->min_size, minimum_height);
490   size->nat_size = MAX (size->nat_size, natural_height);
491 }
492
493 void
494 _gtk_cell_area_box_context_push_group_height (GtkCellAreaBoxContext *box_context,
495                                              gint                   group_idx,
496                                              gint                   minimum_height,
497                                              gint                   natural_height)
498 {
499   GtkCellAreaBoxContextPrivate *priv;
500   CachedSize                   *size;
501   gboolean                      grew = FALSE;
502
503   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
504
505   priv = box_context->priv;
506   g_return_if_fail (group_idx < priv->base_heights->len);
507
508   size = &g_array_index (priv->base_heights, CachedSize, group_idx);
509   if (minimum_height > size->min_size)
510     {
511       size->min_size = minimum_height;
512       grew = TRUE;
513     }
514   if (natural_height > size->nat_size)
515     {
516       size->nat_size = natural_height;
517       grew = TRUE;
518     }
519
520   if (grew)
521     _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_VERTICAL, -1, NULL, NULL);
522 }
523
524 void
525 _gtk_cell_area_box_context_push_group_width_for_height (GtkCellAreaBoxContext *box_context,
526                                                        gint                   group_idx,
527                                                        gint                   for_height,
528                                                        gint                   minimum_width,
529                                                        gint                   natural_width)
530 {
531   GtkCellAreaBoxContextPrivate *priv;
532   GArray                       *group_array;
533   CachedSize                   *size;
534
535   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
536
537   priv        = box_context->priv;
538   g_return_if_fail (group_idx < priv->base_widths->len);
539
540   group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));
541   if (!group_array)
542     {
543       group_array = group_array_new (box_context);
544       g_hash_table_insert (priv->widths, GINT_TO_POINTER (for_height), group_array);
545     }
546
547   size = &g_array_index (group_array, CachedSize, group_idx);
548   size->min_size = MAX (size->min_size, minimum_width);
549   size->nat_size = MAX (size->nat_size, natural_width);
550 }
551
552 void
553 _gtk_cell_area_box_context_get_group_width (GtkCellAreaBoxContext *box_context,
554                                            gint                   group_idx,
555                                            gint                  *minimum_width,
556                                            gint                  *natural_width)
557 {
558   GtkCellAreaBoxContextPrivate *priv;
559   CachedSize                   *size;
560
561   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
562
563   priv = box_context->priv;
564   g_return_if_fail (group_idx < priv->base_widths->len);
565
566   size = &g_array_index (priv->base_widths, CachedSize, group_idx);
567
568   if (minimum_width)
569     *minimum_width = size->min_size;
570
571   if (natural_width)
572     *natural_width = size->nat_size;
573 }
574
575 void
576 _gtk_cell_area_box_context_get_group_height_for_width (GtkCellAreaBoxContext *box_context,
577                                                       gint                   group_idx,
578                                                       gint                   for_width,
579                                                       gint                  *minimum_height,
580                                                       gint                  *natural_height)
581 {
582   GtkCellAreaBoxContextPrivate *priv;
583   GArray                    *group_array;
584
585   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
586
587   priv        = box_context->priv;
588   g_return_if_fail (group_idx < priv->base_widths->len);
589
590   group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));
591
592   if (group_array)
593     {
594       CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);
595
596       if (minimum_height)
597         *minimum_height = size->min_size;
598
599       if (natural_height)
600         *natural_height = size->nat_size;
601     }
602   else
603     {
604       if (minimum_height)
605         *minimum_height = -1;
606
607       if (natural_height)
608         *natural_height = -1;
609     }
610 }
611
612 void
613 _gtk_cell_area_box_context_get_group_height (GtkCellAreaBoxContext *box_context,
614                                             gint                   group_idx,
615                                             gint                  *minimum_height,
616                                             gint                  *natural_height)
617 {
618   GtkCellAreaBoxContextPrivate *priv;
619   CachedSize                   *size;
620
621   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
622
623   priv = box_context->priv;
624   g_return_if_fail (group_idx < priv->base_heights->len);
625
626   size = &g_array_index (priv->base_heights, CachedSize, group_idx);
627
628   if (minimum_height)
629     *minimum_height = size->min_size;
630
631   if (natural_height)
632     *natural_height = size->nat_size;
633 }
634
635 void
636 _gtk_cell_area_box_context_get_group_width_for_height (GtkCellAreaBoxContext *box_context,
637                                                       gint                   group_idx,
638                                                       gint                   for_height,
639                                                       gint                  *minimum_width,
640                                                       gint                  *natural_width)
641 {
642   GtkCellAreaBoxContextPrivate *priv;
643   GArray                       *group_array;
644
645   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
646
647   priv = box_context->priv;
648   g_return_if_fail (group_idx < priv->base_widths->len);
649
650   group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));
651
652   if (group_array)
653     {
654       CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);
655
656       if (minimum_width)
657         *minimum_width = size->min_size;
658
659       if (natural_width)
660         *natural_width = size->nat_size;
661     }
662   else
663     {
664       if (minimum_width)
665         *minimum_width = -1;
666
667       if (natural_width)
668         *natural_width = -1;
669     }
670 }
671
672 static GtkRequestedSize *
673 _gtk_cell_area_box_context_get_requests (GtkCellAreaBoxContext *box_context,
674                                         GtkCellAreaBox        *area,
675                                         GtkOrientation         orientation,
676                                         gint                   for_size,
677                                         gint                  *n_requests)
678 {
679   GtkCellAreaBoxContextPrivate *priv = box_context->priv;
680   GtkRequestedSize             *requests;
681   GArray                       *array;
682   CachedSize                   *size;
683   gint                          visible_groups = 0;
684   gint                          last_aligned_group_idx = 0;
685   gint                          i, j;
686
687   /* Get the last visible aligned group 
688    * (we need to get space at least up till this group) */
689   for (i = priv->base_widths->len - 1; i >= 0; i--)
690     {
691       if (priv->align[i] && 
692           _gtk_cell_area_box_group_visible (area, i))
693         break;
694     }
695   last_aligned_group_idx = i >= 0 ? i : 0;
696
697   priv  = box_context->priv;
698   array = get_array (box_context, orientation, for_size);
699
700   for (i = 0; i < array->len; i++)
701     {
702       size = &g_array_index (array, CachedSize, i);
703
704       if (size->nat_size > 0 &&
705           (i <= last_aligned_group_idx ||
706            _gtk_cell_area_box_group_visible (area, i)))
707         visible_groups++;
708     }
709
710   requests = g_new (GtkRequestedSize, visible_groups);
711
712   for (j = 0, i = 0; i < array->len; i++)
713     {
714       size = &g_array_index (array, CachedSize, i);
715
716       if (size->nat_size > 0 &&
717           (i <= last_aligned_group_idx ||
718            _gtk_cell_area_box_group_visible (area, i)))
719         {
720           requests[j].data         = GINT_TO_POINTER (i);
721           requests[j].minimum_size = size->min_size;
722           requests[j].natural_size = size->nat_size;
723           j++;
724         }
725     }
726
727   if (n_requests)
728     *n_requests = visible_groups;
729
730   return requests;
731 }
732
733 static GtkCellAreaBoxAllocation *
734 allocate_for_orientation (GtkCellAreaBoxContext *context,
735                           GtkCellAreaBox        *area,
736                           GtkOrientation         orientation,
737                           gint                   spacing,
738                           gint                   size,
739                           gint                   for_size,
740                           gint                  *n_allocs)
741 {
742   GtkCellAreaBoxContextPrivate *priv = context->priv;
743   GtkCellAreaBoxAllocation     *allocs;
744   GtkRequestedSize             *sizes;
745   gint                          n_expand_groups = 0;
746   gint                          i, n_groups, position, vis_position;
747   gint                          extra_size, extra_extra;
748   gint                          avail_size = size;
749
750   sizes           = _gtk_cell_area_box_context_get_requests (context, area, orientation, for_size, &n_groups);
751   n_expand_groups = count_expand_groups (context);
752
753   /* First start by naturally allocating space among groups */
754   avail_size -= (n_groups - 1) * spacing;
755   for (i = 0; i < n_groups; i++)
756     avail_size -= sizes[i].minimum_size;
757
758   if (avail_size > 0)
759     avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, sizes);
760   else
761     avail_size = 0;
762
763   /* Calculate/distribute expand for groups */
764   if (n_expand_groups > 0)
765     {
766       extra_size  = avail_size / n_expand_groups;
767       extra_extra = avail_size % n_expand_groups;
768     }
769   else
770     extra_size = extra_extra = 0;
771
772   allocs = g_new (GtkCellAreaBoxAllocation, n_groups);
773
774   for (vis_position = 0, position = 0, i = 0; i < n_groups; i++)
775     {
776       allocs[i].group_idx = GPOINTER_TO_INT (sizes[i].data);
777
778       if (priv->align[allocs[i].group_idx])
779         vis_position = position;
780
781       allocs[i].position  = vis_position;
782       allocs[i].size      = sizes[i].minimum_size;
783
784       if (group_expands (context, allocs[i].group_idx))
785         {
786           allocs[i].size += extra_size;
787           if (extra_extra)
788             {
789               allocs[i].size++;
790               extra_extra--;
791             }
792         }
793
794       position += allocs[i].size;
795       position += spacing;
796
797       if (_gtk_cell_area_box_group_visible (area, allocs[i].group_idx))
798         {
799           vis_position += allocs[i].size;
800           vis_position += spacing;
801         }
802     }
803
804   if (n_allocs)
805     *n_allocs = n_groups;
806
807   g_free (sizes);
808
809   return allocs;
810 }
811
812 GtkRequestedSize *
813 _gtk_cell_area_box_context_get_widths (GtkCellAreaBoxContext *box_context,
814                                       gint                  *n_widths)
815 {
816   GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));
817
818   return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_HORIZONTAL, -1, n_widths);
819 }
820
821 GtkRequestedSize *
822 _gtk_cell_area_box_context_get_heights (GtkCellAreaBoxContext *box_context,
823                                        gint                  *n_heights)
824 {
825   GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));
826
827   return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_VERTICAL, -1, n_heights);
828 }
829
830 GtkCellAreaBoxAllocation *
831 _gtk_cell_area_box_context_get_orientation_allocs (GtkCellAreaBoxContext *context,
832                                                   gint                  *n_allocs)
833 {
834   GtkCellAreaContext       *ctx  = GTK_CELL_AREA_CONTEXT (context);
835   GtkCellAreaBox           *area;
836   GtkOrientation            orientation;
837   gint                      spacing, width, height, alloc_count = 0;
838   GtkCellAreaBoxAllocation *allocs = NULL;
839
840   area        = (GtkCellAreaBox *)gtk_cell_area_context_get_area (ctx);
841   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (area));
842   spacing     = gtk_cell_area_box_get_spacing (area);
843
844   gtk_cell_area_context_get_allocation (ctx, &width, &height);
845
846   if (orientation == GTK_ORIENTATION_HORIZONTAL && width > 0)
847     allocs = allocate_for_orientation (context, area, orientation, 
848                                        spacing, width, height,
849                                        &alloc_count);
850   else if (orientation == GTK_ORIENTATION_VERTICAL && height > 0)
851     allocs = allocate_for_orientation (context, area, orientation, 
852                                        spacing, height, width,
853                                        &alloc_count);
854
855   *n_allocs = alloc_count;
856
857   return allocs;
858 }