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 GtkCellRendererState flags);
59 static void gtk_cell_area_box_render (GtkCellArea *area,
60 GtkCellAreaIter *iter,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
66 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
67 GtkCellRenderer *renderer,
71 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
72 GtkCellRenderer *renderer,
77 static GtkCellAreaIter *gtk_cell_area_box_create_iter (GtkCellArea *area);
78 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
79 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
80 GtkCellAreaIter *iter,
84 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
85 GtkCellAreaIter *iter,
88 gint *natural_height);
89 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
90 GtkCellAreaIter *iter,
94 gint *natural_height);
95 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
96 GtkCellAreaIter *iter,
100 gint *natural_width);
102 /* GtkCellLayoutIface */
103 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
104 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
105 GtkCellRenderer *renderer,
107 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
108 GtkCellRenderer *renderer,
110 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
111 GtkCellRenderer *renderer,
115 /* CellInfo/CellGroup metadata handling and convenience functions */
117 GtkCellRenderer *renderer;
119 guint expand : 1; /* Whether the cell expands */
120 guint pack : 1; /* Whether the cell is packed from the start or end */
121 guint align : 1; /* Whether to align this cell's position with adjacent rows */
129 guint expand_cells : 8;
133 GtkCellRenderer *renderer;
139 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
143 static void cell_info_free (CellInfo *info);
144 static gint cell_info_find (CellInfo *info,
145 GtkCellRenderer *renderer);
146 static CellGroup *cell_group_new (guint id);
147 static void cell_group_free (CellGroup *group);
148 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
151 static void allocated_cell_free (AllocatedCell *cell);
152 static GList *list_consecutive_cells (GtkCellAreaBox *box);
153 static GList *construct_cell_groups (GtkCellAreaBox *box);
154 static gint count_expand_groups (GtkCellAreaBox *box);
155 static void iter_weak_notify (GtkCellAreaBox *box,
156 GtkCellAreaBoxIter *dead_iter);
157 static void flush_iters (GtkCellAreaBox *box);
158 static void init_iter_groups (GtkCellAreaBox *box);
159 static void init_iter_group (GtkCellAreaBox *box,
160 GtkCellAreaBoxIter *iter);
161 static GSList *get_allocated_cells (GtkCellAreaBox *box,
162 GtkCellAreaBoxIter *iter,
166 struct _GtkCellAreaBoxPrivate
168 GtkOrientation orientation;
191 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
193 gtk_cell_area_box_cell_layout_init)
194 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
196 #define OPPOSITE_ORIENTATION(orientation) \
197 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
198 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
201 gtk_cell_area_box_init (GtkCellAreaBox *box)
203 GtkCellAreaBoxPrivate *priv;
205 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
206 GTK_TYPE_CELL_AREA_BOX,
207 GtkCellAreaBoxPrivate);
210 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
218 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
220 GObjectClass *object_class = G_OBJECT_CLASS (class);
221 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
224 object_class->finalize = gtk_cell_area_box_finalize;
225 object_class->dispose = gtk_cell_area_box_dispose;
226 object_class->set_property = gtk_cell_area_box_set_property;
227 object_class->get_property = gtk_cell_area_box_get_property;
229 /* GtkCellAreaClass */
230 area_class->add = gtk_cell_area_box_add;
231 area_class->remove = gtk_cell_area_box_remove;
232 area_class->forall = gtk_cell_area_box_forall;
233 area_class->event = gtk_cell_area_box_event;
234 area_class->render = gtk_cell_area_box_render;
235 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
236 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
238 area_class->create_iter = gtk_cell_area_box_create_iter;
239 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
240 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
241 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
242 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
243 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
246 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
248 g_object_class_install_property (object_class,
250 g_param_spec_int ("spacing",
252 P_("Space which is inserted between cells"),
256 GTK_PARAM_READWRITE));
258 /* Cell Properties */
259 gtk_cell_area_class_install_cell_property (area_class,
264 P_("Whether the cell expands"),
266 GTK_PARAM_READWRITE));
268 gtk_cell_area_class_install_cell_property (area_class,
273 P_("Whether cell should align with adjacent rows"),
275 GTK_PARAM_READWRITE));
277 gtk_cell_area_class_install_cell_property (area_class,
282 P_("A GtkPackType indicating whether the cell is packed with "
283 "reference to the start or end of the cell area"),
284 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
285 GTK_PARAM_READWRITE));
287 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
291 /*************************************************************
292 * CellInfo/CellGroup basics and convenience functions *
293 *************************************************************/
295 cell_info_new (GtkCellRenderer *renderer,
300 CellInfo *info = g_slice_new (CellInfo);
302 info->renderer = g_object_ref_sink (renderer);
304 info->expand = expand;
311 cell_info_free (CellInfo *info)
313 g_object_unref (info->renderer);
315 g_slice_free (CellInfo, info);
319 cell_info_find (CellInfo *info,
320 GtkCellRenderer *renderer)
322 return (info->renderer == renderer) ? 0 : -1;
326 cell_group_new (guint id)
328 CellGroup *group = g_slice_new0 (CellGroup);
336 cell_group_free (CellGroup *group)
338 g_list_free (group->cells);
339 g_slice_free (CellGroup, group);
342 static AllocatedCell *
343 allocated_cell_new (GtkCellRenderer *renderer,
347 AllocatedCell *cell = g_slice_new (AllocatedCell);
349 cell->renderer = renderer;
350 cell->position = position;
357 allocated_cell_free (AllocatedCell *cell)
359 g_slice_free (AllocatedCell, cell);
363 list_consecutive_cells (GtkCellAreaBox *box)
365 GtkCellAreaBoxPrivate *priv = box->priv;
366 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
369 /* List cells in consecutive order taking their
370 * PACK_START/PACK_END options into account
372 for (l = priv->cells; l; l = l->next)
376 if (info->pack == GTK_PACK_START)
377 consecutive_cells = g_list_prepend (consecutive_cells, info);
380 for (l = priv->cells; l; l = l->next)
384 if (info->pack == GTK_PACK_END)
385 pack_end_cells = g_list_prepend (pack_end_cells, info);
388 consecutive_cells = g_list_reverse (consecutive_cells);
389 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
391 return consecutive_cells;
395 construct_cell_groups (GtkCellAreaBox *box)
397 GtkCellAreaBoxPrivate *priv = box->priv;
400 GList *groups = NULL;
406 cells = list_consecutive_cells (box);
407 group = cell_group_new (id++);
408 groups = g_list_prepend (groups, group);
410 for (l = cells; l; l = l->next)
412 CellInfo *info = l->data;
414 /* A new group starts with any aligned cell, the first group is implied */
415 if (info->align && l != cells)
417 group = cell_group_new (id++);
418 groups = g_list_prepend (groups, group);
421 group->cells = g_list_prepend (group->cells, info);
424 /* A group expands if it contains any expand cells */
426 group->expand_cells ++;
431 for (l = cells; l; l = l->next)
434 group->cells = g_list_reverse (group->cells);
437 return g_list_reverse (groups);
441 count_expand_groups (GtkCellAreaBox *box)
443 GtkCellAreaBoxPrivate *priv = box->priv;
445 gint expand_groups = 0;
447 for (l = priv->groups; l; l = l->next)
449 CellGroup *group = l->data;
451 if (group->expand_cells > 0)
455 return expand_groups;
459 iter_weak_notify (GtkCellAreaBox *box,
460 GtkCellAreaBoxIter *dead_iter)
462 GtkCellAreaBoxPrivate *priv = box->priv;
464 priv->iters = g_slist_remove (priv->iters, dead_iter);
468 init_iter_group (GtkCellAreaBox *box,
469 GtkCellAreaBoxIter *iter)
471 GtkCellAreaBoxPrivate *priv = box->priv;
472 gint n_groups, *expand_groups, i;
475 n_groups = g_list_length (priv->groups);
476 expand_groups = g_new (gboolean, n_groups);
478 for (i = 0, l = priv->groups; l; l = l->next, i++)
480 CellGroup *group = l->data;
482 expand_groups[i] = (group->expand_cells > 0);
485 gtk_cell_area_box_init_groups (iter, n_groups, expand_groups);
486 g_free (expand_groups);
490 init_iter_groups (GtkCellAreaBox *box)
492 GtkCellAreaBoxPrivate *priv = box->priv;
495 /* When the box's groups are reconstructed, iters need to
498 for (l = priv->iters; l; l = l->next)
500 GtkCellAreaBoxIter *iter = l->data;
502 init_iter_group (box, iter);
507 flush_iters (GtkCellAreaBox *box)
509 GtkCellAreaBoxPrivate *priv = box->priv;
512 /* When the box layout changes, iters need to
513 * be flushed and sizes for the box get requested again
515 for (l = priv->iters; l; l = l->next)
517 GtkCellAreaIter *iter = l->data;
519 gtk_cell_area_iter_flush (iter);
523 /* Returns an allocation for each cell in the orientation of the box,
524 * used in ->render()/->event() implementations to get a straight-forward
525 * list of allocated cells to operate on.
528 get_allocated_cells (GtkCellAreaBox *box,
529 GtkCellAreaBoxIter *iter,
532 const GtkCellAreaBoxAllocation *group_allocs;
533 GtkCellArea *area = GTK_CELL_AREA (box);
534 GtkCellAreaBoxPrivate *priv = box->priv;
535 GList *group_list, *cell_list;
536 GSList *allocated_cells = NULL;
539 group_allocs = gtk_cell_area_box_iter_get_orientation_allocs (iter, &n_allocs);
542 g_warning ("Trying to operate on an unallocated GtkCellAreaIter, "
543 "GtkCellAreaBox requires that the iter be allocated at least "
544 "in the orientation of the box");
548 for (i = 0, group_list = priv->groups; group_list; i++, group_list = group_list->next)
550 CellGroup *group = group_list->data;
552 /* Exception for single cell groups */
553 if (group->n_cells == 1)
555 CellInfo *info = group->cells->data;
556 AllocatedCell *cell =
557 allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
559 allocated_cells = g_slist_prepend (allocated_cells, cell);
563 GtkRequestedSize *sizes = g_new (GtkRequestedSize, group->n_cells);
564 gint avail_size = group_allocs[i].size;
565 gint position = group_allocs[i].position;
566 gint extra_size, extra_extra;
568 for (j = 0, cell_list = group->cells; cell_list; j++, cell_list = cell_list->next)
570 CellInfo *info = cell_list->data;
572 gtk_cell_area_request_renderer (area, info->renderer,
575 &sizes[j].minimum_size,
576 &sizes[j].natural_size);
578 avail_size -= sizes[j].minimum_size;
581 /* Distribute cells naturally within the group */
582 avail_size -= (group->n_cells - 1) * priv->spacing;
583 avail_size = gtk_distribute_natural_allocation (avail_size, group->n_cells, sizes);
585 /* Calculate/distribute expand for cells */
586 if (group->expand_cells > 0)
588 extra_size = avail_size / group->expand_cells;
589 extra_extra = avail_size % group->expand_cells;
592 extra_size = extra_extra = 0;
594 /* Create the allocated cells */
595 for (j = 0, cell_list = group->cells; cell_list; j++, cell_list = cell_list->next)
597 CellInfo *info = cell_list->data;
602 sizes[j].minimum_size += extra_size;
605 sizes[j].minimum_size++;
610 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
612 allocated_cells = g_slist_prepend (allocated_cells, cell);
614 position += sizes[j].minimum_size;
615 position += priv->spacing;
622 return g_slist_reverse (allocated_cells);
625 /*************************************************************
627 *************************************************************/
629 gtk_cell_area_box_finalize (GObject *object)
631 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
632 GtkCellAreaBoxPrivate *priv = box->priv;
635 for (l = priv->iters; l; l = l->next)
636 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)iter_weak_notify, box);
638 g_slist_free (priv->iters);
640 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
644 gtk_cell_area_box_dispose (GObject *object)
646 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
650 gtk_cell_area_box_set_property (GObject *object,
655 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
659 case PROP_ORIENTATION:
660 box->priv->orientation = g_value_get_enum (value);
662 /* Notify that size needs to be requested again */
666 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675 gtk_cell_area_box_get_property (GObject *object,
680 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
684 case PROP_ORIENTATION:
685 g_value_set_enum (value, box->priv->orientation);
688 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
691 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
696 /*************************************************************
698 *************************************************************/
700 gtk_cell_area_box_add (GtkCellArea *area,
701 GtkCellRenderer *renderer)
703 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
704 renderer, FALSE, TRUE);
708 gtk_cell_area_box_remove (GtkCellArea *area,
709 GtkCellRenderer *renderer)
711 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
712 GtkCellAreaBoxPrivate *priv = box->priv;
715 node = g_list_find_custom (priv->cells, renderer,
716 (GCompareFunc)cell_info_find);
720 CellInfo *info = node->data;
722 cell_info_free (info);
724 priv->cells = g_list_delete_link (priv->cells, node);
726 /* Reconstruct cell groups */
727 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
728 g_list_free (priv->groups);
729 priv->groups = construct_cell_groups (box);
731 /* Reinitialize groups on iters */
732 init_iter_groups (box);
735 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
739 gtk_cell_area_box_forall (GtkCellArea *area,
740 GtkCellCallback callback,
741 gpointer callback_data)
743 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
744 GtkCellAreaBoxPrivate *priv = box->priv;
747 for (list = priv->cells; list; list = list->next)
749 CellInfo *info = list->data;
751 callback (info->renderer, callback_data);
756 gtk_cell_area_box_event (GtkCellArea *area,
757 GtkCellAreaIter *iter,
760 const GdkRectangle *cell_area,
761 GtkCellRendererState flags)
769 gtk_cell_area_box_render (GtkCellArea *area,
770 GtkCellAreaIter *iter,
773 const GdkRectangle *cell_area,
774 GtkCellRendererState flags)
776 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
777 GtkCellAreaBoxPrivate *priv = box->priv;
778 GtkCellAreaBoxIter *box_iter = GTK_CELL_AREA_BOX_ITER (iter);
779 GSList *allocated_cells, *l;
780 GdkRectangle background_area, inner_area;
782 background_area = *cell_area;
784 /* Get a list of cells with allocation sizes decided regardless
785 * of alignments and pack order etc. */
786 allocated_cells = get_allocated_cells (box, box_iter, widget);
788 for (l = allocated_cells; l; l = l->next)
790 AllocatedCell *cell = l->data;
792 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
794 background_area.x = cell_area->x + cell->position;
795 background_area.width = cell->size;
799 background_area.y = cell_area->y + cell->position;
800 background_area.height = cell->size;
803 /* Remove margins from the background area to produce the cell area
805 gtk_cell_area_inner_cell_area (area, &background_area, &inner_area);
807 /* XXX We have to do some per-cell considerations for the 'flags'
808 * for focus handling */
809 gtk_cell_renderer_render (cell->renderer, cr, widget,
810 &background_area, &inner_area,
815 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
816 g_slist_free (allocated_cells);
820 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
821 GtkCellRenderer *renderer,
826 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
827 GtkCellAreaBoxPrivate *priv = box->priv;
830 gboolean rebuild = FALSE;
831 gboolean flush = FALSE;
833 GtkPackType pack_type;
835 node = g_list_find_custom (priv->cells, renderer,
836 (GCompareFunc)cell_info_find);
844 case CELL_PROP_EXPAND:
845 val = g_value_get_boolean (value);
847 if (info->expand != val)
854 case CELL_PROP_ALIGN:
855 val = g_value_get_boolean (value);
857 if (info->align != val)
864 case CELL_PROP_PACK_TYPE:
865 pack_type = g_value_get_enum (value);
867 if (info->pack != pack_type)
869 info->pack = pack_type;
874 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
878 /* Groups need to be rebuilt */
881 /* Reconstruct cell groups */
882 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
883 g_list_free (priv->groups);
884 priv->groups = construct_cell_groups (box);
886 /* Reinitialize groups on iters */
887 init_iter_groups (box);
896 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
897 GtkCellRenderer *renderer,
902 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
903 GtkCellAreaBoxPrivate *priv = box->priv;
907 node = g_list_find_custom (priv->cells, renderer,
908 (GCompareFunc)cell_info_find);
916 case CELL_PROP_EXPAND:
917 g_value_set_boolean (value, info->expand);
920 case CELL_PROP_ALIGN:
921 g_value_set_boolean (value, info->align);
924 case CELL_PROP_PACK_TYPE:
925 g_value_set_enum (value, info->pack);
928 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
934 static GtkCellAreaIter *
935 gtk_cell_area_box_create_iter (GtkCellArea *area)
937 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
938 GtkCellAreaBoxPrivate *priv = box->priv;
939 GtkCellAreaIter *iter =
940 (GtkCellAreaIter *)g_object_new (GTK_TYPE_CELL_AREA_BOX_ITER, NULL);
942 priv->iters = g_slist_prepend (priv->iters, iter);
944 g_object_weak_ref (G_OBJECT (iter), (GWeakNotify)iter_weak_notify, box);
946 /* Tell the new group about our cell layout */
947 init_iter_group (box, GTK_CELL_AREA_BOX_ITER (iter));
952 static GtkSizeRequestMode
953 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
955 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
956 GtkCellAreaBoxPrivate *priv = box->priv;
958 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
959 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
960 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
964 compute_size (GtkCellAreaBox *box,
965 GtkOrientation orientation,
966 GtkCellAreaBoxIter *iter,
972 GtkCellAreaBoxPrivate *priv = box->priv;
973 GtkCellArea *area = GTK_CELL_AREA (box);
976 GList *cell_list, *group_list;
980 for (group_list = priv->groups; group_list; group_list = group_list->next)
982 gint group_min_size = 0;
983 gint group_nat_size = 0;
985 group = group_list->data;
987 for (cell_list = group->cells; cell_list; cell_list = cell_list->next)
989 gint renderer_min_size, renderer_nat_size;
991 info = cell_list->data;
993 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
994 &renderer_min_size, &renderer_nat_size);
996 if (orientation == priv->orientation)
1000 min_size += priv->spacing;
1001 nat_size += priv->spacing;
1004 if (group_min_size > 0)
1006 group_min_size += priv->spacing;
1007 group_nat_size += priv->spacing;
1010 min_size += renderer_min_size;
1011 nat_size += renderer_nat_size;
1012 group_min_size += renderer_min_size;
1013 group_nat_size += renderer_nat_size;
1017 min_size = MAX (min_size, renderer_min_size);
1018 nat_size = MAX (nat_size, renderer_nat_size);
1019 group_min_size = MAX (group_min_size, renderer_min_size);
1020 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1024 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1027 gtk_cell_area_box_iter_push_group_width (iter, group->id, group_min_size, group_nat_size);
1029 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
1030 group_min_size, group_nat_size);
1035 gtk_cell_area_box_iter_push_group_height (iter, group->id, group_min_size, group_nat_size);
1037 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
1038 group_min_size, group_nat_size);
1042 *minimum_size = min_size;
1043 *natural_size = nat_size;
1047 get_group_sizes (GtkCellArea *area,
1049 GtkOrientation orientation,
1053 GtkRequestedSize *sizes;
1057 *n_sizes = g_list_length (group->cells);
1058 sizes = g_new (GtkRequestedSize, *n_sizes);
1060 for (l = group->cells, i = 0; l; l = l->next, i++)
1062 CellInfo *info = l->data;
1064 sizes[i].data = info;
1066 gtk_cell_area_request_renderer (area, info->renderer,
1067 orientation, widget, -1,
1068 &sizes[i].minimum_size,
1069 &sizes[i].natural_size);
1076 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1083 GtkCellAreaBoxPrivate *priv = box->priv;
1084 GtkCellArea *area = GTK_CELL_AREA (box);
1086 /* Exception for single cell groups */
1087 if (group->n_cells == 1)
1089 CellInfo *info = group->cells->data;
1091 gtk_cell_area_request_renderer (area, info->renderer,
1092 OPPOSITE_ORIENTATION (priv->orientation),
1093 widget, for_size, minimum_size, natural_size);
1097 GtkRequestedSize *orientation_sizes;
1100 gint avail_size = for_size;
1101 gint extra_size, extra_extra;
1102 gint min_size = 0, nat_size = 0;
1104 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1106 /* First naturally allocate the cells in the group into the for_size */
1107 avail_size -= (n_sizes - 1) * priv->spacing;
1108 for (i = 0; i < n_sizes; i++)
1109 avail_size -= orientation_sizes[i].minimum_size;
1111 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1113 /* Calculate/distribute expand for cells */
1114 if (group->expand_cells > 0)
1116 extra_size = avail_size / group->expand_cells;
1117 extra_extra = avail_size % group->expand_cells;
1120 extra_size = extra_extra = 0;
1122 for (i = 0; i < n_sizes; i++)
1124 gint cell_min, cell_nat;
1126 info = orientation_sizes[i].data;
1130 orientation_sizes[i].minimum_size += extra_size;
1133 orientation_sizes[i].minimum_size++;
1138 gtk_cell_area_request_renderer (area, info->renderer,
1139 OPPOSITE_ORIENTATION (priv->orientation),
1141 orientation_sizes[i].minimum_size,
1142 &cell_min, &cell_nat);
1144 min_size = MAX (min_size, cell_min);
1145 nat_size = MAX (nat_size, cell_nat);
1148 *minimum_size = min_size;
1149 *natural_size = nat_size;
1151 g_free (orientation_sizes);
1156 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1157 GtkCellAreaBoxIter *iter,
1163 GtkCellAreaBoxPrivate *priv = box->priv;
1166 GtkRequestedSize *orientation_sizes;
1167 gint n_groups, n_expand_groups, i;
1168 gint avail_size = for_size;
1169 gint extra_size, extra_extra;
1170 gint min_size = 0, nat_size = 0;
1172 n_expand_groups = count_expand_groups (box);
1174 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1175 orientation_sizes = gtk_cell_area_box_iter_get_widths (iter, &n_groups);
1177 orientation_sizes = gtk_cell_area_box_iter_get_heights (iter, &n_groups);
1179 /* First start by naturally allocating space among groups of cells */
1180 avail_size -= (n_groups - 1) * priv->spacing;
1181 for (i = 0; i < n_groups; i++)
1182 avail_size -= orientation_sizes[i].minimum_size;
1184 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1186 /* Calculate/distribute expand for groups */
1187 if (n_expand_groups > 0)
1189 extra_size = avail_size / n_expand_groups;
1190 extra_extra = avail_size % n_expand_groups;
1193 extra_size = extra_extra = 0;
1195 /* Now we need to naturally allocate sizes for cells in each group
1196 * and push the height-for-width for each group accordingly while accumulating
1197 * the overall height-for-width for this row.
1199 for (group_list = priv->groups; group_list; group_list = group_list->next)
1201 gint group_min, group_nat;
1203 group = group_list->data;
1205 if (group->expand_cells > 0)
1207 orientation_sizes[group->id].minimum_size += extra_size;
1210 orientation_sizes[group->id].minimum_size++;
1215 /* Now we have the allocation for the group, request it's height-for-width */
1216 compute_group_size_for_opposing_orientation (box, group, widget,
1217 orientation_sizes[group->id].minimum_size,
1218 &group_min, &group_nat);
1220 min_size = MAX (min_size, group_min);
1221 nat_size = MAX (nat_size, group_nat);
1223 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1225 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
1226 group_min, group_nat);
1230 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
1231 group_min, group_nat);
1235 *minimum_size = min_size;
1236 *natural_size = nat_size;
1238 g_free (orientation_sizes);
1244 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1245 GtkCellAreaIter *iter,
1247 gint *minimum_width,
1248 gint *natural_width)
1250 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1251 GtkCellAreaBoxIter *box_iter;
1252 gint min_width, nat_width;
1254 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1256 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1258 /* Compute the size of all renderers for current row data,
1259 * bumping cell alignments in the iter along the way */
1260 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1261 box_iter, widget, -1, &min_width, &nat_width);
1264 *minimum_width = min_width;
1267 *natural_width = nat_width;
1271 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1272 GtkCellAreaIter *iter,
1274 gint *minimum_height,
1275 gint *natural_height)
1277 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1278 GtkCellAreaBoxIter *box_iter;
1279 gint min_height, nat_height;
1281 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1283 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1285 /* Compute the size of all renderers for current row data,
1286 * bumping cell alignments in the iter along the way */
1287 compute_size (box, GTK_ORIENTATION_VERTICAL,
1288 box_iter, widget, -1, &min_height, &nat_height);
1291 *minimum_height = min_height;
1294 *natural_height = nat_height;
1298 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1299 GtkCellAreaIter *iter,
1302 gint *minimum_height,
1303 gint *natural_height)
1305 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1306 GtkCellAreaBoxIter *box_iter;
1307 GtkCellAreaBoxPrivate *priv;
1308 gint min_height, nat_height;
1310 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1312 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1315 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1317 /* Add up vertical requests of height for width and push the overall
1318 * cached sizes for alignments */
1319 compute_size (box, priv->orientation, box_iter, widget, width, &min_height, &nat_height);
1323 /* Juice: virtually allocate cells into the for_width using the
1324 * alignments and then return the overall height for that width, and cache it */
1325 compute_size_for_opposing_orientation (box, box_iter, widget, width, &min_height, &nat_height);
1329 *minimum_height = min_height;
1332 *natural_height = nat_height;
1336 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1337 GtkCellAreaIter *iter,
1340 gint *minimum_width,
1341 gint *natural_width)
1343 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1344 GtkCellAreaBoxIter *box_iter;
1345 GtkCellAreaBoxPrivate *priv;
1346 gint min_width, nat_width;
1348 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1350 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1353 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1355 /* Add up horizontal requests of width for height and push the overall
1356 * cached sizes for alignments */
1357 compute_size (box, priv->orientation, box_iter, widget, height, &min_width, &nat_width);
1361 /* Juice: horizontally allocate cells into the for_height using the
1362 * alignments and then return the overall width for that height, and cache it */
1363 compute_size_for_opposing_orientation (box, box_iter, widget, height, &min_width, &nat_width);
1367 *minimum_width = min_width;
1370 *natural_width = nat_width;
1374 /*************************************************************
1375 * GtkCellLayoutIface *
1376 *************************************************************/
1378 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1380 iface->pack_start = gtk_cell_area_box_layout_pack_start;
1381 iface->pack_end = gtk_cell_area_box_layout_pack_end;
1382 iface->reorder = gtk_cell_area_box_layout_reorder;
1386 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
1387 GtkCellRenderer *renderer,
1390 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1394 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
1395 GtkCellRenderer *renderer,
1398 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1402 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
1403 GtkCellRenderer *renderer,
1406 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
1407 GtkCellAreaBoxPrivate *priv = box->priv;
1411 node = g_list_find_custom (priv->cells, renderer,
1412 (GCompareFunc)cell_info_find);
1418 priv->cells = g_list_delete_link (priv->cells, node);
1419 priv->cells = g_list_insert (priv->cells, info, position);
1421 /* Reconstruct cell groups */
1422 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1423 g_list_free (priv->groups);
1424 priv->groups = construct_cell_groups (box);
1426 /* Reinitialize groups on iters */
1427 init_iter_groups (box);
1431 /*************************************************************
1433 *************************************************************/
1435 gtk_cell_area_box_new (void)
1437 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
1441 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
1442 GtkCellRenderer *renderer,
1446 GtkCellAreaBoxPrivate *priv;
1449 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1450 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1454 if (g_list_find_custom (priv->cells, renderer,
1455 (GCompareFunc)cell_info_find))
1457 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
1461 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
1463 priv->cells = g_list_append (priv->cells, info);
1465 /* Reconstruct cell groups */
1466 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1467 g_list_free (priv->groups);
1468 priv->groups = construct_cell_groups (box);
1470 /* Reinitialize groups on iters */
1471 init_iter_groups (box);
1475 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
1476 GtkCellRenderer *renderer,
1480 GtkCellAreaBoxPrivate *priv;
1483 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1484 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1488 if (g_list_find_custom (priv->cells, renderer,
1489 (GCompareFunc)cell_info_find))
1491 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
1495 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
1497 priv->cells = g_list_append (priv->cells, info);
1499 /* Reconstruct cell groups */
1500 g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
1501 g_list_free (priv->groups);
1502 priv->groups = construct_cell_groups (box);
1504 /* Reinitialize groups on iters */
1505 init_iter_groups (box);
1509 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
1511 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
1513 return box->priv->spacing;
1517 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
1520 GtkCellAreaBoxPrivate *priv;
1522 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1526 if (priv->spacing != spacing)
1528 priv->spacing = spacing;
1530 g_object_notify (G_OBJECT (box), "spacing");
1532 /* Notify that size needs to be requested again */