gtkcellareaboxcontext.c 29.1 KB
Newer Older
1
/* gtkcellareaboxcontext.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright (C) 2010 Openismus GmbH
 *
 * Authors:
 *      Tristan Van Berkom <tristanvb@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"
#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 33
static void      _gtk_cell_area_box_context_reset                 (GtkCellAreaContext    *context);
static void      _gtk_cell_area_box_context_get_preferred_height_for_width (GtkCellAreaContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
34 35 36
                                                                           gint                width,
                                                                           gint               *minimum_height,
                                                                           gint               *natural_height);
37
static void      _gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
38 39 40
                                                                           gint                height,
                                                                           gint               *minimum_width,
                                                                           gint               *natural_width);
41 42


43 44

/* Internal functions */
45
static void      _gtk_cell_area_box_context_sum                  (GtkCellAreaBoxContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
46 47 48 49
                                                                 GtkOrientation          orientation,
                                                                 gint                    for_size,
                                                                 gint                   *minimum_size,
                                                                 gint                   *natural_size);
50 51 52
static void      free_cache_array                                (GArray                *array);
static GArray   *group_array_new                                 (GtkCellAreaBoxContext *context);
static GArray   *get_array                                       (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
53 54
                                                                  GtkOrientation         orientation,
                                                                  gint                   for_size);
55
static gboolean  group_expands                                   (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
56
                                                                  gint                   group_idx);
57
static gint      count_expand_groups                             (GtkCellAreaBoxContext *context);
58

59 60 61 62 63 64 65

/* CachedSize management */
typedef struct {
  gint     min_size;
  gint     nat_size;
} CachedSize;

66
struct _GtkCellAreaBoxContextPrivate
67 68
{
  /* Table of per renderer CachedSizes */
69 70
  GArray *base_widths;
  GArray *base_heights;
71

72 73 74 75
  /* Table of per height/width hash tables of per renderer CachedSizes */
  GHashTable *widths;
  GHashTable *heights;

76 77 78
  /* Whether each group expands */
  gboolean  *expand;

79 80
  /* Whether each group is aligned */
  gboolean  *align;
81 82
};

83
G_DEFINE_TYPE_WITH_PRIVATE (GtkCellAreaBoxContext, _gtk_cell_area_box_context, GTK_TYPE_CELL_AREA_CONTEXT)
84

85 86 87 88 89 90
static void
free_cache_array (GArray *array)
{
  g_array_free (array, TRUE);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104
static GArray *
group_array_new (GtkCellAreaBoxContext *context)
{
  GtkCellAreaBoxContextPrivate *priv = context->priv;
  GArray *group_array;

  group_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
  g_array_set_size (group_array, priv->base_widths->len);

  return group_array;
}

static GArray *
get_array (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
105 106
           GtkOrientation         orientation,
           gint                   for_size)
107 108 109 110 111 112 113
{
  GtkCellAreaBoxContextPrivate *priv = context->priv;
  GArray                       *array;

  if (for_size < 0)
    {
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
Matthias Clasen's avatar
Matthias Clasen committed
114
        array = priv->base_widths;
115
      else
Matthias Clasen's avatar
Matthias Clasen committed
116
        array = priv->base_heights;
117 118 119 120
    }
  else
    {
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
Matthias Clasen's avatar
Matthias Clasen committed
121 122
        {
          array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_size));
123

Matthias Clasen's avatar
Matthias Clasen committed
124 125 126
          if (!array)
            array = priv->base_widths;
        }
127
      else
Matthias Clasen's avatar
Matthias Clasen committed
128 129
        {
          array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_size));
130

Matthias Clasen's avatar
Matthias Clasen committed
131 132 133
          if (!array)
            array = priv->base_heights;
        }
134 135 136 137 138 139 140
    }

  return array;
}

static gboolean 
group_expands (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
141
               gint                   group_idx)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
  GtkCellAreaBoxContextPrivate *priv = context->priv;

  g_assert (group_idx >= 0 && group_idx < priv->base_widths->len);

  return priv->expand[group_idx];
}

static gint
count_expand_groups (GtkCellAreaBoxContext *context)
{
  GtkCellAreaBoxContextPrivate *priv = context->priv;
  gint i, expand = 0;

  for (i = 0; i < priv->base_widths->len; i++)
    {
      if (priv->expand[i])
Matthias Clasen's avatar
Matthias Clasen committed
159
        expand++;
160 161 162 163 164
    }
  
  return expand;
}

165
static void
166
_gtk_cell_area_box_context_init (GtkCellAreaBoxContext *box_context)
167
{
168
  GtkCellAreaBoxContextPrivate *priv;
169

170
  box_context->priv = _gtk_cell_area_box_context_get_instance_private (box_context);
171
  priv = box_context->priv;
172

173 174
  priv->base_widths  = g_array_new (FALSE, TRUE, sizeof (CachedSize));
  priv->base_heights = g_array_new (FALSE, TRUE, sizeof (CachedSize));
175

176
  priv->widths       = g_hash_table_new_full (g_direct_hash, g_direct_equal,
Matthias Clasen's avatar
Matthias Clasen committed
177
                                              NULL, (GDestroyNotify)free_cache_array);
178
  priv->heights      = g_hash_table_new_full (g_direct_hash, g_direct_equal,
Matthias Clasen's avatar
Matthias Clasen committed
179
                                              NULL, (GDestroyNotify)free_cache_array);
180 181 182
}

static void 
183
_gtk_cell_area_box_context_class_init (GtkCellAreaBoxContextClass *class)
184
{
185 186
  GObjectClass            *object_class = G_OBJECT_CLASS (class);
  GtkCellAreaContextClass *context_class   = GTK_CELL_AREA_CONTEXT_CLASS (class);
187 188

  /* GObjectClass */
189
  object_class->finalize = _gtk_cell_area_box_context_finalize;
190

191 192 193
  context_class->reset                          = _gtk_cell_area_box_context_reset;
  context_class->get_preferred_height_for_width = _gtk_cell_area_box_context_get_preferred_height_for_width;
  context_class->get_preferred_width_for_height = _gtk_cell_area_box_context_get_preferred_width_for_height;
194 195 196 197 198 199
}

/*************************************************************
 *                      GObjectClass                         *
 *************************************************************/
static void
200
_gtk_cell_area_box_context_finalize (GObject *object)
201
{
202 203
  GtkCellAreaBoxContext        *box_context = GTK_CELL_AREA_BOX_CONTEXT (object);
  GtkCellAreaBoxContextPrivate *priv        = box_context->priv;
204

205 206
  g_array_free (priv->base_widths, TRUE);
  g_array_free (priv->base_heights, TRUE);
207 208
  g_hash_table_destroy (priv->widths);
  g_hash_table_destroy (priv->heights);
209

210
  g_free (priv->expand);
211
  g_free (priv->align);
212

213
  G_OBJECT_CLASS (_gtk_cell_area_box_context_parent_class)->finalize (object);
214 215 216
}

/*************************************************************
Matthias Clasen's avatar
Matthias Clasen committed
217
 *                    GtkCellAreaContextClass                *
218
 *************************************************************/
219
static void
220
_gtk_cell_area_box_context_reset (GtkCellAreaContext *context)
221
{
222 223
  GtkCellAreaBoxContext        *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
  GtkCellAreaBoxContextPrivate *priv        = box_context->priv;
224
  CachedSize                   *size;
225
  gint                          i;
226 227 228

  for (i = 0; i < priv->base_widths->len; i++)
    {
229
      size = &g_array_index (priv->base_widths, CachedSize, i);
230 231 232 233

      size->min_size = 0;
      size->nat_size = 0;

234
      size = &g_array_index (priv->base_heights, CachedSize, i);
235 236 237 238

      size->min_size = 0;
      size->nat_size = 0;
    }
239

240 241
  /* Reset context sizes as well */
  g_hash_table_remove_all (priv->widths);
242
  g_hash_table_remove_all (priv->heights);
243

244
  GTK_CELL_AREA_CONTEXT_CLASS
245
    (_gtk_cell_area_box_context_parent_class)->reset (context);
246 247
}

248
static void
249
_gtk_cell_area_box_context_sum (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
250 251 252 253
                               GtkOrientation         orientation,
                               gint                   for_size,
                               gint                  *minimum_size,
                               gint                  *natural_size)
254
{
255 256
  GtkCellAreaBoxContextPrivate *priv = context->priv;
  GtkCellAreaBox *area;
257 258
  GtkOrientation  box_orientation;
  GArray         *array;
259
  gint            spacing, i, last_aligned_group_idx;
260
  gint            min_size = 0, nat_size = 0;
261

262 263
  area            = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (context));
  spacing         = gtk_cell_area_box_get_spacing (area);
264
  box_orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (area));
265
  array           = get_array (context, orientation, for_size);
266

267 268 269 270 271
  /* Get the last visible aligned group 
   * (we need to get space at least up till this group) */
  for (i = array->len - 1; i >= 0; i--)
    {
      if (priv->align[i] && 
272 273
          _gtk_cell_area_box_group_visible (area, i))
        break;
274 275 276
    }
  last_aligned_group_idx = i >= 0 ? i : 0;

277 278
  for (i = 0; i < array->len; i++)
    {
279
      CachedSize *size = &g_array_index (array, CachedSize, i);
280

281
      if (box_orientation == orientation)
Matthias Clasen's avatar
Matthias Clasen committed
282
        {
283 284 285
          if (i > last_aligned_group_idx &&
              !_gtk_cell_area_box_group_visible (area, i))
            continue;
286

Matthias Clasen's avatar
Matthias Clasen committed
287 288 289 290 291 292 293 294 295 296 297 298
          /* Dont add spacing for 0 size groups, they can be 0 size because
           * they contain only invisible cells for this round of requests
           */
          if (min_size > 0 && size->nat_size > 0)
            {
              min_size += spacing;
              nat_size += spacing;
            }

          min_size += size->min_size;
          nat_size += size->nat_size;
        }
299
      else
Matthias Clasen's avatar
Matthias Clasen committed
300 301 302 303
        {
          min_size = MAX (min_size, size->min_size);
          nat_size = MAX (nat_size, size->nat_size);
        }
304 305
    }

306 307 308
  if (for_size < 0)
    {
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
Matthias Clasen's avatar
Matthias Clasen committed
309
        gtk_cell_area_context_push_preferred_width (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
310
      else
Matthias Clasen's avatar
Matthias Clasen committed
311
        gtk_cell_area_context_push_preferred_height (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
312 313 314 315 316 317 318 319 320
    }

  if (minimum_size)
    *minimum_size = min_size;
  if (natural_size)
    *natural_size = nat_size;
}

static void
321
_gtk_cell_area_box_context_get_preferred_height_for_width (GtkCellAreaContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
322 323 324
                                                          gint                width,
                                                          gint               *minimum_height,
                                                          gint               *natural_height)
325
{
326
  _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_VERTICAL, 
Matthias Clasen's avatar
Matthias Clasen committed
327
                                 width, minimum_height, natural_height);
328 329 330
}

static void
331
_gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
332 333 334
                                                          gint                height,
                                                          gint               *minimum_width,
                                                          gint               *natural_width)
335
{
336
  _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_HORIZONTAL, 
Matthias Clasen's avatar
Matthias Clasen committed
337
                                 height, minimum_width, natural_width);
338
}
339

340 341 342
/*************************************************************
 *                            API                            *
 *************************************************************/
343 344
static void
copy_size_array (GArray *src_array,
Matthias Clasen's avatar
Matthias Clasen committed
345
                 GArray *dest_array)
346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
  gint i;

  for (i = 0; i < src_array->len; i++)
    {
      CachedSize *src  = &g_array_index (src_array, CachedSize, i);
      CachedSize *dest = &g_array_index (dest_array, CachedSize, i);

      memcpy (dest, src, sizeof (CachedSize));
    }
}

static void
for_size_copy (gpointer    key,
Matthias Clasen's avatar
Matthias Clasen committed
360 361
               GArray     *size_array,
               GHashTable *dest_hash)
362 363 364 365 366 367 368 369 370 371 372 373
{
  GArray *new_array;

  new_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
  g_array_set_size (new_array, size_array->len);

  copy_size_array (size_array, new_array);

  g_hash_table_insert (dest_hash, key, new_array);
}

GtkCellAreaBoxContext *
374
_gtk_cell_area_box_context_copy (GtkCellAreaBox        *box,
Matthias Clasen's avatar
Matthias Clasen committed
375
                                GtkCellAreaBoxContext *context)
376
{
377 378
  GtkCellAreaBoxContext *copy;

Matthias Clasen's avatar
Matthias Clasen committed
379 380
  copy = g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
                       "area", box, NULL);
381

382 383 384 385
  _gtk_cell_area_box_init_groups (copy,
                                  context->priv->base_widths->len,
                                  context->priv->expand,
                                  context->priv->align);
386 387

  /* Copy the base arrays */
Matthias Clasen's avatar
Matthias Clasen committed
388 389 390 391
  copy_size_array (context->priv->base_widths,
                   copy->priv->base_widths);
  copy_size_array (context->priv->base_heights,
                   copy->priv->base_heights);
392 393 394

  /* Copy each for size */
  g_hash_table_foreach (context->priv->heights,
Matthias Clasen's avatar
Matthias Clasen committed
395
                        (GHFunc)for_size_copy, copy->priv->heights);
396
  g_hash_table_foreach (context->priv->widths,
Matthias Clasen's avatar
Matthias Clasen committed
397
                        (GHFunc)for_size_copy, copy->priv->widths);
398

399

400
  return copy;
401 402
}

403
void
404 405 406 407
_gtk_cell_area_box_init_groups (GtkCellAreaBoxContext *box_context,
                                guint                  n_groups,
                                gboolean              *expand_groups,
                                gboolean              *align_groups)
408
{
409
  GtkCellAreaBoxContextPrivate *priv;
410

411
  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
412
  g_return_if_fail (n_groups == 0 || expand_groups != NULL);
413

Matthias Clasen's avatar
Matthias Clasen committed
414
  /* When the group dimensions change, all info must be reset
415
   * Note this already clears the min/nat values on the CachedSizes
416
   */
417
  gtk_cell_area_context_reset (GTK_CELL_AREA_CONTEXT (box_context));
418

419
  priv = box_context->priv;
420 421 422
  g_array_set_size (priv->base_widths,  n_groups);
  g_array_set_size (priv->base_heights, n_groups);

423 424
  g_free (priv->expand);
  priv->expand = g_memdup (expand_groups, n_groups * sizeof (gboolean));
425 426 427

  g_free (priv->align);
  priv->align = g_memdup (align_groups, n_groups * sizeof (gboolean));
428
}
429 430

void
431
_gtk_cell_area_box_context_push_group_width (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
432 433 434
                                            gint                   group_idx,
                                            gint                   minimum_width,
                                            gint                   natural_width)
435
{
436
  GtkCellAreaBoxContextPrivate *priv;
437
  CachedSize                   *size;
438
  gboolean                      grew = FALSE;
439

440
  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
441

442
  priv = box_context->priv;
443
  g_return_if_fail (group_idx < priv->base_widths->len);
444

445
  size = &g_array_index (priv->base_widths, CachedSize, group_idx);
446 447 448 449 450 451 452 453 454 455 456 457
  if (minimum_width > size->min_size)
    {
      size->min_size = minimum_width;
      grew = TRUE;
    }
  if (natural_width > size->nat_size)
    {
      size->nat_size = natural_width;
      grew = TRUE;
    }

  if (grew)
458
    _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_HORIZONTAL, -1, NULL, NULL);
459 460
}

461
void
462
_gtk_cell_area_box_context_push_group_height_for_width  (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
463 464 465 466
                                                        gint                   group_idx,
                                                        gint                   for_width,
                                                        gint                   minimum_height,
                                                        gint                   natural_height)
467 468 469 470 471 472 473 474 475 476 477 478 479
{
  GtkCellAreaBoxContextPrivate *priv;
  GArray                       *group_array;
  CachedSize                   *size;

  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));

  priv = box_context->priv;
  g_return_if_fail (group_idx < priv->base_widths->len);

  group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));
  if (!group_array)
    {
480
      group_array = group_array_new (box_context);
481 482 483 484 485 486 487 488
      g_hash_table_insert (priv->heights, GINT_TO_POINTER (for_width), group_array);
    }

  size = &g_array_index (group_array, CachedSize, group_idx);
  size->min_size = MAX (size->min_size, minimum_height);
  size->nat_size = MAX (size->nat_size, natural_height);
}

489
void
490
_gtk_cell_area_box_context_push_group_height (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
491 492 493
                                             gint                   group_idx,
                                             gint                   minimum_height,
                                             gint                   natural_height)
494
{
495
  GtkCellAreaBoxContextPrivate *priv;
496
  CachedSize                   *size;
497
  gboolean                      grew = FALSE;
498

499
  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
500

501
  priv = box_context->priv;
502
  g_return_if_fail (group_idx < priv->base_heights->len);
503

504
  size = &g_array_index (priv->base_heights, CachedSize, group_idx);
505 506 507 508 509 510 511 512 513 514 515 516
  if (minimum_height > size->min_size)
    {
      size->min_size = minimum_height;
      grew = TRUE;
    }
  if (natural_height > size->nat_size)
    {
      size->nat_size = natural_height;
      grew = TRUE;
    }

  if (grew)
517
    _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_VERTICAL, -1, NULL, NULL);
518 519
}

520
void
521
_gtk_cell_area_box_context_push_group_width_for_height (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
522 523 524 525
                                                       gint                   group_idx,
                                                       gint                   for_height,
                                                       gint                   minimum_width,
                                                       gint                   natural_width)
526 527
{
  GtkCellAreaBoxContextPrivate *priv;
528 529
  GArray                       *group_array;
  CachedSize                   *size;
530 531 532 533 534 535 536 537 538

  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));

  priv        = box_context->priv;
  g_return_if_fail (group_idx < priv->base_widths->len);

  group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));
  if (!group_array)
    {
539
      group_array = group_array_new (box_context);
540 541 542 543 544 545 546 547
      g_hash_table_insert (priv->widths, GINT_TO_POINTER (for_height), group_array);
    }

  size = &g_array_index (group_array, CachedSize, group_idx);
  size->min_size = MAX (size->min_size, minimum_width);
  size->nat_size = MAX (size->nat_size, natural_width);
}

548
void
549
_gtk_cell_area_box_context_get_group_width (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
550 551 552
                                           gint                   group_idx,
                                           gint                  *minimum_width,
                                           gint                  *natural_width)
553
{
554
  GtkCellAreaBoxContextPrivate *priv;
555
  CachedSize                   *size;
556

557
  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
558

559
  priv = box_context->priv;
560
  g_return_if_fail (group_idx < priv->base_widths->len);
561

562
  size = &g_array_index (priv->base_widths, CachedSize, group_idx);
563

564 565
  if (minimum_width)
    *minimum_width = size->min_size;
Matthias Clasen's avatar
Matthias Clasen committed
566

567 568
  if (natural_width)
    *natural_width = size->nat_size;
569 570
}

571
void
572
_gtk_cell_area_box_context_get_group_height_for_width (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
573 574 575 576
                                                      gint                   group_idx,
                                                      gint                   for_width,
                                                      gint                  *minimum_height,
                                                      gint                  *natural_height)
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
{
  GtkCellAreaBoxContextPrivate *priv;
  GArray                    *group_array;

  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));

  priv        = box_context->priv;
  g_return_if_fail (group_idx < priv->base_widths->len);

  group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));

  if (group_array)
    {
      CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);

      if (minimum_height)
Matthias Clasen's avatar
Matthias Clasen committed
593
        *minimum_height = size->min_size;
594 595

      if (natural_height)
Matthias Clasen's avatar
Matthias Clasen committed
596
        *natural_height = size->nat_size;
597 598 599 600
    }
  else
    {
      if (minimum_height)
Matthias Clasen's avatar
Matthias Clasen committed
601
        *minimum_height = -1;
602 603

      if (natural_height)
Matthias Clasen's avatar
Matthias Clasen committed
604
        *natural_height = -1;
605 606 607
    }
}

608
void
609
_gtk_cell_area_box_context_get_group_height (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
610 611 612
                                            gint                   group_idx,
                                            gint                  *minimum_height,
                                            gint                  *natural_height)
613
{
614
  GtkCellAreaBoxContextPrivate *priv;
615
  CachedSize                   *size;
616

617
  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
618

619
  priv = box_context->priv;
620
  g_return_if_fail (group_idx < priv->base_heights->len);
621

622
  size = &g_array_index (priv->base_heights, CachedSize, group_idx);
623

624 625
  if (minimum_height)
    *minimum_height = size->min_size;
Matthias Clasen's avatar
Matthias Clasen committed
626

627 628
  if (natural_height)
    *natural_height = size->nat_size;
629 630
}

631
void
632
_gtk_cell_area_box_context_get_group_width_for_height (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
633 634 635 636
                                                      gint                   group_idx,
                                                      gint                   for_height,
                                                      gint                  *minimum_width,
                                                      gint                  *natural_width)
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
{
  GtkCellAreaBoxContextPrivate *priv;
  GArray                       *group_array;

  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));

  priv = box_context->priv;
  g_return_if_fail (group_idx < priv->base_widths->len);

  group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));

  if (group_array)
    {
      CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);

      if (minimum_width)
Matthias Clasen's avatar
Matthias Clasen committed
653
        *minimum_width = size->min_size;
654 655

      if (natural_width)
Matthias Clasen's avatar
Matthias Clasen committed
656
        *natural_width = size->nat_size;
657 658 659 660
    }
  else
    {
      if (minimum_width)
Matthias Clasen's avatar
Matthias Clasen committed
661
        *minimum_width = -1;
662 663

      if (natural_width)
Matthias Clasen's avatar
Matthias Clasen committed
664
        *natural_width = -1;
665 666 667
    }
}

668
static GtkRequestedSize *
669
_gtk_cell_area_box_context_get_requests (GtkCellAreaBoxContext *box_context,
670
                                        GtkCellAreaBox        *area,
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
                                        GtkOrientation         orientation,
                                        gint                   for_size,
                                        gint                  *n_requests)
{
  GtkCellAreaBoxContextPrivate *priv = box_context->priv;
  GtkRequestedSize             *requests;
  GArray                       *array;
  CachedSize                   *size;
  gint                          visible_groups = 0;
  gint                          last_aligned_group_idx = 0;
  gint                          i, j;

  /* Get the last visible aligned group 
   * (we need to get space at least up till this group) */
  for (i = priv->base_widths->len - 1; i >= 0; i--)
    {
      if (priv->align[i] && 
688 689
          _gtk_cell_area_box_group_visible (area, i))
        break;
690 691 692 693 694 695 696 697 698 699 700
    }
  last_aligned_group_idx = i >= 0 ? i : 0;

  priv  = box_context->priv;
  array = get_array (box_context, orientation, for_size);

  for (i = 0; i < array->len; i++)
    {
      size = &g_array_index (array, CachedSize, i);

      if (size->nat_size > 0 &&
701 702 703
          (i <= last_aligned_group_idx ||
           _gtk_cell_area_box_group_visible (area, i)))
        visible_groups++;
704 705 706 707 708 709 710 711 712
    }

  requests = g_new (GtkRequestedSize, visible_groups);

  for (j = 0, i = 0; i < array->len; i++)
    {
      size = &g_array_index (array, CachedSize, i);

      if (size->nat_size > 0 &&
713 714
          (i <= last_aligned_group_idx ||
           _gtk_cell_area_box_group_visible (area, i)))
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        {
          requests[j].data         = GINT_TO_POINTER (i);
          requests[j].minimum_size = size->min_size;
          requests[j].natural_size = size->nat_size;
          j++;
        }
    }

  if (n_requests)
    *n_requests = visible_groups;

  return requests;
}

static GtkCellAreaBoxAllocation *
allocate_for_orientation (GtkCellAreaBoxContext *context,
731
                          GtkCellAreaBox        *area,
732 733 734 735 736 737 738 739 740 741 742 743 744 745
                          GtkOrientation         orientation,
                          gint                   spacing,
                          gint                   size,
                          gint                   for_size,
                          gint                  *n_allocs)
{
  GtkCellAreaBoxContextPrivate *priv = context->priv;
  GtkCellAreaBoxAllocation     *allocs;
  GtkRequestedSize             *sizes;
  gint                          n_expand_groups = 0;
  gint                          i, n_groups, position, vis_position;
  gint                          extra_size, extra_extra;
  gint                          avail_size = size;

746
  sizes           = _gtk_cell_area_box_context_get_requests (context, area, orientation, for_size, &n_groups);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
  n_expand_groups = count_expand_groups (context);

  /* First start by naturally allocating space among groups */
  avail_size -= (n_groups - 1) * spacing;
  for (i = 0; i < n_groups; i++)
    avail_size -= sizes[i].minimum_size;

  if (avail_size > 0)
    avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, sizes);
  else
    avail_size = 0;

  /* Calculate/distribute expand for groups */
  if (n_expand_groups > 0)
    {
      extra_size  = avail_size / n_expand_groups;
      extra_extra = avail_size % n_expand_groups;
    }
  else
    extra_size = extra_extra = 0;

  allocs = g_new (GtkCellAreaBoxAllocation, n_groups);

  for (vis_position = 0, position = 0, i = 0; i < n_groups; i++)
    {
      allocs[i].group_idx = GPOINTER_TO_INT (sizes[i].data);

      if (priv->align[allocs[i].group_idx])
775
        vis_position = position;
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

      allocs[i].position  = vis_position;
      allocs[i].size      = sizes[i].minimum_size;

      if (group_expands (context, allocs[i].group_idx))
        {
          allocs[i].size += extra_size;
          if (extra_extra)
            {
              allocs[i].size++;
              extra_extra--;
            }
        }

      position += allocs[i].size;
      position += spacing;

      if (_gtk_cell_area_box_group_visible (area, allocs[i].group_idx))
794 795 796 797
        {
          vis_position += allocs[i].size;
          vis_position += spacing;
        }
798 799 800 801 802 803 804 805 806 807
    }

  if (n_allocs)
    *n_allocs = n_groups;

  g_free (sizes);

  return allocs;
}

808
GtkRequestedSize *
809
_gtk_cell_area_box_context_get_widths (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
810
                                      gint                  *n_widths)
811
{
812 813
  GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));

814
  return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_HORIZONTAL, -1, n_widths);
815 816 817
}

GtkRequestedSize *
818
_gtk_cell_area_box_context_get_heights (GtkCellAreaBoxContext *box_context,
Matthias Clasen's avatar
Matthias Clasen committed
819
                                       gint                  *n_heights)
820
{
821 822
  GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));

823
  return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_VERTICAL, -1, n_heights);
824
}
825

826
GtkCellAreaBoxAllocation *
827
_gtk_cell_area_box_context_get_orientation_allocs (GtkCellAreaBoxContext *context,
Matthias Clasen's avatar
Matthias Clasen committed
828
                                                  gint                  *n_allocs)
829
{
830 831 832 833 834
  GtkCellAreaContext       *ctx  = GTK_CELL_AREA_CONTEXT (context);
  GtkCellAreaBox           *area;
  GtkOrientation            orientation;
  gint                      spacing, width, height, alloc_count = 0;
  GtkCellAreaBoxAllocation *allocs = NULL;
835

836 837 838
  area        = (GtkCellAreaBox *)gtk_cell_area_context_get_area (ctx);
  orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (area));
  spacing     = gtk_cell_area_box_get_spacing (area);
Matthias Clasen's avatar
Matthias Clasen committed
839

840
  gtk_cell_area_context_get_allocation (ctx, &width, &height);
841

842 843
  if (orientation == GTK_ORIENTATION_HORIZONTAL && width > 0)
    allocs = allocate_for_orientation (context, area, orientation, 
844 845
                                       spacing, width, height,
                                       &alloc_count);
846 847
  else if (orientation == GTK_ORIENTATION_VERTICAL && height > 0)
    allocs = allocate_for_orientation (context, area, orientation, 
848 849
                                       spacing, height, width,
                                       &alloc_count);
850 851

  *n_allocs = alloc_count;
852

853
  return allocs;
854
}