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