1 /* gtkcellareaboxcontext.c
3 * Copyright (C) 2010 Openismus GmbH
6 * Tristan Van Berkom <tristanvb@openismus.com>
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.
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.
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.
26 #include "gtkcellareabox.h"
27 #include "gtkcellareaboxcontextprivate.h"
28 #include "gtkorientable.h"
31 static void _gtk_cell_area_box_context_finalize (GObject *object);
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,
38 gint *natural_height);
39 static void _gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
46 /* Internal functions */
47 static void _gtk_cell_area_box_context_sum (GtkCellAreaBoxContext *context,
48 GtkOrientation orientation,
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,
57 static gboolean group_expands (GtkCellAreaBoxContext *context,
59 static gint count_expand_groups (GtkCellAreaBoxContext *context);
62 /* CachedSize management */
68 struct _GtkCellAreaBoxContextPrivate
70 /* Table of per renderer CachedSizes */
74 /* Table of per height/width hash tables of per renderer CachedSizes */
78 /* Whether each group expands */
81 /* Whether each group is aligned */
85 G_DEFINE_TYPE (GtkCellAreaBoxContext, _gtk_cell_area_box_context, GTK_TYPE_CELL_AREA_CONTEXT);
88 free_cache_array (GArray *array)
90 g_array_free (array, TRUE);
94 group_array_new (GtkCellAreaBoxContext *context)
96 GtkCellAreaBoxContextPrivate *priv = context->priv;
99 group_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
100 g_array_set_size (group_array, priv->base_widths->len);
106 get_array (GtkCellAreaBoxContext *context,
107 GtkOrientation orientation,
110 GtkCellAreaBoxContextPrivate *priv = context->priv;
115 if (orientation == GTK_ORIENTATION_HORIZONTAL)
116 array = priv->base_widths;
118 array = priv->base_heights;
122 if (orientation == GTK_ORIENTATION_HORIZONTAL)
124 array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_size));
127 array = priv->base_widths;
131 array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_size));
134 array = priv->base_heights;
142 group_expands (GtkCellAreaBoxContext *context,
145 GtkCellAreaBoxContextPrivate *priv = context->priv;
147 g_assert (group_idx >= 0 && group_idx < priv->base_widths->len);
149 return priv->expand[group_idx];
153 count_expand_groups (GtkCellAreaBoxContext *context)
155 GtkCellAreaBoxContextPrivate *priv = context->priv;
158 for (i = 0; i < priv->base_widths->len; i++)
168 _gtk_cell_area_box_context_init (GtkCellAreaBoxContext *box_context)
170 GtkCellAreaBoxContextPrivate *priv;
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;
177 priv->base_widths = g_array_new (FALSE, TRUE, sizeof (CachedSize));
178 priv->base_heights = g_array_new (FALSE, TRUE, sizeof (CachedSize));
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);
187 _gtk_cell_area_box_context_class_init (GtkCellAreaBoxContextClass *class)
189 GObjectClass *object_class = G_OBJECT_CLASS (class);
190 GtkCellAreaContextClass *context_class = GTK_CELL_AREA_CONTEXT_CLASS (class);
193 object_class->finalize = _gtk_cell_area_box_context_finalize;
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;
199 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxContextPrivate));
202 /*************************************************************
204 *************************************************************/
206 _gtk_cell_area_box_context_finalize (GObject *object)
208 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (object);
209 GtkCellAreaBoxContextPrivate *priv = box_context->priv;
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);
216 g_free (priv->expand);
217 g_free (priv->align);
219 G_OBJECT_CLASS (_gtk_cell_area_box_context_parent_class)->finalize (object);
222 /*************************************************************
223 * GtkCellAreaContextClass *
224 *************************************************************/
226 _gtk_cell_area_box_context_reset (GtkCellAreaContext *context)
228 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
229 GtkCellAreaBoxContextPrivate *priv = box_context->priv;
233 for (i = 0; i < priv->base_widths->len; i++)
235 size = &g_array_index (priv->base_widths, CachedSize, i);
240 size = &g_array_index (priv->base_heights, CachedSize, i);
246 /* Reset context sizes as well */
247 g_hash_table_remove_all (priv->widths);
248 g_hash_table_remove_all (priv->heights);
250 GTK_CELL_AREA_CONTEXT_CLASS
251 (_gtk_cell_area_box_context_parent_class)->reset (context);
255 _gtk_cell_area_box_context_sum (GtkCellAreaBoxContext *context,
256 GtkOrientation orientation,
261 GtkCellAreaBoxContextPrivate *priv = context->priv;
262 GtkCellAreaBox *area;
263 GtkOrientation box_orientation;
265 gint spacing, i, last_aligned_group_idx;
266 gint min_size = 0, nat_size = 0;
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);
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--)
277 if (priv->align[i] &&
278 _gtk_cell_area_box_group_visible (area, i))
281 last_aligned_group_idx = i >= 0 ? i : 0;
283 for (i = 0; i < array->len; i++)
285 CachedSize *size = &g_array_index (array, CachedSize, i);
287 if (box_orientation == orientation)
289 if (i > last_aligned_group_idx &&
290 !_gtk_cell_area_box_group_visible (area, i))
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
296 if (min_size > 0 && size->nat_size > 0)
302 min_size += size->min_size;
303 nat_size += size->nat_size;
307 min_size = MAX (min_size, size->min_size);
308 nat_size = MAX (nat_size, size->nat_size);
314 if (orientation == GTK_ORIENTATION_HORIZONTAL)
315 gtk_cell_area_context_push_preferred_width (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
317 gtk_cell_area_context_push_preferred_height (GTK_CELL_AREA_CONTEXT (context), min_size, nat_size);
321 *minimum_size = min_size;
323 *natural_size = nat_size;
327 _gtk_cell_area_box_context_get_preferred_height_for_width (GtkCellAreaContext *context,
329 gint *minimum_height,
330 gint *natural_height)
332 _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_VERTICAL,
333 width, minimum_height, natural_height);
337 _gtk_cell_area_box_context_get_preferred_width_for_height (GtkCellAreaContext *context,
342 _gtk_cell_area_box_context_sum (GTK_CELL_AREA_BOX_CONTEXT (context), GTK_ORIENTATION_HORIZONTAL,
343 height, minimum_width, natural_width);
346 /*************************************************************
348 *************************************************************/
350 copy_size_array (GArray *src_array,
355 for (i = 0; i < src_array->len; i++)
357 CachedSize *src = &g_array_index (src_array, CachedSize, i);
358 CachedSize *dest = &g_array_index (dest_array, CachedSize, i);
360 memcpy (dest, src, sizeof (CachedSize));
365 for_size_copy (gpointer key,
367 GHashTable *dest_hash)
371 new_array = g_array_new (FALSE, TRUE, sizeof (CachedSize));
372 g_array_set_size (new_array, size_array->len);
374 copy_size_array (size_array, new_array);
376 g_hash_table_insert (dest_hash, key, new_array);
379 GtkCellAreaBoxContext *
380 _gtk_cell_area_box_context_copy (GtkCellAreaBox *box,
381 GtkCellAreaBoxContext *context)
383 GtkCellAreaBoxContext *copy;
385 copy = g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
388 _gtk_cell_area_box_init_groups (copy,
389 context->priv->base_widths->len,
390 context->priv->expand,
391 context->priv->align);
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);
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);
410 _gtk_cell_area_box_init_groups (GtkCellAreaBoxContext *box_context,
412 gboolean *expand_groups,
413 gboolean *align_groups)
415 GtkCellAreaBoxContextPrivate *priv;
417 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
418 g_return_if_fail (n_groups == 0 || expand_groups != NULL);
420 /* When the group dimensions change, all info must be reset
421 * Note this already clears the min/nat values on the CachedSizes
423 gtk_cell_area_context_reset (GTK_CELL_AREA_CONTEXT (box_context));
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);
429 g_free (priv->expand);
430 priv->expand = g_memdup (expand_groups, n_groups * sizeof (gboolean));
432 g_free (priv->align);
433 priv->align = g_memdup (align_groups, n_groups * sizeof (gboolean));
437 _gtk_cell_area_box_context_push_group_width (GtkCellAreaBoxContext *box_context,
442 GtkCellAreaBoxContextPrivate *priv;
444 gboolean grew = FALSE;
446 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
448 priv = box_context->priv;
449 g_return_if_fail (group_idx < priv->base_widths->len);
451 size = &g_array_index (priv->base_widths, CachedSize, group_idx);
452 if (minimum_width > size->min_size)
454 size->min_size = minimum_width;
457 if (natural_width > size->nat_size)
459 size->nat_size = natural_width;
464 _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_HORIZONTAL, -1, NULL, NULL);
468 _gtk_cell_area_box_context_push_group_height_for_width (GtkCellAreaBoxContext *box_context,
474 GtkCellAreaBoxContextPrivate *priv;
478 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
480 priv = box_context->priv;
481 g_return_if_fail (group_idx < priv->base_widths->len);
483 group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));
486 group_array = group_array_new (box_context);
487 g_hash_table_insert (priv->heights, GINT_TO_POINTER (for_width), group_array);
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);
496 _gtk_cell_area_box_context_push_group_height (GtkCellAreaBoxContext *box_context,
501 GtkCellAreaBoxContextPrivate *priv;
503 gboolean grew = FALSE;
505 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
507 priv = box_context->priv;
508 g_return_if_fail (group_idx < priv->base_heights->len);
510 size = &g_array_index (priv->base_heights, CachedSize, group_idx);
511 if (minimum_height > size->min_size)
513 size->min_size = minimum_height;
516 if (natural_height > size->nat_size)
518 size->nat_size = natural_height;
523 _gtk_cell_area_box_context_sum (box_context, GTK_ORIENTATION_VERTICAL, -1, NULL, NULL);
527 _gtk_cell_area_box_context_push_group_width_for_height (GtkCellAreaBoxContext *box_context,
533 GtkCellAreaBoxContextPrivate *priv;
537 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
539 priv = box_context->priv;
540 g_return_if_fail (group_idx < priv->base_widths->len);
542 group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));
545 group_array = group_array_new (box_context);
546 g_hash_table_insert (priv->widths, GINT_TO_POINTER (for_height), group_array);
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);
555 _gtk_cell_area_box_context_get_group_width (GtkCellAreaBoxContext *box_context,
560 GtkCellAreaBoxContextPrivate *priv;
563 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
565 priv = box_context->priv;
566 g_return_if_fail (group_idx < priv->base_widths->len);
568 size = &g_array_index (priv->base_widths, CachedSize, group_idx);
571 *minimum_width = size->min_size;
574 *natural_width = size->nat_size;
578 _gtk_cell_area_box_context_get_group_height_for_width (GtkCellAreaBoxContext *box_context,
581 gint *minimum_height,
582 gint *natural_height)
584 GtkCellAreaBoxContextPrivate *priv;
587 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
589 priv = box_context->priv;
590 g_return_if_fail (group_idx < priv->base_widths->len);
592 group_array = g_hash_table_lookup (priv->heights, GINT_TO_POINTER (for_width));
596 CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);
599 *minimum_height = size->min_size;
602 *natural_height = size->nat_size;
607 *minimum_height = -1;
610 *natural_height = -1;
615 _gtk_cell_area_box_context_get_group_height (GtkCellAreaBoxContext *box_context,
617 gint *minimum_height,
618 gint *natural_height)
620 GtkCellAreaBoxContextPrivate *priv;
623 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
625 priv = box_context->priv;
626 g_return_if_fail (group_idx < priv->base_heights->len);
628 size = &g_array_index (priv->base_heights, CachedSize, group_idx);
631 *minimum_height = size->min_size;
634 *natural_height = size->nat_size;
638 _gtk_cell_area_box_context_get_group_width_for_height (GtkCellAreaBoxContext *box_context,
644 GtkCellAreaBoxContextPrivate *priv;
647 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (box_context));
649 priv = box_context->priv;
650 g_return_if_fail (group_idx < priv->base_widths->len);
652 group_array = g_hash_table_lookup (priv->widths, GINT_TO_POINTER (for_height));
656 CachedSize *size = &g_array_index (group_array, CachedSize, group_idx);
659 *minimum_width = size->min_size;
662 *natural_width = size->nat_size;
674 static GtkRequestedSize *
675 _gtk_cell_area_box_context_get_requests (GtkCellAreaBoxContext *box_context,
676 GtkCellAreaBox *area,
677 GtkOrientation orientation,
681 GtkCellAreaBoxContextPrivate *priv = box_context->priv;
682 GtkRequestedSize *requests;
685 gint visible_groups = 0;
686 gint last_aligned_group_idx = 0;
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--)
693 if (priv->align[i] &&
694 _gtk_cell_area_box_group_visible (area, i))
697 last_aligned_group_idx = i >= 0 ? i : 0;
699 priv = box_context->priv;
700 array = get_array (box_context, orientation, for_size);
702 for (i = 0; i < array->len; i++)
704 size = &g_array_index (array, CachedSize, i);
706 if (size->nat_size > 0 &&
707 (i <= last_aligned_group_idx ||
708 _gtk_cell_area_box_group_visible (area, i)))
712 requests = g_new (GtkRequestedSize, visible_groups);
714 for (j = 0, i = 0; i < array->len; i++)
716 size = &g_array_index (array, CachedSize, i);
718 if (size->nat_size > 0 &&
719 (i <= last_aligned_group_idx ||
720 _gtk_cell_area_box_group_visible (area, i)))
722 requests[j].data = GINT_TO_POINTER (i);
723 requests[j].minimum_size = size->min_size;
724 requests[j].natural_size = size->nat_size;
730 *n_requests = visible_groups;
735 static GtkCellAreaBoxAllocation *
736 allocate_for_orientation (GtkCellAreaBoxContext *context,
737 GtkCellAreaBox *area,
738 GtkOrientation orientation,
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;
752 sizes = _gtk_cell_area_box_context_get_requests (context, area, orientation, for_size, &n_groups);
753 n_expand_groups = count_expand_groups (context);
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;
761 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, sizes);
765 /* Calculate/distribute expand for groups */
766 if (n_expand_groups > 0)
768 extra_size = avail_size / n_expand_groups;
769 extra_extra = avail_size % n_expand_groups;
772 extra_size = extra_extra = 0;
774 allocs = g_new (GtkCellAreaBoxAllocation, n_groups);
776 for (vis_position = 0, position = 0, i = 0; i < n_groups; i++)
778 allocs[i].group_idx = GPOINTER_TO_INT (sizes[i].data);
780 if (priv->align[allocs[i].group_idx])
781 vis_position = position;
783 allocs[i].position = vis_position;
784 allocs[i].size = sizes[i].minimum_size;
786 if (group_expands (context, allocs[i].group_idx))
788 allocs[i].size += extra_size;
796 position += allocs[i].size;
799 if (_gtk_cell_area_box_group_visible (area, allocs[i].group_idx))
801 vis_position += allocs[i].size;
802 vis_position += spacing;
807 *n_allocs = n_groups;
815 _gtk_cell_area_box_context_get_widths (GtkCellAreaBoxContext *box_context,
818 GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));
820 return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_HORIZONTAL, -1, n_widths);
824 _gtk_cell_area_box_context_get_heights (GtkCellAreaBoxContext *box_context,
827 GtkCellAreaBox *area = (GtkCellAreaBox *)gtk_cell_area_context_get_area (GTK_CELL_AREA_CONTEXT (box_context));
829 return _gtk_cell_area_box_context_get_requests (box_context, area, GTK_ORIENTATION_VERTICAL, -1, n_heights);
832 GtkCellAreaBoxAllocation *
833 _gtk_cell_area_box_context_get_orientation_allocs (GtkCellAreaBoxContext *context,
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;
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);
846 gtk_cell_area_context_get_allocation (ctx, &width, &height);
848 if (orientation == GTK_ORIENTATION_HORIZONTAL && width > 0)
849 allocs = allocate_for_orientation (context, area, orientation,
850 spacing, width, height,
852 else if (orientation == GTK_ORIENTATION_VERTICAL && height > 0)
853 allocs = allocate_for_orientation (context, area, orientation,
854 spacing, height, width,
857 *n_allocs = alloc_count;