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 void gtk_cell_area_box_get_cell_allocation (GtkCellArea *area,
54 GtkCellAreaIter *iter,
56 GtkCellRenderer *renderer,
57 const GdkRectangle *cell_area,
58 GdkRectangle *allocation);
59 static gint gtk_cell_area_box_event (GtkCellArea *area,
60 GtkCellAreaIter *iter,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
65 static void gtk_cell_area_box_render (GtkCellArea *area,
66 GtkCellAreaIter *iter,
69 const GdkRectangle *cell_area,
70 GtkCellRendererState flags);
71 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
72 GtkCellRenderer *renderer,
76 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
77 GtkCellRenderer *renderer,
81 static GtkCellAreaIter *gtk_cell_area_box_create_iter (GtkCellArea *area);
82 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
83 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
84 GtkCellAreaIter *iter,
88 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
89 GtkCellAreaIter *iter,
92 gint *natural_height);
93 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
94 GtkCellAreaIter *iter,
98 gint *natural_height);
99 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
100 GtkCellAreaIter *iter,
104 gint *natural_width);
105 static gboolean gtk_cell_area_box_focus (GtkCellArea *area,
106 GtkDirectionType direction);
108 /* GtkCellLayoutIface */
109 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
110 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
111 GtkCellRenderer *renderer,
113 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
114 GtkCellRenderer *renderer,
116 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
117 GtkCellRenderer *renderer,
121 /* CellInfo/CellGroup metadata handling and convenience functions */
123 GtkCellRenderer *renderer;
125 guint expand : 1; /* Whether the cell expands */
126 guint pack : 1; /* Whether the cell is packed from the start or end */
127 guint align : 1; /* Whether to align this cell's position with adjacent rows */
135 guint expand_cells : 8;
139 GtkCellRenderer *renderer;
145 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
149 static void cell_info_free (CellInfo *info);
150 static gint cell_info_find (CellInfo *info,
151 GtkCellRenderer *renderer);
153 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
156 static void allocated_cell_free (AllocatedCell *cell);
157 static GList *list_consecutive_cells (GtkCellAreaBox *box);
158 static gint count_expand_groups (GtkCellAreaBox *box);
159 static void iter_weak_notify (GtkCellAreaBox *box,
160 GtkCellAreaBoxIter *dead_iter);
161 static void flush_iters (GtkCellAreaBox *box);
162 static void init_iter_groups (GtkCellAreaBox *box);
163 static void init_iter_group (GtkCellAreaBox *box,
164 GtkCellAreaBoxIter *iter);
165 static GSList *get_allocated_cells (GtkCellAreaBox *box,
166 GtkCellAreaBoxIter *iter,
170 struct _GtkCellAreaBoxPrivate
172 GtkOrientation orientation;
195 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
196 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
197 gtk_cell_area_box_cell_layout_init)
198 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
200 #define OPPOSITE_ORIENTATION(orientation) \
201 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
202 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
205 gtk_cell_area_box_init (GtkCellAreaBox *box)
207 GtkCellAreaBoxPrivate *priv;
209 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
210 GTK_TYPE_CELL_AREA_BOX,
211 GtkCellAreaBoxPrivate);
214 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
215 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
222 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
224 GObjectClass *object_class = G_OBJECT_CLASS (class);
225 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
228 object_class->finalize = gtk_cell_area_box_finalize;
229 object_class->dispose = gtk_cell_area_box_dispose;
230 object_class->set_property = gtk_cell_area_box_set_property;
231 object_class->get_property = gtk_cell_area_box_get_property;
233 /* GtkCellAreaClass */
234 area_class->add = gtk_cell_area_box_add;
235 area_class->remove = gtk_cell_area_box_remove;
236 area_class->forall = gtk_cell_area_box_forall;
237 area_class->get_cell_allocation = gtk_cell_area_box_get_cell_allocation;
238 area_class->event = gtk_cell_area_box_event;
239 area_class->render = gtk_cell_area_box_render;
240 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
241 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
243 area_class->create_iter = gtk_cell_area_box_create_iter;
244 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
245 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
246 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
247 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
248 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
250 area_class->focus = gtk_cell_area_box_focus;
253 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
255 g_object_class_install_property (object_class,
257 g_param_spec_int ("spacing",
259 P_("Space which is inserted between cells"),
263 GTK_PARAM_READWRITE));
265 /* Cell Properties */
266 gtk_cell_area_class_install_cell_property (area_class,
271 P_("Whether the cell expands"),
273 GTK_PARAM_READWRITE));
275 gtk_cell_area_class_install_cell_property (area_class,
280 P_("Whether cell should align with adjacent rows"),
282 GTK_PARAM_READWRITE));
284 gtk_cell_area_class_install_cell_property (area_class,
289 P_("A GtkPackType indicating whether the cell is packed with "
290 "reference to the start or end of the cell area"),
291 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
292 GTK_PARAM_READWRITE));
294 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
298 /*************************************************************
299 * CellInfo/CellGroup basics and convenience functions *
300 *************************************************************/
302 cell_info_new (GtkCellRenderer *renderer,
307 CellInfo *info = g_slice_new (CellInfo);
309 info->renderer = g_object_ref_sink (renderer);
311 info->expand = expand;
318 cell_info_free (CellInfo *info)
320 g_object_unref (info->renderer);
322 g_slice_free (CellInfo, info);
326 cell_info_find (CellInfo *info,
327 GtkCellRenderer *renderer)
329 return (info->renderer == renderer) ? 0 : -1;
332 static AllocatedCell *
333 allocated_cell_new (GtkCellRenderer *renderer,
337 AllocatedCell *cell = g_slice_new (AllocatedCell);
339 cell->renderer = renderer;
340 cell->position = position;
347 allocated_cell_free (AllocatedCell *cell)
349 g_slice_free (AllocatedCell, cell);
353 list_consecutive_cells (GtkCellAreaBox *box)
355 GtkCellAreaBoxPrivate *priv = box->priv;
356 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
359 /* List cells in consecutive order taking their
360 * PACK_START/PACK_END options into account
362 for (l = priv->cells; l; l = l->next)
366 if (info->pack == GTK_PACK_START)
367 consecutive_cells = g_list_prepend (consecutive_cells, info);
370 for (l = priv->cells; l; l = l->next)
374 if (info->pack == GTK_PACK_END)
375 pack_end_cells = g_list_prepend (pack_end_cells, info);
378 consecutive_cells = g_list_reverse (consecutive_cells);
379 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
381 return consecutive_cells;
385 cell_groups_clear (GtkCellAreaBox *box)
387 GtkCellAreaBoxPrivate *priv = box->priv;
390 for (i = 0; i < priv->groups->len; i++)
392 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
394 g_list_free (group->cells);
397 g_array_set_size (priv->groups, 0);
401 cell_groups_rebuild (GtkCellAreaBox *box)
403 GtkCellAreaBoxPrivate *priv = box->priv;
404 CellGroup group = { 0, };
405 CellGroup *group_ptr;
409 cell_groups_clear (box);
414 cells = list_consecutive_cells (box);
416 /* First group is implied */
417 g_array_append_val (priv->groups, group);
418 group_ptr = &g_array_index (priv->groups, CellGroup, id);
420 for (l = cells; l; l = l->next)
422 CellInfo *info = l->data;
424 /* A new group starts with any aligned cell, the first group is implied */
425 if (info->align && l != cells)
427 memset (&group, 0x0, sizeof (CellGroup));
430 g_array_append_val (priv->groups, group);
431 group_ptr = &g_array_index (priv->groups, CellGroup, id);
434 group_ptr->cells = g_list_prepend (group_ptr->cells, info);
435 group_ptr->n_cells++;
437 /* A group expands if it contains any expand cells */
439 group_ptr->expand_cells++;
444 for (id = 0; id < priv->groups->len; id++)
446 group_ptr = &g_array_index (priv->groups, CellGroup, id);
448 group_ptr->cells = g_list_reverse (group_ptr->cells);
451 /* Iters need to be updated with the new grouping information */
452 init_iter_groups (box);
456 count_visible_cells (CellGroup *group,
460 gint visible_cells = 0;
461 gint n_expand_cells = 0;
463 for (l = group->cells; l; l = l->next)
465 CellInfo *info = l->data;
467 if (gtk_cell_renderer_get_visible (info->renderer))
477 *expand_cells = n_expand_cells;
479 return visible_cells;
483 count_expand_groups (GtkCellAreaBox *box)
485 GtkCellAreaBoxPrivate *priv = box->priv;
487 gint expand_groups = 0;
489 for (i = 0; i < priv->groups->len; i++)
491 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
493 if (group->expand_cells > 0)
497 return expand_groups;
501 iter_weak_notify (GtkCellAreaBox *box,
502 GtkCellAreaBoxIter *dead_iter)
504 GtkCellAreaBoxPrivate *priv = box->priv;
506 priv->iters = g_slist_remove (priv->iters, dead_iter);
510 init_iter_group (GtkCellAreaBox *box,
511 GtkCellAreaBoxIter *iter)
513 GtkCellAreaBoxPrivate *priv = box->priv;
514 gint *expand_groups, i;
516 expand_groups = g_new (gboolean, priv->groups->len);
518 for (i = 0; i < priv->groups->len; i++)
520 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
522 expand_groups[i] = (group->expand_cells > 0);
525 /* This call implies flushing the request info */
526 gtk_cell_area_box_init_groups (iter, priv->groups->len, expand_groups);
527 g_free (expand_groups);
531 init_iter_groups (GtkCellAreaBox *box)
533 GtkCellAreaBoxPrivate *priv = box->priv;
536 /* When the box's groups are reconstructed, iters need to
539 for (l = priv->iters; l; l = l->next)
541 GtkCellAreaBoxIter *iter = l->data;
543 init_iter_group (box, iter);
548 flush_iters (GtkCellAreaBox *box)
550 GtkCellAreaBoxPrivate *priv = box->priv;
553 /* When the box layout changes, iters need to
554 * be flushed and sizes for the box get requested again
556 for (l = priv->iters; l; l = l->next)
558 GtkCellAreaIter *iter = l->data;
560 gtk_cell_area_iter_flush (iter);
564 /* Returns an allocation for each cell in the orientation of the box,
565 * used in ->render()/->event() implementations to get a straight-forward
566 * list of allocated cells to operate on.
569 get_allocated_cells (GtkCellAreaBox *box,
570 GtkCellAreaBoxIter *iter,
573 const GtkCellAreaBoxAllocation *group_allocs;
574 GtkCellArea *area = GTK_CELL_AREA (box);
575 GtkCellAreaBoxPrivate *priv = box->priv;
577 GSList *allocated_cells = NULL;
580 group_allocs = gtk_cell_area_box_iter_get_orientation_allocs (iter, &n_allocs);
583 g_warning ("Trying to operate on an unallocated GtkCellAreaIter, "
584 "GtkCellAreaBox requires that the iter be allocated at least "
585 "in the orientation of the box");
589 for (i = 0; i < n_allocs; i++)
591 /* We dont always allocate all groups, sometimes the requested group has only invisible
592 * cells for every row, hence the usage of group_allocs[i].group_idx here
594 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
596 /* Exception for single cell groups */
597 if (group->n_cells == 1)
599 CellInfo *info = group->cells->data;
600 AllocatedCell *cell =
601 allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
603 allocated_cells = g_slist_prepend (allocated_cells, cell);
607 GtkRequestedSize *sizes;
608 gint avail_size, position;
609 gint visible_cells, expand_cells;
610 gint extra_size, extra_extra;
612 visible_cells = count_visible_cells (group, &expand_cells);
614 /* If this row has no visible cells in this group, just
615 * skip the allocation */
616 if (visible_cells == 0)
619 /* Offset the allocation to the group position and allocate into
620 * the group's available size */
621 position = group_allocs[i].position;
622 avail_size = group_allocs[i].size;
624 sizes = g_new (GtkRequestedSize, visible_cells);
626 for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
628 CellInfo *info = cell_list->data;
630 if (!gtk_cell_renderer_get_visible (info->renderer))
633 gtk_cell_area_request_renderer (area, info->renderer,
636 &sizes[j].minimum_size,
637 &sizes[j].natural_size);
639 sizes[j].data = info;
640 avail_size -= sizes[j].minimum_size;
645 /* Distribute cells naturally within the group */
646 avail_size -= (visible_cells - 1) * priv->spacing;
647 avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
649 /* Calculate/distribute expand for cells */
650 if (expand_cells > 0)
652 extra_size = avail_size / expand_cells;
653 extra_extra = avail_size % expand_cells;
656 extra_size = extra_extra = 0;
658 /* Create the allocated cells (loop only over visible cells here) */
659 for (j = 0; j < visible_cells; j++)
661 CellInfo *info = sizes[j].data;
666 sizes[j].minimum_size += extra_size;
669 sizes[j].minimum_size++;
674 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
676 allocated_cells = g_slist_prepend (allocated_cells, cell);
678 position += sizes[j].minimum_size;
679 position += priv->spacing;
686 /* Note it might not be important to reverse the list here at all,
687 * we have the correct positions, no need to allocate from left to right */
688 return g_slist_reverse (allocated_cells);
691 /*************************************************************
693 *************************************************************/
695 gtk_cell_area_box_finalize (GObject *object)
697 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
698 GtkCellAreaBoxPrivate *priv = box->priv;
701 /* Unref/free the iter list */
702 for (l = priv->iters; l; l = l->next)
703 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)iter_weak_notify, box);
705 g_slist_free (priv->iters);
708 /* Free the cell grouping info */
709 cell_groups_clear (box);
710 g_array_free (priv->groups, TRUE);
712 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
716 gtk_cell_area_box_dispose (GObject *object)
718 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
722 gtk_cell_area_box_set_property (GObject *object,
727 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
731 case PROP_ORIENTATION:
732 box->priv->orientation = g_value_get_enum (value);
734 /* Notify that size needs to be requested again */
738 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
741 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
747 gtk_cell_area_box_get_property (GObject *object,
752 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
756 case PROP_ORIENTATION:
757 g_value_set_enum (value, box->priv->orientation);
760 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
763 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
768 /*************************************************************
770 *************************************************************/
772 gtk_cell_area_box_add (GtkCellArea *area,
773 GtkCellRenderer *renderer)
775 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
776 renderer, FALSE, TRUE);
780 gtk_cell_area_box_remove (GtkCellArea *area,
781 GtkCellRenderer *renderer)
783 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
784 GtkCellAreaBoxPrivate *priv = box->priv;
787 node = g_list_find_custom (priv->cells, renderer,
788 (GCompareFunc)cell_info_find);
792 CellInfo *info = node->data;
794 cell_info_free (info);
796 priv->cells = g_list_delete_link (priv->cells, node);
798 /* Reconstruct cell groups */
799 cell_groups_rebuild (box);
802 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
806 gtk_cell_area_box_forall (GtkCellArea *area,
807 GtkCellCallback callback,
808 gpointer callback_data)
810 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
811 GtkCellAreaBoxPrivate *priv = box->priv;
814 for (list = priv->cells; list; list = list->next)
816 CellInfo *info = list->data;
818 callback (info->renderer, callback_data);
823 gtk_cell_area_box_get_cell_allocation (GtkCellArea *area,
824 GtkCellAreaIter *iter,
826 GtkCellRenderer *renderer,
827 const GdkRectangle *cell_area,
828 GdkRectangle *allocation)
830 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
831 GtkCellAreaBoxPrivate *priv = box->priv;
832 GtkCellAreaBoxIter *box_iter = GTK_CELL_AREA_BOX_ITER (iter);
833 GSList *allocated_cells, *l;
835 *allocation = *cell_area;
837 /* Get a list of cells with allocation sizes decided regardless
838 * of alignments and pack order etc. */
839 allocated_cells = get_allocated_cells (box, box_iter, widget);
841 for (l = allocated_cells; l; l = l->next)
843 AllocatedCell *cell = l->data;
845 if (cell->renderer == renderer)
847 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
849 allocation->x = cell_area->x + cell->position;
850 allocation->width = cell->size;
854 allocation->y = cell_area->y + cell->position;
855 allocation->height = cell->size;
862 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
863 g_slist_free (allocated_cells);
873 gtk_cell_area_box_event (GtkCellArea *area,
874 GtkCellAreaIter *iter,
877 const GdkRectangle *cell_area,
878 GtkCellRendererState flags)
882 /* First let the parent class handle activation of cells via keystrokes */
884 GTK_CELL_AREA_CLASS (gtk_cell_area_box_parent_class)->event (area, iter, widget,
885 event, cell_area, flags);
890 /* Also detect mouse events, for mouse events we need to allocate the renderers
891 * and find which renderer needs to be activated.
899 gtk_cell_area_box_render (GtkCellArea *area,
900 GtkCellAreaIter *iter,
903 const GdkRectangle *cell_area,
904 GtkCellRendererState flags)
906 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
907 GtkCellAreaBoxPrivate *priv = box->priv;
908 GtkCellAreaBoxIter *box_iter = GTK_CELL_AREA_BOX_ITER (iter);
909 GSList *allocated_cells, *l;
910 GdkRectangle background_area, inner_area;
911 GtkCellRenderer *focus_cell = NULL;
913 if (flags & GTK_CELL_RENDERER_FOCUSED)
915 focus_cell = gtk_cell_area_get_focus_cell (area);
916 flags &= ~GTK_CELL_RENDERER_FOCUSED;
919 background_area = *cell_area;
921 /* Get a list of cells with allocation sizes decided regardless
922 * of alignments and pack order etc. */
923 allocated_cells = get_allocated_cells (box, box_iter, widget);
925 for (l = allocated_cells; l; l = l->next)
927 AllocatedCell *cell = l->data;
928 GtkCellRendererState cell_fields = 0;
930 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
932 background_area.x = cell_area->x + cell->position;
933 background_area.width = cell->size;
937 background_area.y = cell_area->y + cell->position;
938 background_area.height = cell->size;
941 if (cell->renderer == focus_cell)
943 g_print ("Rendering a cell with the focus flag !\n");
945 cell_fields |= GTK_CELL_RENDERER_FOCUSED;
948 /* Remove margins from the background area to produce the cell area
950 gtk_cell_area_inner_cell_area (area, &background_area, &inner_area);
952 /* We have to do some per-cell considerations for the 'flags'
953 * for focus handling */
954 gtk_cell_renderer_render (cell->renderer, cr, widget,
955 &background_area, &inner_area,
956 flags | cell_fields);
959 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
960 g_slist_free (allocated_cells);
964 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
965 GtkCellRenderer *renderer,
970 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
971 GtkCellAreaBoxPrivate *priv = box->priv;
974 gboolean rebuild = FALSE;
976 GtkPackType pack_type;
978 node = g_list_find_custom (priv->cells, renderer,
979 (GCompareFunc)cell_info_find);
987 case CELL_PROP_EXPAND:
988 val = g_value_get_boolean (value);
990 if (info->expand != val)
997 case CELL_PROP_ALIGN:
998 val = g_value_get_boolean (value);
1000 if (info->align != val)
1007 case CELL_PROP_PACK_TYPE:
1008 pack_type = g_value_get_enum (value);
1010 if (info->pack != pack_type)
1012 info->pack = pack_type;
1017 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
1021 /* Groups need to be rebuilt */
1023 cell_groups_rebuild (box);
1027 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
1028 GtkCellRenderer *renderer,
1033 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1034 GtkCellAreaBoxPrivate *priv = box->priv;
1038 node = g_list_find_custom (priv->cells, renderer,
1039 (GCompareFunc)cell_info_find);
1047 case CELL_PROP_EXPAND:
1048 g_value_set_boolean (value, info->expand);
1051 case CELL_PROP_ALIGN:
1052 g_value_set_boolean (value, info->align);
1055 case CELL_PROP_PACK_TYPE:
1056 g_value_set_enum (value, info->pack);
1059 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
1065 static GtkCellAreaIter *
1066 gtk_cell_area_box_create_iter (GtkCellArea *area)
1068 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1069 GtkCellAreaBoxPrivate *priv = box->priv;
1070 GtkCellAreaIter *iter =
1071 (GtkCellAreaIter *)g_object_new (GTK_TYPE_CELL_AREA_BOX_ITER,
1072 "area", area, NULL);
1074 priv->iters = g_slist_prepend (priv->iters, iter);
1076 g_object_weak_ref (G_OBJECT (iter), (GWeakNotify)iter_weak_notify, box);
1078 /* Tell the new group about our cell layout */
1079 init_iter_group (box, GTK_CELL_AREA_BOX_ITER (iter));
1084 static GtkSizeRequestMode
1085 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1087 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1088 GtkCellAreaBoxPrivate *priv = box->priv;
1090 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1091 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1092 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1096 compute_size (GtkCellAreaBox *box,
1097 GtkOrientation orientation,
1098 GtkCellAreaBoxIter *iter,
1104 GtkCellAreaBoxPrivate *priv = box->priv;
1105 GtkCellArea *area = GTK_CELL_AREA (box);
1111 for (i = 0; i < priv->groups->len; i++)
1113 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1114 gint group_min_size = 0;
1115 gint group_nat_size = 0;
1117 for (list = group->cells; list; list = list->next)
1119 CellInfo *info = list->data;
1120 gint renderer_min_size, renderer_nat_size;
1122 if (!gtk_cell_renderer_get_visible (info->renderer))
1125 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1126 &renderer_min_size, &renderer_nat_size);
1128 if (orientation == priv->orientation)
1132 min_size += priv->spacing;
1133 nat_size += priv->spacing;
1136 if (group_min_size > 0)
1138 group_min_size += priv->spacing;
1139 group_nat_size += priv->spacing;
1142 min_size += renderer_min_size;
1143 nat_size += renderer_nat_size;
1144 group_min_size += renderer_min_size;
1145 group_nat_size += renderer_nat_size;
1149 min_size = MAX (min_size, renderer_min_size);
1150 nat_size = MAX (nat_size, renderer_nat_size);
1151 group_min_size = MAX (group_min_size, renderer_min_size);
1152 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1156 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1159 gtk_cell_area_box_iter_push_group_width (iter, group->id, group_min_size, group_nat_size);
1161 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
1162 group_min_size, group_nat_size);
1167 gtk_cell_area_box_iter_push_group_height (iter, group->id, group_min_size, group_nat_size);
1169 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
1170 group_min_size, group_nat_size);
1174 *minimum_size = min_size;
1175 *natural_size = nat_size;
1179 get_group_sizes (GtkCellArea *area,
1181 GtkOrientation orientation,
1185 GtkRequestedSize *sizes;
1189 *n_sizes = count_visible_cells (group, NULL);
1190 sizes = g_new (GtkRequestedSize, *n_sizes);
1192 for (l = group->cells, i = 0; l; l = l->next)
1194 CellInfo *info = l->data;
1196 if (!gtk_cell_renderer_get_visible (info->renderer))
1199 sizes[i].data = info;
1201 gtk_cell_area_request_renderer (area, info->renderer,
1202 orientation, widget, -1,
1203 &sizes[i].minimum_size,
1204 &sizes[i].natural_size);
1213 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1220 GtkCellAreaBoxPrivate *priv = box->priv;
1221 GtkCellArea *area = GTK_CELL_AREA (box);
1223 /* Exception for single cell groups */
1224 if (group->n_cells == 1)
1226 CellInfo *info = group->cells->data;
1228 gtk_cell_area_request_renderer (area, info->renderer,
1229 OPPOSITE_ORIENTATION (priv->orientation),
1230 widget, for_size, minimum_size, natural_size);
1234 GtkRequestedSize *orientation_sizes;
1237 gint avail_size = for_size;
1238 gint extra_size, extra_extra;
1239 gint min_size = 0, nat_size = 0;
1241 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1243 /* First naturally allocate the cells in the group into the for_size */
1244 avail_size -= (n_sizes - 1) * priv->spacing;
1245 for (i = 0; i < n_sizes; i++)
1246 avail_size -= orientation_sizes[i].minimum_size;
1248 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1250 /* Calculate/distribute expand for cells */
1251 if (group->expand_cells > 0)
1253 extra_size = avail_size / group->expand_cells;
1254 extra_extra = avail_size % group->expand_cells;
1257 extra_size = extra_extra = 0;
1259 for (i = 0; i < n_sizes; i++)
1261 gint cell_min, cell_nat;
1263 info = orientation_sizes[i].data;
1267 orientation_sizes[i].minimum_size += extra_size;
1270 orientation_sizes[i].minimum_size++;
1275 gtk_cell_area_request_renderer (area, info->renderer,
1276 OPPOSITE_ORIENTATION (priv->orientation),
1278 orientation_sizes[i].minimum_size,
1279 &cell_min, &cell_nat);
1281 min_size = MAX (min_size, cell_min);
1282 nat_size = MAX (nat_size, cell_nat);
1285 *minimum_size = min_size;
1286 *natural_size = nat_size;
1288 g_free (orientation_sizes);
1293 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1294 GtkCellAreaBoxIter *iter,
1300 GtkCellAreaBoxPrivate *priv = box->priv;
1302 GtkRequestedSize *orientation_sizes;
1303 gint n_groups, n_expand_groups, i;
1304 gint avail_size = for_size;
1305 gint extra_size, extra_extra;
1306 gint min_size = 0, nat_size = 0;
1308 n_expand_groups = count_expand_groups (box);
1310 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1311 orientation_sizes = gtk_cell_area_box_iter_get_widths (iter, &n_groups);
1313 orientation_sizes = gtk_cell_area_box_iter_get_heights (iter, &n_groups);
1315 /* First start by naturally allocating space among groups of cells */
1316 avail_size -= (n_groups - 1) * priv->spacing;
1317 for (i = 0; i < n_groups; i++)
1318 avail_size -= orientation_sizes[i].minimum_size;
1320 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1322 /* Calculate/distribute expand for groups */
1323 if (n_expand_groups > 0)
1325 extra_size = avail_size / n_expand_groups;
1326 extra_extra = avail_size % n_expand_groups;
1329 extra_size = extra_extra = 0;
1331 /* Now we need to naturally allocate sizes for cells in each group
1332 * and push the height-for-width for each group accordingly while accumulating
1333 * the overall height-for-width for this row.
1335 for (i = 0; i < n_groups; i++)
1337 gint group_min, group_nat;
1338 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1340 group = &g_array_index (priv->groups, CellGroup, group_idx);
1342 if (group->expand_cells > 0)
1344 orientation_sizes[i].minimum_size += extra_size;
1347 orientation_sizes[i].minimum_size++;
1352 /* Now we have the allocation for the group, request it's height-for-width */
1353 compute_group_size_for_opposing_orientation (box, group, widget,
1354 orientation_sizes[i].minimum_size,
1355 &group_min, &group_nat);
1357 min_size = MAX (min_size, group_min);
1358 nat_size = MAX (nat_size, group_nat);
1360 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1362 gtk_cell_area_box_iter_push_group_height_for_width (iter, group_idx, for_size,
1363 group_min, group_nat);
1367 gtk_cell_area_box_iter_push_group_width_for_height (iter, group_idx, for_size,
1368 group_min, group_nat);
1372 *minimum_size = min_size;
1373 *natural_size = nat_size;
1375 g_free (orientation_sizes);
1381 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1382 GtkCellAreaIter *iter,
1384 gint *minimum_width,
1385 gint *natural_width)
1387 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1388 GtkCellAreaBoxIter *box_iter;
1389 gint min_width, nat_width;
1391 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1393 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1395 /* Compute the size of all renderers for current row data,
1396 * bumping cell alignments in the iter along the way */
1397 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1398 box_iter, widget, -1, &min_width, &nat_width);
1401 *minimum_width = min_width;
1404 *natural_width = nat_width;
1408 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1409 GtkCellAreaIter *iter,
1411 gint *minimum_height,
1412 gint *natural_height)
1414 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1415 GtkCellAreaBoxIter *box_iter;
1416 gint min_height, nat_height;
1418 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1420 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1422 /* Compute the size of all renderers for current row data,
1423 * bumping cell alignments in the iter along the way */
1424 compute_size (box, GTK_ORIENTATION_VERTICAL,
1425 box_iter, widget, -1, &min_height, &nat_height);
1428 *minimum_height = min_height;
1431 *natural_height = nat_height;
1435 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1436 GtkCellAreaIter *iter,
1439 gint *minimum_height,
1440 gint *natural_height)
1442 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1443 GtkCellAreaBoxIter *box_iter;
1444 GtkCellAreaBoxPrivate *priv;
1445 gint min_height, nat_height;
1447 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1449 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1452 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1454 /* Add up vertical requests of height for width and push the overall
1455 * cached sizes for alignments */
1456 compute_size (box, priv->orientation, box_iter, widget, width, &min_height, &nat_height);
1460 /* Juice: virtually allocate cells into the for_width using the
1461 * alignments and then return the overall height for that width, and cache it */
1462 compute_size_for_opposing_orientation (box, box_iter, widget, width, &min_height, &nat_height);
1466 *minimum_height = min_height;
1469 *natural_height = nat_height;
1473 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1474 GtkCellAreaIter *iter,
1477 gint *minimum_width,
1478 gint *natural_width)
1480 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1481 GtkCellAreaBoxIter *box_iter;
1482 GtkCellAreaBoxPrivate *priv;
1483 gint min_width, nat_width;
1485 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1487 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1490 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1492 /* Add up horizontal requests of width for height and push the overall
1493 * cached sizes for alignments */
1494 compute_size (box, priv->orientation, box_iter, widget, height, &min_width, &nat_width);
1498 /* Juice: horizontally allocate cells into the for_height using the
1499 * alignments and then return the overall width for that height, and cache it */
1500 compute_size_for_opposing_orientation (box, box_iter, widget, height, &min_width, &nat_width);
1504 *minimum_width = min_width;
1507 *natural_width = nat_width;
1511 gtk_cell_area_box_focus (GtkCellArea *area,
1512 GtkDirectionType direction)
1514 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1515 GtkCellAreaBoxPrivate *priv = box->priv;
1516 gint cycle = FOCUS_NONE;
1517 gboolean cycled_focus = FALSE;
1518 GtkCellRenderer *focus_cell;
1520 focus_cell = gtk_cell_area_get_focus_cell (area);
1524 case GTK_DIR_TAB_FORWARD:
1527 case GTK_DIR_TAB_BACKWARD:
1531 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1537 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1543 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1549 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1558 if (cycle != FOCUS_NONE)
1560 gboolean found_cell = FALSE;
1564 /* If there is no focused cell, focus on the first one in the list */
1568 for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
1569 i >= 0 && i < priv->groups->len;
1570 i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
1572 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1574 for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
1575 list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
1577 CellInfo *info = list->data;
1579 if (!found_cell && info->renderer == focus_cell)
1581 else if (found_cell)
1583 if (gtk_cell_renderer_can_focus (info->renderer))
1585 gtk_cell_area_set_focus_cell (area, info->renderer);
1587 cycled_focus = TRUE;
1594 return cycled_focus;
1598 /*************************************************************
1599 * GtkCellLayoutIface *
1600 *************************************************************/
1602 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1604 iface->pack_start = gtk_cell_area_box_layout_pack_start;
1605 iface->pack_end = gtk_cell_area_box_layout_pack_end;
1606 iface->reorder = gtk_cell_area_box_layout_reorder;
1610 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
1611 GtkCellRenderer *renderer,
1614 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1618 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
1619 GtkCellRenderer *renderer,
1622 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1626 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
1627 GtkCellRenderer *renderer,
1630 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
1631 GtkCellAreaBoxPrivate *priv = box->priv;
1635 node = g_list_find_custom (priv->cells, renderer,
1636 (GCompareFunc)cell_info_find);
1642 priv->cells = g_list_delete_link (priv->cells, node);
1643 priv->cells = g_list_insert (priv->cells, info, position);
1645 cell_groups_rebuild (box);
1649 /*************************************************************
1651 *************************************************************/
1653 gtk_cell_area_box_new (void)
1655 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
1659 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
1660 GtkCellRenderer *renderer,
1664 GtkCellAreaBoxPrivate *priv;
1667 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1668 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1672 if (g_list_find_custom (priv->cells, renderer,
1673 (GCompareFunc)cell_info_find))
1675 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
1679 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
1681 priv->cells = g_list_append (priv->cells, info);
1683 cell_groups_rebuild (box);
1687 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
1688 GtkCellRenderer *renderer,
1692 GtkCellAreaBoxPrivate *priv;
1695 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1696 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1700 if (g_list_find_custom (priv->cells, renderer,
1701 (GCompareFunc)cell_info_find))
1703 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
1707 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
1709 priv->cells = g_list_append (priv->cells, info);
1711 cell_groups_rebuild (box);
1715 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
1717 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
1719 return box->priv->spacing;
1723 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
1726 GtkCellAreaBoxPrivate *priv;
1728 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1732 if (priv->spacing != spacing)
1734 priv->spacing = spacing;
1736 g_object_notify (G_OBJECT (box), "spacing");
1738 /* Notify that size needs to be requested again */