+/* Returns an allocation for each cell in the orientation of the box,
+ * used in ->render()/->event() implementations to get a straight-forward
+ * list of allocated cells to operate on.
+ */
+static GSList *
+get_allocated_cells (GtkCellAreaBox *box,
+ GtkCellAreaBoxContext *context,
+ GtkWidget *widget,
+ gint width,
+ gint height)
+{
+ GtkCellAreaBoxAllocation *group_allocs;
+ GtkCellArea *area = GTK_CELL_AREA (box);
+ GtkCellAreaBoxPrivate *priv = box->priv;
+ GList *cell_list;
+ GSList *allocated_cells = NULL;
+ gint i, j, n_allocs, position;
+ gint for_size, full_size;
+ gboolean rtl;
+
+ group_allocs = _gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
+ if (!group_allocs)
+ return allocate_cells_manually (box, widget, width, height);
+
+ if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+ {
+ full_size = width;
+ for_size = height;
+ }
+ else
+ {
+ full_size = height;
+ for_size = width;
+ }
+
+ /* For vertical oriented boxes, we just let the cell renderers
+ * realign themselves for rtl
+ */
+ rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+ gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
+
+ for (position = 0, i = 0; i < n_allocs; i++)
+ {
+ /* We dont always allocate all groups, sometimes the requested
+ * group has only invisible cells for every row, hence the usage
+ * of group_allocs[i].group_idx here
+ */
+ CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
+
+ /* Exception for single cell groups */
+ if (group->n_cells == 1)
+ {
+ CellInfo *info = group->cells->data;
+ AllocatedCell *cell;
+ gint cell_position, cell_size;
+
+ if (!gtk_cell_renderer_get_visible (info->renderer))
+ continue;
+
+ /* If were not aligned, place the cell after the last cell */
+ if (info->align)
+ position = cell_position = group_allocs[i].position;
+ else
+ cell_position = position;
+
+ /* If not a fixed size, use only the requested size for this row */
+ if (info->fixed)
+ cell_size = group_allocs[i].size;
+ else
+ {
+ gint dummy;
+ gtk_cell_area_request_renderer (area, info->renderer,
+ priv->orientation,
+ widget, for_size,
+ &dummy,
+ &cell_size);
+ cell_size = MIN (cell_size, group_allocs[i].size);
+ }
+
+ if (rtl)
+ cell = allocated_cell_new (info->renderer,
+ full_size - (cell_position + cell_size), cell_size);
+ else
+ cell = allocated_cell_new (info->renderer, cell_position, cell_size);
+
+ position += cell_size;
+ position += priv->spacing;
+
+ allocated_cells = g_slist_prepend (allocated_cells, cell);
+ }
+ else
+ {
+ GtkRequestedSize *sizes;
+ gint avail_size, cell_position;
+ gint visible_cells, expand_cells;
+ gint extra_size, extra_extra;
+
+ visible_cells = count_visible_cells (group, &expand_cells);
+
+ /* If this row has no visible cells in this group, just
+ * skip the allocation
+ */
+ if (visible_cells == 0)
+ continue;
+
+ /* If were not aligned, place the cell after the last cell
+ * and eat up the extra space
+ */
+ if (group->align)
+ {
+ avail_size = group_allocs[i].size;
+ position = cell_position = group_allocs[i].position;
+ }
+ else
+ {
+ avail_size = group_allocs[i].size + (group_allocs[i].position - position);
+ cell_position = position;
+ }
+
+ sizes = g_new (GtkRequestedSize, visible_cells);
+
+ for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
+ {
+ CellInfo *info = cell_list->data;
+
+ if (!gtk_cell_renderer_get_visible (info->renderer))
+ continue;
+
+ gtk_cell_area_request_renderer (area, info->renderer,
+ priv->orientation,
+ widget, for_size,
+ &sizes[j].minimum_size,
+ &sizes[j].natural_size);
+
+ sizes[j].data = info;
+ avail_size -= sizes[j].minimum_size;
+
+ j++;
+ }
+
+ /* Distribute cells naturally within the group */
+ avail_size -= (visible_cells - 1) * priv->spacing;
+ if (avail_size > 0)
+ avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
+ else
+ avail_size = 0;
+
+ /* Calculate/distribute expand for cells */
+ if (expand_cells > 0)
+ {
+ extra_size = avail_size / expand_cells;
+ extra_extra = avail_size % expand_cells;
+ }
+ else
+ extra_size = extra_extra = 0;
+
+ /* Create the allocated cells (loop only over visible cells here) */
+ for (j = 0; j < visible_cells; j++)
+ {
+ CellInfo *info = sizes[j].data;
+ AllocatedCell *cell;
+
+ if (info->expand)
+ {
+ sizes[j].minimum_size += extra_size;
+ if (extra_extra)
+ {
+ sizes[j].minimum_size++;
+ extra_extra--;
+ }
+ }
+
+ if (rtl)
+ cell = allocated_cell_new (info->renderer,
+ full_size - (cell_position + sizes[j].minimum_size),
+ sizes[j].minimum_size);
+ else
+ cell = allocated_cell_new (info->renderer, cell_position, sizes[j].minimum_size);
+
+ allocated_cells = g_slist_prepend (allocated_cells, cell);
+
+ cell_position += sizes[j].minimum_size;
+ cell_position += priv->spacing;
+ }
+
+ g_free (sizes);
+
+ position = cell_position;
+ }
+ }
+
+ g_free (group_allocs);
+
+ /* Note it might not be important to reverse the list here at all,
+ * we have the correct positions, no need to allocate from left to right
+ */
+ return g_slist_reverse (allocated_cells);
+}
+
+
+static void
+gtk_cell_area_box_focus_changed (GtkCellArea *area,
+ GParamSpec *pspec,
+ GtkCellAreaBox *box)
+{
+ if (gtk_cell_area_get_focus_cell (area))
+ box->priv->last_focus_cell = gtk_cell_area_get_focus_cell (area);
+}