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 "gtkorientable.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellareabox.h"
29 #include "gtkcellareaboxiter.h"
30 #include "gtkprivate.h"
34 static void gtk_cell_area_box_finalize (GObject *object);
35 static void gtk_cell_area_box_dispose (GObject *object);
36 static void gtk_cell_area_box_set_property (GObject *object,
40 static void gtk_cell_area_box_get_property (GObject *object,
45 /* GtkCellAreaClass */
46 static void gtk_cell_area_box_add (GtkCellArea *area,
47 GtkCellRenderer *renderer);
48 static void gtk_cell_area_box_remove (GtkCellArea *area,
49 GtkCellRenderer *renderer);
50 static void gtk_cell_area_box_forall (GtkCellArea *area,
51 GtkCellCallback callback,
52 gpointer callback_data);
53 static gint gtk_cell_area_box_event (GtkCellArea *area,
54 GtkCellAreaIter *iter,
57 const GdkRectangle *cell_area);
58 static void gtk_cell_area_box_render (GtkCellArea *area,
59 GtkCellAreaIter *iter,
62 const GdkRectangle *cell_area);
64 static GtkCellAreaIter *gtk_cell_area_box_create_iter (GtkCellArea *area);
65 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
66 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
67 GtkCellAreaIter *iter,
71 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
72 GtkCellAreaIter *iter,
75 gint *natural_height);
76 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
77 GtkCellAreaIter *iter,
81 gint *natural_height);
82 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
83 GtkCellAreaIter *iter,
89 /* GtkCellLayoutIface */
90 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
91 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
92 GtkCellRenderer *renderer,
94 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
95 GtkCellRenderer *renderer,
97 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
98 GtkCellRenderer *renderer,
102 /* CellInfo/CellGroup metadata handling and convenience functions */
104 GtkCellRenderer *renderer;
106 guint expand : 1; /* Whether the cell expands */
107 guint pack : 1; /* Whether the cell is packed from the start or end */
108 guint align : 1; /* Whether to align this cell's position with adjacent rows */
116 guint expand_cells : 8;
120 GtkCellRenderer *renderer;
126 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
130 static void cell_info_free (CellInfo *info);
131 static gint cell_info_find (CellInfo *info,
132 GtkCellRenderer *renderer);
133 static CellGroup *cell_group_new (guint id);
134 static void cell_group_free (CellGroup *group);
135 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
138 static void allocated_cell_free (AllocatedCell *cell);
139 static GList *list_consecutive_cells (GtkCellAreaBox *box);
140 static GList *construct_cell_groups (GtkCellAreaBox *box);
141 static gint count_expand_groups (GtkCellAreaBox *box);
142 static void iter_weak_notify (GtkCellAreaBox *box,
143 GtkCellAreaBoxIter *dead_iter);
144 static void flush_iters (GtkCellAreaBox *box);
145 static void init_iter_groups (GtkCellAreaBox *box);
146 static void init_iter_group (GtkCellAreaBox *box,
147 GtkCellAreaBoxIter *iter);
148 static void get_renderer_size (GtkCellRenderer *renderer,
149 GtkOrientation orientation,
154 static GSList *get_allocated_cells (GtkCellAreaBox *box,
155 GtkCellAreaBoxIter *iter,
159 struct _GtkCellAreaBoxPrivate
161 GtkOrientation orientation;
179 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
180 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
181 gtk_cell_area_box_cell_layout_init)
182 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
184 #define OPPOSITE_ORIENTATION(orientation) \
185 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
186 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
189 gtk_cell_area_box_init (GtkCellAreaBox *box)
191 GtkCellAreaBoxPrivate *priv;
193 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
194 GTK_TYPE_CELL_AREA_BOX,
195 GtkCellAreaBoxPrivate);
198 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
206 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
208 GObjectClass *object_class = G_OBJECT_CLASS (class);
209 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
212 object_class->finalize = gtk_cell_area_box_finalize;
213 object_class->dispose = gtk_cell_area_box_dispose;
214 object_class->set_property = gtk_cell_area_box_set_property;
215 object_class->get_property = gtk_cell_area_box_get_property;
217 /* GtkCellAreaClass */
218 area_class->add = gtk_cell_area_box_add;
219 area_class->remove = gtk_cell_area_box_remove;
220 area_class->forall = gtk_cell_area_box_forall;
221 area_class->event = gtk_cell_area_box_event;
222 area_class->render = gtk_cell_area_box_render;
224 area_class->create_iter = gtk_cell_area_box_create_iter;
225 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
226 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
227 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
228 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
229 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
231 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
233 g_object_class_install_property (object_class,
235 g_param_spec_int ("spacing",
237 P_("Space which is inserted between cells"),
241 GTK_PARAM_READWRITE));
243 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
247 /*************************************************************
248 * CellInfo/CellGroup basics and convenience functions *
249 *************************************************************/
251 cell_info_new (GtkCellRenderer *renderer,
256 CellInfo *info = g_slice_new (CellInfo);
258 info->renderer = g_object_ref_sink (renderer);
260 info->expand = expand;
267 cell_info_free (CellInfo *info)
269 g_object_unref (info->renderer);
271 g_slice_free (CellInfo, info);
275 cell_info_find (CellInfo *info,
276 GtkCellRenderer *renderer)
278 return (info->renderer == renderer) ? 0 : -1;
282 cell_group_new (guint id)
284 CellGroup *group = g_slice_new0 (CellGroup);
292 cell_group_free (CellGroup *group)
294 g_list_free (group->cells);
295 g_slice_free (CellGroup, group);
298 static AllocatedCell *
299 allocated_cell_new (GtkCellRenderer *renderer,
303 AllocatedCell *cell = g_slice_new (AllocatedCell);
305 cell->renderer = renderer;
306 cell->position = position;
313 allocated_cell_free (AllocatedCell *cell)
315 g_slice_free (AllocatedCell, cell);
319 list_consecutive_cells (GtkCellAreaBox *box)
321 GtkCellAreaBoxPrivate *priv = box->priv;
322 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
325 /* List cells in consecutive order taking their
326 * PACK_START/PACK_END options into account
328 for (l = priv->cells; l; l = l->next)
332 if (info->pack == GTK_PACK_START)
333 consecutive_cells = g_list_prepend (consecutive_cells, info);
336 for (l = priv->cells; l; l = l->next)
340 if (info->pack == GTK_PACK_END)
341 pack_end_cells = g_list_prepend (pack_end_cells, info);
344 consecutive_cells = g_list_reverse (consecutive_cells);
345 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
347 return consecutive_cells;
351 construct_cell_groups (GtkCellAreaBox *box)
353 GtkCellAreaBoxPrivate *priv = box->priv;
356 GList *groups = NULL;
362 cells = list_consecutive_cells (box);
363 group = cell_group_new (id++);
364 groups = g_list_prepend (groups, group);
366 for (l = cells; l; l = l->next)
368 CellInfo *info = l->data;
370 /* A new group starts with any aligned cell, the first group is implied */
371 if (info->align && l != cells)
373 group = cell_group_new (id++);
374 groups = g_list_prepend (groups, group);
377 group->cells = g_list_prepend (group->cells, info);
380 /* A group expands if it contains any expand cells */
382 group->expand_cells ++;
387 for (l = cells; l; l = l->next)
390 group->cells = g_list_reverse (group->cells);
393 return g_list_reverse (groups);
397 count_expand_groups (GtkCellAreaBox *box)
399 GtkCellAreaBoxPrivate *priv = box->priv;
401 gint expand_groups = 0;
403 for (l = priv->groups; l; l = l->next)
405 CellGroup *group = l->data;
407 if (group->expand_cells > 0)
411 return expand_groups;
415 iter_weak_notify (GtkCellAreaBox *box,
416 GtkCellAreaBoxIter *dead_iter)
418 GtkCellAreaBoxPrivate *priv = box->priv;
420 priv->iters = g_slist_remove (priv->iters, dead_iter);
424 init_iter_group (GtkCellAreaBox *box,
425 GtkCellAreaBoxIter *iter)
427 GtkCellAreaBoxPrivate *priv = box->priv;
428 gint n_groups, *expand_groups, i;
431 n_groups = g_list_length (priv->groups);
432 expand_groups = g_new (gboolean, n_groups);
434 for (i = 0, l = priv->groups; l; l = l->next, i++)
436 CellGroup *group = l->data;
438 expand_groups[i] = (group->expand_cells > 0);
441 gtk_cell_area_box_init_groups (iter, n_groups, expand_groups);
442 g_free (expand_groups);
446 init_iter_groups (GtkCellAreaBox *box)
448 GtkCellAreaBoxPrivate *priv = box->priv;
451 /* When the box's groups are reconstructed, iters need to
454 for (l = priv->iters; l; l = l->next)
456 GtkCellAreaBoxIter *iter = l->data;
458 init_iter_group (box, iter);
463 flush_iters (GtkCellAreaBox *box)
465 GtkCellAreaBoxPrivate *priv = box->priv;
468 /* When the box layout changes, iters need to
469 * be flushed and sizes for the box get requested again
471 for (l = priv->iters; l; l = l->next)
473 GtkCellAreaIter *iter = l->data;
475 gtk_cell_area_iter_flush (iter);
479 /* Returns an allocation for each cell in the orientation of the box,
480 * used in ->render()/->event() implementations to get a straight-forward
481 * list of allocated cells to operate on.
484 get_allocated_cells (GtkCellAreaBox *box,
485 GtkCellAreaBoxIter *iter,
488 const GtkCellAreaBoxAllocation *group_allocs;
489 GtkCellAreaBoxPrivate *priv = box->priv;
490 GList *group_list, *cell_list;
491 GSList *allocated_cells = NULL;
494 group_allocs = gtk_cell_area_box_iter_get_orientation_allocs (iter, &n_allocs);
497 g_warning ("Trying to operate on an unallocated GtkCellAreaIter, "
498 "GtkCellAreaBox requires that the iter be allocated at least "
499 "in the orientation of the box");
503 for (i = 0, group_list = priv->groups; group_list; i++, group_list = group_list->next)
505 CellGroup *group = group_list->data;
507 /* Exception for single cell groups */
508 if (group->n_cells == 1)
510 CellInfo *info = group->cells->data;
511 AllocatedCell *cell =
512 allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
514 allocated_cells = g_slist_prepend (allocated_cells, cell);
518 GtkRequestedSize *sizes = g_new (GtkRequestedSize, group->n_cells);
519 gint avail_size = group_allocs[i].size;
520 gint position = group_allocs[i].position;
521 gint extra_size, extra_extra;
523 for (j = 0, cell_list = group->cells; cell_list; j++, cell_list = cell_list->next)
525 CellInfo *info = cell_list->data;
527 get_renderer_size (info->renderer,
530 &sizes[j].minimum_size,
531 &sizes[j].natural_size);
533 avail_size -= sizes[j].minimum_size;
536 /* Distribute cells naturally within the group */
537 avail_size -= (group->n_cells - 1) * priv->spacing;
538 avail_size = gtk_distribute_natural_allocation (avail_size, group->n_cells, sizes);
540 /* Calculate/distribute expand for cells */
541 if (group->expand_cells > 0)
543 extra_size = avail_size / group->expand_cells;
544 extra_extra = avail_size % group->expand_cells;
547 extra_size = extra_extra = 0;
549 /* Create the allocated cells */
550 for (j = 0, cell_list = group->cells; cell_list; j++, cell_list = cell_list->next)
552 CellInfo *info = cell_list->data;
557 sizes[j].minimum_size += extra_size;
560 sizes[j].minimum_size++;
565 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
567 allocated_cells = g_slist_prepend (allocated_cells, cell);
569 position += sizes[j].minimum_size;
570 position += priv->spacing;
577 return g_slist_reverse (allocated_cells);
580 /*************************************************************
582 *************************************************************/
584 gtk_cell_area_box_finalize (GObject *object)
586 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
587 GtkCellAreaBoxPrivate *priv = box->priv;
590 for (l = priv->iters; l; l = l->next)
591 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)iter_weak_notify, box);
593 g_slist_free (priv->iters);
595 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
599 gtk_cell_area_box_dispose (GObject *object)
601 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
605 gtk_cell_area_box_set_property (GObject *object,
610 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
614 case PROP_ORIENTATION:
615 box->priv->orientation = g_value_get_enum (value);
617 /* Notify that size needs to be requested again */
621 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
624 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630 gtk_cell_area_box_get_property (GObject *object,
635 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
639 case PROP_ORIENTATION:
640 g_value_set_enum (value, box->priv->orientation);
643 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
646 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 /*************************************************************
653 *************************************************************/
655 gtk_cell_area_box_add (GtkCellArea *area,
656 GtkCellRenderer *renderer)
658 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
659 renderer, FALSE, TRUE);
663 gtk_cell_area_box_remove (GtkCellArea *area,
664 GtkCellRenderer *renderer)
666 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
667 GtkCellAreaBoxPrivate *priv = box->priv;
670 node = g_list_find_custom (priv->cells, renderer,
671 (GCompareFunc)cell_info_find);
675 CellInfo *info = node->data;
677 cell_info_free (info);
679 priv->cells = g_list_delete_link (priv->cells, node);
681 /* Reconstruct cell groups */
682 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
683 g_list_free (priv->groups);
684 priv->groups = construct_cell_groups (box);
686 /* Reinitialize groups on iters */
687 init_iter_groups (box);
690 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
694 gtk_cell_area_box_forall (GtkCellArea *area,
695 GtkCellCallback callback,
696 gpointer callback_data)
698 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
699 GtkCellAreaBoxPrivate *priv = box->priv;
702 for (list = priv->cells; list; list = list->next)
704 CellInfo *info = list->data;
706 callback (info->renderer, callback_data);
711 gtk_cell_area_box_event (GtkCellArea *area,
712 GtkCellAreaIter *iter,
715 const GdkRectangle *cell_area)
723 gtk_cell_area_box_render (GtkCellArea *area,
724 GtkCellAreaIter *iter,
727 const GdkRectangle *cell_area)
729 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
730 GtkCellAreaBoxIter *box_iter = GTK_CELL_AREA_BOX_ITER (iter);
731 GSList *allocated_cells, *l;
733 /* Get a list of cells with allocation sizes decided regardless
734 * of alignments and pack order etc. */
735 allocated_cells = get_allocated_cells (box, box_iter, widget);
737 for (l = allocated_cells; l; l = l->next)
739 AllocatedCell *cell = l->data;
744 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
745 g_slist_free (allocated_cells);
748 static GtkCellAreaIter *
749 gtk_cell_area_box_create_iter (GtkCellArea *area)
751 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
752 GtkCellAreaBoxPrivate *priv = box->priv;
753 GtkCellAreaIter *iter =
754 (GtkCellAreaIter *)g_object_new (GTK_TYPE_CELL_AREA_BOX_ITER, NULL);
756 priv->iters = g_slist_prepend (priv->iters, iter);
758 g_object_weak_ref (G_OBJECT (iter), (GWeakNotify)iter_weak_notify, box);
760 /* Tell the new group about our cell layout */
761 init_iter_group (box, GTK_CELL_AREA_BOX_ITER (iter));
766 static GtkSizeRequestMode
767 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
769 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
770 GtkCellAreaBoxPrivate *priv = box->priv;
772 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
773 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
774 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
778 get_renderer_size (GtkCellRenderer *renderer,
779 GtkOrientation orientation,
785 if (orientation == GTK_ORIENTATION_HORIZONTAL)
788 gtk_cell_renderer_get_preferred_width (renderer, widget, minimum_size, natural_size);
790 gtk_cell_renderer_get_preferred_width_for_height (renderer, widget, for_size,
791 minimum_size, natural_size);
793 else /* GTK_ORIENTATION_VERTICAL */
796 gtk_cell_renderer_get_preferred_height (renderer, widget, minimum_size, natural_size);
798 gtk_cell_renderer_get_preferred_height_for_width (renderer, widget, for_size,
799 minimum_size, natural_size);
804 compute_size (GtkCellAreaBox *box,
805 GtkOrientation orientation,
806 GtkCellAreaBoxIter *iter,
812 GtkCellAreaBoxPrivate *priv = box->priv;
815 GList *cell_list, *group_list;
819 for (group_list = priv->groups; group_list; group_list = group_list->next)
821 gint group_min_size = 0;
822 gint group_nat_size = 0;
824 group = group_list->data;
826 for (cell_list = group->cells; cell_list; cell_list = cell_list->next)
828 gint renderer_min_size, renderer_nat_size;
830 info = cell_list->data;
832 get_renderer_size (info->renderer, orientation, widget, for_size,
833 &renderer_min_size, &renderer_nat_size);
835 if (orientation == priv->orientation)
839 min_size += priv->spacing;
840 nat_size += priv->spacing;
843 if (group_min_size > 0)
845 group_min_size += priv->spacing;
846 group_nat_size += priv->spacing;
849 min_size += renderer_min_size;
850 nat_size += renderer_nat_size;
851 group_min_size += renderer_min_size;
852 group_nat_size += renderer_nat_size;
856 min_size = MAX (min_size, renderer_min_size);
857 nat_size = MAX (nat_size, renderer_nat_size);
858 group_min_size = MAX (group_min_size, renderer_min_size);
859 group_nat_size = MAX (group_nat_size, renderer_nat_size);
863 if (orientation == GTK_ORIENTATION_HORIZONTAL)
866 gtk_cell_area_box_iter_push_group_width (iter, group->id, group_min_size, group_nat_size);
868 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
869 group_min_size, group_nat_size);
874 gtk_cell_area_box_iter_push_group_height (iter, group->id, group_min_size, group_nat_size);
876 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
877 group_min_size, group_nat_size);
881 *minimum_size = min_size;
882 *natural_size = nat_size;
886 get_group_sizes (CellGroup *group,
887 GtkOrientation orientation,
891 GtkRequestedSize *sizes;
895 *n_sizes = g_list_length (group->cells);
896 sizes = g_new (GtkRequestedSize, *n_sizes);
898 for (l = group->cells, i = 0; l; l = l->next, i++)
900 CellInfo *info = l->data;
902 sizes[i].data = info;
904 get_renderer_size (info->renderer,
905 orientation, widget, -1,
906 &sizes[i].minimum_size,
907 &sizes[i].natural_size);
914 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
921 GtkCellAreaBoxPrivate *priv = box->priv;
923 /* Exception for single cell groups */
924 if (group->n_cells == 1)
926 CellInfo *info = group->cells->data;
928 get_renderer_size (info->renderer,
929 OPPOSITE_ORIENTATION (priv->orientation),
930 widget, for_size, minimum_size, natural_size);
934 GtkRequestedSize *orientation_sizes;
937 gint avail_size = for_size;
938 gint extra_size, extra_extra;
939 gint min_size = 0, nat_size = 0;
941 orientation_sizes = get_group_sizes (group, priv->orientation, widget, &n_sizes);
943 /* First naturally allocate the cells in the group into the for_size */
944 avail_size -= (n_sizes - 1) * priv->spacing;
945 for (i = 0; i < n_sizes; i++)
946 avail_size -= orientation_sizes[i].minimum_size;
948 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
950 /* Calculate/distribute expand for cells */
951 if (group->expand_cells > 0)
953 extra_size = avail_size / group->expand_cells;
954 extra_extra = avail_size % group->expand_cells;
957 extra_size = extra_extra = 0;
959 for (i = 0; i < n_sizes; i++)
961 gint cell_min, cell_nat;
963 info = orientation_sizes[i].data;
967 orientation_sizes[i].minimum_size += extra_size;
970 orientation_sizes[i].minimum_size++;
975 get_renderer_size (info->renderer,
976 OPPOSITE_ORIENTATION (priv->orientation),
978 orientation_sizes[i].minimum_size,
979 &cell_min, &cell_nat);
981 min_size = MAX (min_size, cell_min);
982 nat_size = MAX (nat_size, cell_nat);
985 *minimum_size = min_size;
986 *natural_size = nat_size;
988 g_free (orientation_sizes);
993 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
994 GtkCellAreaBoxIter *iter,
1000 GtkCellAreaBoxPrivate *priv = box->priv;
1003 GtkRequestedSize *orientation_sizes;
1004 gint n_groups, n_expand_groups, i;
1005 gint avail_size = for_size;
1006 gint extra_size, extra_extra;
1007 gint min_size = 0, nat_size = 0;
1009 n_expand_groups = count_expand_groups (box);
1011 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1012 orientation_sizes = gtk_cell_area_box_iter_get_widths (iter, &n_groups);
1014 orientation_sizes = gtk_cell_area_box_iter_get_heights (iter, &n_groups);
1016 /* First start by naturally allocating space among groups of cells */
1017 avail_size -= (n_groups - 1) * priv->spacing;
1018 for (i = 0; i < n_groups; i++)
1019 avail_size -= orientation_sizes[i].minimum_size;
1021 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1023 /* Calculate/distribute expand for groups */
1024 if (n_expand_groups > 0)
1026 extra_size = avail_size / n_expand_groups;
1027 extra_extra = avail_size % n_expand_groups;
1030 extra_size = extra_extra = 0;
1032 /* Now we need to naturally allocate sizes for cells in each group
1033 * and push the height-for-width for each group accordingly while accumulating
1034 * the overall height-for-width for this row.
1036 for (group_list = priv->groups; group_list; group_list = group_list->next)
1038 gint group_min, group_nat;
1040 group = group_list->data;
1042 if (group->expand_cells > 0)
1044 orientation_sizes[group->id].minimum_size += extra_size;
1047 orientation_sizes[group->id].minimum_size++;
1052 /* Now we have the allocation for the group, request it's height-for-width */
1053 compute_group_size_for_opposing_orientation (box, group, widget,
1054 orientation_sizes[group->id].minimum_size,
1055 &group_min, &group_nat);
1057 min_size = MAX (min_size, group_min);
1058 nat_size = MAX (nat_size, group_nat);
1060 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1062 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
1063 group_min, group_nat);
1067 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
1068 group_min, group_nat);
1072 *minimum_size = min_size;
1073 *natural_size = nat_size;
1075 g_free (orientation_sizes);
1081 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1082 GtkCellAreaIter *iter,
1084 gint *minimum_width,
1085 gint *natural_width)
1087 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1088 GtkCellAreaBoxIter *box_iter;
1089 gint min_width, nat_width;
1091 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1093 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1095 /* Compute the size of all renderers for current row data,
1096 * bumping cell alignments in the iter along the way */
1097 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1098 box_iter, widget, -1, &min_width, &nat_width);
1101 *minimum_width = min_width;
1104 *natural_width = nat_width;
1108 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1109 GtkCellAreaIter *iter,
1111 gint *minimum_height,
1112 gint *natural_height)
1114 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1115 GtkCellAreaBoxIter *box_iter;
1116 gint min_height, nat_height;
1118 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1120 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1122 /* Compute the size of all renderers for current row data,
1123 * bumping cell alignments in the iter along the way */
1124 compute_size (box, GTK_ORIENTATION_VERTICAL,
1125 box_iter, widget, -1, &min_height, &nat_height);
1128 *minimum_height = min_height;
1131 *natural_height = nat_height;
1135 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1136 GtkCellAreaIter *iter,
1139 gint *minimum_height,
1140 gint *natural_height)
1142 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1143 GtkCellAreaBoxIter *box_iter;
1144 GtkCellAreaBoxPrivate *priv;
1145 gint min_height, nat_height;
1147 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1149 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1152 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1154 /* Add up vertical requests of height for width and push the overall
1155 * cached sizes for alignments */
1156 compute_size (box, priv->orientation, box_iter, widget, width, &min_height, &nat_height);
1160 /* Juice: virtually allocate cells into the for_width using the
1161 * alignments and then return the overall height for that width, and cache it */
1162 compute_size_for_opposing_orientation (box, box_iter, widget, width, &min_height, &nat_height);
1166 *minimum_height = min_height;
1169 *natural_height = nat_height;
1173 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1174 GtkCellAreaIter *iter,
1177 gint *minimum_width,
1178 gint *natural_width)
1180 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1181 GtkCellAreaBoxIter *box_iter;
1182 GtkCellAreaBoxPrivate *priv;
1183 gint min_width, nat_width;
1185 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1187 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1190 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1192 /* Add up horizontal requests of width for height and push the overall
1193 * cached sizes for alignments */
1194 compute_size (box, priv->orientation, box_iter, widget, height, &min_width, &nat_width);
1198 /* Juice: horizontally allocate cells into the for_height using the
1199 * alignments and then return the overall width for that height, and cache it */
1200 compute_size_for_opposing_orientation (box, box_iter, widget, height, &min_width, &nat_width);
1204 *minimum_width = min_width;
1207 *natural_width = nat_width;
1211 /*************************************************************
1212 * GtkCellLayoutIface *
1213 *************************************************************/
1215 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1217 iface->pack_start = gtk_cell_area_box_layout_pack_start;
1218 iface->pack_end = gtk_cell_area_box_layout_pack_end;
1219 iface->reorder = gtk_cell_area_box_layout_reorder;
1223 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
1224 GtkCellRenderer *renderer,
1227 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1231 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
1232 GtkCellRenderer *renderer,
1235 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1239 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
1240 GtkCellRenderer *renderer,
1243 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
1244 GtkCellAreaBoxPrivate *priv = box->priv;
1248 node = g_list_find_custom (priv->cells, renderer,
1249 (GCompareFunc)cell_info_find);
1255 priv->cells = g_list_delete_link (priv->cells, node);
1256 priv->cells = g_list_insert (priv->cells, info, position);
1258 /* Reconstruct cell groups */
1259 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1260 g_list_free (priv->groups);
1261 priv->groups = construct_cell_groups (box);
1263 /* Reinitialize groups on iters */
1264 init_iter_groups (box);
1268 /*************************************************************
1270 *************************************************************/
1272 gtk_cell_area_box_new (void)
1274 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
1278 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
1279 GtkCellRenderer *renderer,
1283 GtkCellAreaBoxPrivate *priv;
1286 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1287 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1291 if (g_list_find_custom (priv->cells, renderer,
1292 (GCompareFunc)cell_info_find))
1294 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
1298 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
1300 priv->cells = g_list_append (priv->cells, info);
1302 /* Reconstruct cell groups */
1303 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1304 g_list_free (priv->groups);
1305 priv->groups = construct_cell_groups (box);
1307 /* Reinitialize groups on iters */
1308 init_iter_groups (box);
1312 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
1313 GtkCellRenderer *renderer,
1317 GtkCellAreaBoxPrivate *priv;
1320 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1321 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1325 if (g_list_find_custom (priv->cells, renderer,
1326 (GCompareFunc)cell_info_find))
1328 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
1332 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
1334 priv->cells = g_list_append (priv->cells, info);
1336 /* Reconstruct cell groups */
1337 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1338 g_list_free (priv->groups);
1339 priv->groups = construct_cell_groups (box);
1341 /* Reinitialize groups on iters */
1342 init_iter_groups (box);
1346 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
1348 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
1350 return box->priv->spacing;
1354 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
1357 GtkCellAreaBoxPrivate *priv;
1359 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1363 if (priv->spacing != spacing)
1365 priv->spacing = spacing;
1367 g_object_notify (G_OBJECT (box), "spacing");
1369 /* Notify that size needs to be requested again */