]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcellareabox.c
Added 'background_area' calculation to GtkCellAreaClass->foreach_alloc vfunc
[~andy/gtk] / gtk / gtkcellareabox.c
index becab5072dda3d53c70d2484918a8a581ad24998..2222b4b36d2092313db51f26add9e209227ba260 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+
+/**
+ * SECTION:gtkcellareabox
+ * @Short_Description: A cell area that renders #GtkCellRenderers into a row or a column
+ * @Title: GtkCellAreaBox
+ *
+ * The #GtkCellAreaBox renders cell renderers into a row or a column depending on
+ * its #GtkOrientation.
+ *
+ * GtkCellAreaBox uses a notion of <emphasis>packing</emphasis>. Packing
+ * refers to adding cell renderers with reference to a particular position 
+ * in a #GtkCellAreaBox. There are two reference positions: the
+ * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
+ * When the #GtkCellAreaBox is oriented in the %GTK_ORIENTATION_VERTICAL orientation, 
+ * the start is defined as the top of the box and the end is defined as the bottom.
+ * In the %GTK_ORIENTATION_HORIZONTAL orientation start is defined as the
+ * left side and the end is defined as the right side.
+ *
+ * Alignments of #GtkCellRenderers rendered in adjacent rows can be configured
+ * by configuring the #GtkCellAreaBox:align child cell property with
+ * gtk_cell_area_cell_set_property() or by specifying the "align" argument
+ * to gtk_cell_area_box_pack_start() and gtk_cell_area_box_pack_end().
+ */
+
 #include "config.h"
 #include "gtkintl.h"
 #include "gtkorientable.h"
 #include "gtkcelllayout.h"
 #include "gtkcellareabox.h"
-#include "gtkcellareaboxiter.h"
+#include "gtkcellareaboxcontext.h"
 #include "gtkprivate.h"
 
 
 /* GObjectClass */
-static void      gtk_cell_area_box_finalize                       (GObject            *object);
-static void      gtk_cell_area_box_dispose                        (GObject            *object);
-static void      gtk_cell_area_box_set_property                   (GObject            *object,
-                                                                  guint               prop_id,
-                                                                  const GValue       *value,
-                                                                  GParamSpec         *pspec);
-static void      gtk_cell_area_box_get_property                   (GObject            *object,
-                                                                  guint               prop_id,
-                                                                  GValue             *value,
-                                                                  GParamSpec         *pspec);
+static void      gtk_cell_area_box_finalize                       (GObject              *object);
+static void      gtk_cell_area_box_dispose                        (GObject              *object);
+static void      gtk_cell_area_box_set_property                   (GObject              *object,
+                                                                  guint                 prop_id,
+                                                                  const GValue         *value,
+                                                                  GParamSpec           *pspec);
+static void      gtk_cell_area_box_get_property                   (GObject              *object,
+                                                                  guint                 prop_id,
+                                                                  GValue               *value,
+                                                                  GParamSpec           *pspec);
 
 /* GtkCellAreaClass */
-static void      gtk_cell_area_box_add                            (GtkCellArea        *area,
-                                                                  GtkCellRenderer    *renderer);
-static void      gtk_cell_area_box_remove                         (GtkCellArea        *area,
-                                                                  GtkCellRenderer    *renderer);
-static void      gtk_cell_area_box_forall                         (GtkCellArea        *area,
-                                                                  GtkCellCallback     callback,
-                                                                  gpointer            callback_data);
-static gint      gtk_cell_area_box_event                          (GtkCellArea        *area,
-                                                                  GtkWidget          *widget,
-                                                                  GdkEvent           *event,
-                                                                  const GdkRectangle *cell_area);
-static void      gtk_cell_area_box_render                         (GtkCellArea        *area,
-                                                                  cairo_t            *cr,
-                                                                  GtkWidget          *widget,
-                                                                  const GdkRectangle *cell_area);
-
-static GtkCellAreaIter    *gtk_cell_area_box_create_iter          (GtkCellArea        *area);
-static GtkSizeRequestMode  gtk_cell_area_box_get_request_mode     (GtkCellArea        *area);
-static void      gtk_cell_area_box_get_preferred_width            (GtkCellArea        *area,
-                                                                  GtkCellAreaIter    *iter,
-                                                                  GtkWidget          *widget,
-                                                                  gint               *minimum_width,
-                                                                  gint               *natural_width);
-static void      gtk_cell_area_box_get_preferred_height           (GtkCellArea        *area,
-                                                                  GtkCellAreaIter    *iter,
-                                                                  GtkWidget          *widget,
-                                                                  gint               *minimum_height,
-                                                                  gint               *natural_height);
-static void      gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea        *area,
-                                                                  GtkCellAreaIter    *iter,
-                                                                  GtkWidget          *widget,
-                                                                  gint                width,
-                                                                  gint               *minimum_height,
-                                                                  gint               *natural_height);
-static void      gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea        *area,
-                                                                  GtkCellAreaIter    *iter,
-                                                                  GtkWidget          *widget,
-                                                                  gint                height,
-                                                                  gint               *minimum_width,
-                                                                  gint               *natural_width);
+static void      gtk_cell_area_box_add                            (GtkCellArea          *area,
+                                                                  GtkCellRenderer      *renderer);
+static void      gtk_cell_area_box_remove                         (GtkCellArea          *area,
+                                                                  GtkCellRenderer      *renderer);
+static void      gtk_cell_area_box_foreach                        (GtkCellArea          *area,
+                                                                  GtkCellCallback       callback,
+                                                                  gpointer              callback_data);
+static void      gtk_cell_area_box_foreach_alloc                  (GtkCellArea          *area,
+                                                                  GtkCellAreaContext   *context,
+                                                                  GtkWidget            *widget,
+                                                                  const GdkRectangle   *cell_area,
+                                                                  const GdkRectangle   *background_area,
+                                                                  GtkCellAllocCallback  callback,
+                                                                  gpointer              callback_data);
+static void      gtk_cell_area_box_set_cell_property              (GtkCellArea          *area,
+                                                                  GtkCellRenderer      *renderer,
+                                                                  guint                 prop_id,
+                                                                  const GValue         *value,
+                                                                  GParamSpec           *pspec);
+static void      gtk_cell_area_box_get_cell_property              (GtkCellArea          *area,
+                                                                  GtkCellRenderer      *renderer,
+                                                                  guint                 prop_id,
+                                                                  GValue               *value,
+                                                                  GParamSpec           *pspec);
+static GtkCellAreaContext *gtk_cell_area_box_create_context       (GtkCellArea          *area);
+static GtkSizeRequestMode  gtk_cell_area_box_get_request_mode     (GtkCellArea          *area);
+static void      gtk_cell_area_box_get_preferred_width            (GtkCellArea          *area,
+                                                                  GtkCellAreaContext   *context,
+                                                                  GtkWidget            *widget,
+                                                                  gint                 *minimum_width,
+                                                                  gint                 *natural_width);
+static void      gtk_cell_area_box_get_preferred_height           (GtkCellArea          *area,
+                                                                  GtkCellAreaContext   *context,
+                                                                  GtkWidget            *widget,
+                                                                  gint                 *minimum_height,
+                                                                  gint                 *natural_height);
+static void      gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea          *area,
+                                                                  GtkCellAreaContext   *context,
+                                                                  GtkWidget            *widget,
+                                                                  gint                  width,
+                                                                  gint                 *minimum_height,
+                                                                  gint                 *natural_height);
+static void      gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea          *area,
+                                                                  GtkCellAreaContext   *context,
+                                                                  GtkWidget            *widget,
+                                                                  gint                  height,
+                                                                  gint                 *minimum_width,
+                                                                  gint                 *natural_width);
+static gboolean  gtk_cell_area_box_focus                          (GtkCellArea          *area,
+                                                                  GtkDirectionType      direction);
 
 /* GtkCellLayoutIface */
 static void      gtk_cell_area_box_cell_layout_init               (GtkCellLayoutIface *iface);
@@ -109,39 +143,59 @@ typedef struct {
 typedef struct {
   GList *cells;
 
-  guint  id     : 16;
-  guint  expand : 1;
+  guint  id           : 8;
+  guint  n_cells      : 8;
+  guint  expand_cells : 8;
 } CellGroup;
 
-static CellInfo  *cell_info_new          (GtkCellRenderer *renderer, 
-                                         GtkPackType      pack,
-                                         gboolean         expand,
-                                         gboolean         align);
-static void       cell_info_free         (CellInfo        *info);
-static gint       cell_info_find         (CellInfo        *info,
-                                         GtkCellRenderer *renderer);
-
-static CellGroup *cell_group_new         (guint            id);
-static void       cell_group_free        (CellGroup       *group);
-
-static GList     *list_consecutive_cells (GtkCellAreaBox  *box);
-static GList     *construct_cell_groups  (GtkCellAreaBox  *box);
-static gint       count_expand_groups    (GtkCellAreaBox  *box);
-static gint       count_expand_cells     (CellGroup       *group);
-static void       iter_weak_notify       (GtkCellAreaBox  *box,
-                                         GtkCellAreaIter *dead_iter);
-static void       flush_iters            (GtkCellAreaBox  *box);
+typedef struct {
+  GtkCellRenderer *renderer;
+
+  gint             position;
+  gint             size;
+} AllocatedCell;
+
+static CellInfo      *cell_info_new          (GtkCellRenderer       *renderer, 
+                                             GtkPackType            pack,
+                                             gboolean               expand,
+                                             gboolean               align);
+static void           cell_info_free         (CellInfo              *info);
+static gint           cell_info_find         (CellInfo              *info,
+                                             GtkCellRenderer       *renderer);
+
+static AllocatedCell *allocated_cell_new     (GtkCellRenderer       *renderer,
+                                             gint                   position,
+                                             gint                   size);
+static void           allocated_cell_free    (AllocatedCell         *cell);
+static GList         *list_consecutive_cells (GtkCellAreaBox        *box);
+static gint           count_expand_groups    (GtkCellAreaBox        *box);
+static void           context_weak_notify    (GtkCellAreaBox        *box,
+                                             GtkCellAreaBoxContext *dead_context);
+static void           reset_contexts         (GtkCellAreaBox        *box);
+static void           init_context_groups    (GtkCellAreaBox        *box);
+static void           init_context_group     (GtkCellAreaBox        *box,
+                                             GtkCellAreaBoxContext *context);
+static GSList        *get_allocated_cells    (GtkCellAreaBox        *box,
+                                             GtkCellAreaBoxContext *context,
+                                             GtkWidget             *widget,
+                                             gint                   width,
+                                             gint                   height);
+
 
 struct _GtkCellAreaBoxPrivate
 {
   GtkOrientation  orientation;
 
   GList          *cells;
-  GList          *groups;
+  GArray         *groups;
 
-  GSList         *iters;
+  GSList         *contexts;
 
   gint            spacing;
+
+  /* We hold on to the rtl state from a widget we are requested for
+   * so that we can navigate focus correctly */
+  gboolean        rtl;
 };
 
 enum {
@@ -150,6 +204,13 @@ enum {
   PROP_SPACING
 };
 
+enum {
+  CELL_PROP_0,
+  CELL_PROP_EXPAND,
+  CELL_PROP_ALIGN,
+  CELL_PROP_PACK_TYPE
+};
+
 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
                                                gtk_cell_area_box_cell_layout_init)
@@ -170,10 +231,11 @@ gtk_cell_area_box_init (GtkCellAreaBox *box)
   priv = box->priv;
 
   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
+  priv->groups      = g_array_new (FALSE, TRUE, sizeof (CellGroup));
   priv->cells       = NULL;
-  priv->groups      = NULL;
-  priv->iters       = NULL;
+  priv->contexts    = NULL;
   priv->spacing     = 0;
+  priv->rtl         = FALSE;
 }
 
 static void 
@@ -189,21 +251,32 @@ gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
   object_class->get_property = gtk_cell_area_box_get_property;
 
   /* GtkCellAreaClass */
-  area_class->add                            = gtk_cell_area_box_add;
-  area_class->remove                         = gtk_cell_area_box_remove;
-  area_class->forall                         = gtk_cell_area_box_forall;
-  area_class->event                          = gtk_cell_area_box_event;
-  area_class->render                         = gtk_cell_area_box_render;
+  area_class->add                 = gtk_cell_area_box_add;
+  area_class->remove              = gtk_cell_area_box_remove;
+  area_class->foreach             = gtk_cell_area_box_foreach;
+  area_class->foreach_alloc       = gtk_cell_area_box_foreach_alloc;
+  area_class->set_cell_property   = gtk_cell_area_box_set_cell_property;
+  area_class->get_cell_property   = gtk_cell_area_box_get_cell_property;
   
-  area_class->create_iter                    = gtk_cell_area_box_create_iter;
+  area_class->create_context                 = gtk_cell_area_box_create_context;
   area_class->get_request_mode               = gtk_cell_area_box_get_request_mode;
   area_class->get_preferred_width            = gtk_cell_area_box_get_preferred_width;
   area_class->get_preferred_height           = gtk_cell_area_box_get_preferred_height;
   area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
   area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
 
+  area_class->focus = gtk_cell_area_box_focus;
+
+  /* Properties */
   g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
 
+  /**
+   * GtkCellAreaBox:spacing:
+   *
+   * The amount of space to reserve between cells.
+   *
+   * Since: 3.0
+   */
   g_object_class_install_property (object_class,
                                    PROP_SPACING,
                                    g_param_spec_int ("spacing",
@@ -214,6 +287,58 @@ gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
                                                     0,
                                                     GTK_PARAM_READWRITE));
 
+  /* Cell Properties */
+  /**
+   * GtkCellAreaBox:expand:
+   *
+   * Whether the cell renderer should receive extra space when the area receives
+   * more than its natural size.
+   *
+   * Since: 3.0
+   */
+  gtk_cell_area_class_install_cell_property (area_class,
+                                            CELL_PROP_EXPAND,
+                                            g_param_spec_boolean 
+                                            ("expand",
+                                             P_("Expand"),
+                                             P_("Whether the cell expands"),
+                                             FALSE,
+                                             GTK_PARAM_READWRITE));
+  
+  /**
+   * GtkCellAreaBox:align:
+   *
+   * Whether the cell renderer should be aligned in adjacent rows.
+   *
+   * Since: 3.0
+   */
+  gtk_cell_area_class_install_cell_property (area_class,
+                                            CELL_PROP_ALIGN,
+                                            g_param_spec_boolean
+                                            ("align",
+                                             P_("Align"),
+                                             P_("Whether cell should align with adjacent rows"),
+                                             TRUE,
+                                             GTK_PARAM_READWRITE));
+
+  /**
+   * GtkCellAreaBox:pack-type:
+   *
+   * A GtkPackType indicating whether the cell renderer is packed with reference to the 
+   * start or end of the area.
+   *
+   * Since: 3.0
+   */
+  gtk_cell_area_class_install_cell_property (area_class,
+                                            CELL_PROP_PACK_TYPE,
+                                            g_param_spec_enum
+                                            ("pack-type",
+                                             P_("Pack Type"),
+                                             P_("A GtkPackType indicating whether the cell is packed with "
+                                                "reference to the start or end of the cell area"),
+                                             GTK_TYPE_PACK_TYPE, GTK_PACK_START,
+                                             GTK_PARAM_READWRITE));
+
   g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
 }
 
@@ -252,21 +377,24 @@ cell_info_find (CellInfo        *info,
   return (info->renderer == renderer) ? 0 : -1;
 }
 
-static CellGroup *
-cell_group_new (guint id)
+static AllocatedCell *
+allocated_cell_new (GtkCellRenderer *renderer,
+                   gint             position,
+                   gint             size)
 {
-  CellGroup *group = g_slice_new0 (CellGroup);
+  AllocatedCell *cell = g_slice_new (AllocatedCell);
 
-  group->id = id;
+  cell->renderer = renderer;
+  cell->position = position;
+  cell->size     = size;
 
-  return group;
+  return cell;
 }
 
 static void
-cell_group_free (CellGroup *group)
+allocated_cell_free (AllocatedCell *cell)
 {
-  g_list_free (group->cells);
-  g_slice_free (CellGroup, group);  
+  g_slice_free (AllocatedCell, cell);
 }
 
 static GList *
@@ -301,21 +429,41 @@ list_consecutive_cells (GtkCellAreaBox *box)
   return consecutive_cells;
 }
 
-static GList *
-construct_cell_groups (GtkCellAreaBox  *box)
+static void
+cell_groups_clear (GtkCellAreaBox     *box)
 {
-  GtkCellAreaBoxPrivate *priv = box->priv;
-  CellGroup             *group;
+  GtkCellAreaBoxPrivate *priv  = box->priv;
+  gint                   i;
+
+  for (i = 0; i < priv->groups->len; i++)
+    {
+      CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
+
+      g_list_free (group->cells);
+    }
+
+  g_array_set_size (priv->groups, 0);
+}
+
+static void
+cell_groups_rebuild (GtkCellAreaBox *box)
+{
+  GtkCellAreaBoxPrivate *priv  = box->priv;
+  CellGroup              group = { 0, };
+  CellGroup             *group_ptr;
   GList                 *cells, *l;
-  GList                 *groups = NULL;
   guint                  id = 0;
 
+  cell_groups_clear (box);
+
   if (!priv->cells)
-    return NULL;
+    return;
+
+  cells = list_consecutive_cells (box);
 
-  cells  = list_consecutive_cells (box);
-  group  = cell_group_new (id++);
-  groups = g_list_prepend (groups, group);
+  /* First group is implied */
+  g_array_append_val (priv->groups, group);
+  group_ptr = &g_array_index (priv->groups, CellGroup, id);
 
   for (l = cells; l; l = l->next)
     {
@@ -324,104 +472,428 @@ construct_cell_groups (GtkCellAreaBox  *box)
       /* A new group starts with any aligned cell, the first group is implied */
       if (info->align && l != cells)
        {
-         group  = cell_group_new (id++);
-         groups = g_list_prepend (groups, group);
+         memset (&group, 0x0, sizeof (CellGroup));
+         group.id = ++id;
+
+         g_array_append_val (priv->groups, group);
+         group_ptr = &g_array_index (priv->groups, CellGroup, id);
        }
 
-      group->cells = g_list_prepend (group->cells, info);
+      group_ptr->cells = g_list_prepend (group_ptr->cells, info);
+      group_ptr->n_cells++;
 
       /* A group expands if it contains any expand cells */
       if (info->expand)
-       group->expand = TRUE;
+       group_ptr->expand_cells++;
     }
 
   g_list_free (cells);
 
-  for (l = cells; l; l = l->next)
+  for (id = 0; id < priv->groups->len; id++)
+    {
+      group_ptr = &g_array_index (priv->groups, CellGroup, id);
+
+      group_ptr->cells = g_list_reverse (group_ptr->cells);
+    }
+
+  /* Contexts need to be updated with the new grouping information */
+  init_context_groups (box);
+}
+
+static gint
+count_visible_cells (CellGroup *group, 
+                    gint      *expand_cells)
+{
+  GList *l;
+  gint   visible_cells = 0;
+  gint   n_expand_cells = 0;
+
+  for (l = group->cells; l; l = l->next)
     {
-      group = l->data;
-      group->cells = g_list_reverse (group->cells);
+      CellInfo *info = l->data;
+
+      if (gtk_cell_renderer_get_visible (info->renderer))
+       {
+         visible_cells++;
+
+         if (info->expand)
+           n_expand_cells++;
+       }
     }
 
-  return g_list_reverse (groups);
+  if (expand_cells)
+    *expand_cells = n_expand_cells;
+
+  return visible_cells;
 }
 
 static gint
 count_expand_groups (GtkCellAreaBox  *box)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
-  GList                 *l;
+  gint                   i;
   gint                   expand_groups = 0;
 
-  for (l = priv->groups; l; l = l->next)
+  for (i = 0; i < priv->groups->len; i++)
     {
-      CellGroup *group = l->data;
+      CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
 
-      if (group->expand)
+      if (group->expand_cells > 0)
        expand_groups++;
     }
 
   return expand_groups;
 }
 
-static gint
-count_expand_cells (CellGroup *group)
+static void 
+context_weak_notify (GtkCellAreaBox        *box,
+                    GtkCellAreaBoxContext *dead_context)
 {
-  GList *l;
-  gint   expand_cells = 0;
+  GtkCellAreaBoxPrivate *priv = box->priv;
 
-  if (!group->expand)
-    return 0;
+  priv->contexts = g_slist_remove (priv->contexts, dead_context);
+}
 
-  for (l = group->cells; l; l = l->next)
+static void
+init_context_group (GtkCellAreaBox        *box,
+                   GtkCellAreaBoxContext *context)
+{
+  GtkCellAreaBoxPrivate *priv = box->priv;
+  gint                  *expand_groups, i;
+
+  expand_groups = g_new (gboolean, priv->groups->len);
+
+  for (i = 0; i < priv->groups->len; i++)
     {
-      CellInfo *info = l->data;
+      CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
 
-      if (info->expand)
-       expand_cells++;
+      expand_groups[i] = (group->expand_cells > 0);
     }
 
-  return expand_cells;
+  /* This call implies reseting the request info */
+  gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups);
+  g_free (expand_groups);
 }
 
-static void 
-iter_weak_notify (GtkCellAreaBox  *box,
-                 GtkCellAreaIter *dead_iter)
+static void
+init_context_groups (GtkCellAreaBox *box)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
+  GSList                *l;
+
+  /* When the box's groups are reconstructed, contexts need to
+   * be reinitialized.
+   */
+  for (l = priv->contexts; l; l = l->next)
+    {
+      GtkCellAreaBoxContext *context = l->data;
 
-  priv->iters = g_slist_remove (priv->iters, dead_iter);
+      init_context_group (box, context);
+    }
 }
 
 static void
-flush_iters (GtkCellAreaBox *box)
+reset_contexts (GtkCellAreaBox *box)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
   GSList                *l;
 
-  /* When the box layout changes, iters need to
-   * be flushed and sizes for the box get requested again
+  /* When the box layout changes, contexts need to
+   * be reset and sizes for the box get requested again
    */
-  for (l = priv->iters; l; l = l->next)
+  for (l = priv->contexts; l; l = l->next)
     {
-      GtkCellAreaIter *iter = l->data;
+      GtkCellAreaContext *context = l->data;
 
-      gtk_cell_area_iter_flush (iter);
+      gtk_cell_area_context_reset (context);
     }
 }
 
-
-/* XXX This guy makes an allocation to be stored and retrieved from the iter */
-GtkCellAreaBoxAllocation *
-gtk_cell_area_box_allocate (GtkCellAreaBox     *box,
-                           GtkCellAreaBoxIter *iter,
-                           gint                size,
-                           gint               *n_allocs)
+/* Fall back on a completely unaligned dynamic allocation of cells
+ * when not allocated for the said orientation, alignment of cells
+ * is not done when each area gets a different size in the orientation
+ * of the box.
+ */
+static GSList *
+allocate_cells_manually (GtkCellAreaBox        *box,
+                        GtkWidget             *widget,
+                        gint                   width,
+                        gint                   height)
 {
+  GtkCellAreaBoxPrivate    *priv = box->priv;
+  GList                    *cells, *l;
+  GSList                   *allocated_cells = NULL;
+  GtkRequestedSize         *sizes;
+  gint                      i;
+  gint                      nvisible = 0, nexpand = 0, group_expand;
+  gint                      avail_size, extra_size, extra_extra, full_size;
+  gint                      position = 0, for_size;
+  gboolean                  rtl;
 
+  if (!priv->cells)
+    return NULL;
 
+  /* 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);
+
+  cells = list_consecutive_cells (box);
+
+  /* Count the visible and expand cells */
+  for (i = 0; i < priv->groups->len; i++)
+    {
+      CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
+
+      nvisible += count_visible_cells (group, &group_expand);
+      nexpand  += group_expand;
+    }
+
+  if (nvisible <= 0)
+    {
+      g_list_free (cells);
+      return NULL;
+    }
+
+  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      full_size = avail_size = width;
+      for_size  = height;
+    }
+  else
+    {
+      full_size = avail_size = height;
+      for_size  = width;
+    }
+
+  /* Go ahead and collect the requests on the fly */
+  sizes = g_new0 (GtkRequestedSize, nvisible);
+  for (l = cells, i = 0; l; l = l->next)
+    {
+      CellInfo *info = l->data;
+
+      if (!gtk_cell_renderer_get_visible (info->renderer))
+       continue;
+
+      gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
+                                     priv->orientation,
+                                     widget, for_size,
+                                     &sizes[i].minimum_size,
+                                     &sizes[i].natural_size);
+
+      avail_size -= sizes[i].minimum_size;
+
+      sizes[i].data = info;
+
+      i++;
+    }
+
+  /* Naturally distribute the allocation */
+  avail_size -= (nvisible - 1) * priv->spacing;
+  if (avail_size > 0)
+    avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
+  else
+    avail_size = 0;
+
+  /* Calculate/distribute expand for cells */
+  if (nexpand > 0)
+    {
+      extra_size  = avail_size / nexpand;
+      extra_extra = avail_size % nexpand;
+    }
+  else
+    extra_size = extra_extra = 0;
+
+  /* Create the allocated cells */
+  for (i = 0; i < nvisible; i++)
+    {
+      CellInfo      *info = sizes[i].data;
+      AllocatedCell *cell;
+
+      if (info->expand)
+       {
+         sizes[i].minimum_size += extra_size;
+         if (extra_extra)
+           {
+             sizes[i].minimum_size++;
+             extra_extra--;
+           }
+       }
+      
+      if (rtl)
+       cell = allocated_cell_new (info->renderer, 
+                                  full_size - (position + sizes[i].minimum_size),
+                                  sizes[i].minimum_size);
+      else
+       cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
+
+      allocated_cells = g_slist_prepend (allocated_cells, cell);
+             
+      position += sizes[i].minimum_size;
+      position += priv->spacing;
+    }
+
+  g_free (sizes);
+  g_list_free (cells);
+
+  /* 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);
 }
 
+/* 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;
+  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 (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;
+
+         if (rtl)
+           cell = allocated_cell_new (info->renderer, 
+                                      full_size - (group_allocs[i].position + group_allocs[i].size),
+                                      group_allocs[i].size);
+         else
+           cell = allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
+
+         allocated_cells = g_slist_prepend (allocated_cells, cell);
+       }
+      else
+       {
+         GtkRequestedSize *sizes;
+         gint              avail_size, 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;
+
+         /* Offset the allocation to the group position and allocate into 
+          * the group's available size */
+         position   = group_allocs[i].position;
+         avail_size = group_allocs[i].size;
+
+         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 - (position + sizes[j].minimum_size),
+                                          sizes[j].minimum_size);
+             else
+               cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
+
+             allocated_cells = g_slist_prepend (allocated_cells, cell);
+             
+             position += sizes[j].minimum_size;
+             position += priv->spacing;
+           }
+
+         g_free (sizes);
+       }
+    }
+
+  /* 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);
+}
 
 /*************************************************************
  *                      GObjectClass                         *
@@ -433,11 +905,17 @@ gtk_cell_area_box_finalize (GObject *object)
   GtkCellAreaBoxPrivate *priv = box->priv;
   GSList                *l;
 
-  for (l = priv->iters; l; l = l->next)
-    g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)iter_weak_notify, box);
+  /* Unref/free the context list */
+  for (l = priv->contexts; l; l = l->next)
+    g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
 
-  g_slist_free (priv->iters);
+  g_slist_free (priv->contexts);
+  priv->contexts = NULL;
 
+  /* Free the cell grouping info */
+  cell_groups_clear (box);
+  g_array_free (priv->groups, TRUE);
+  
   G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
 }
 
@@ -457,6 +935,12 @@ gtk_cell_area_box_set_property (GObject       *object,
 
   switch (prop_id)
     {
+    case PROP_ORIENTATION:
+      box->priv->orientation = g_value_get_enum (value);
+
+      /* Notify that size needs to be requested again */
+      reset_contexts (box);
+      break;
     case PROP_SPACING:
       gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
       break;
@@ -476,6 +960,9 @@ gtk_cell_area_box_get_property (GObject     *object,
 
   switch (prop_id)
     {
+    case PROP_ORIENTATION:
+      g_value_set_enum (value, box->priv->orientation);
+      break;
     case PROP_SPACING:
       g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
       break;
@@ -516,21 +1003,16 @@ gtk_cell_area_box_remove (GtkCellArea        *area,
       priv->cells = g_list_delete_link (priv->cells, node);
 
       /* Reconstruct cell groups */
-      g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
-      g_list_free (priv->groups);
-      priv->groups = construct_cell_groups (box);
-
-      /* Notify that size needs to be requested again */
-      flush_iters (box);
+      cell_groups_rebuild (box);
     }
   else
     g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
 }
 
 static void
-gtk_cell_area_box_forall (GtkCellArea        *area,
-                         GtkCellCallback     callback,
-                         gpointer            callback_data)
+gtk_cell_area_box_foreach (GtkCellArea        *area,
+                          GtkCellCallback     callback,
+                          gpointer            callback_data)
 {
   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
   GtkCellAreaBoxPrivate *priv = box->priv;
@@ -540,43 +1022,256 @@ gtk_cell_area_box_forall (GtkCellArea        *area,
     {
       CellInfo *info = list->data;
 
-      callback (info->renderer, callback_data);
+      if (callback (info->renderer, callback_data))
+       break;
     }
 }
 
-static gint
-gtk_cell_area_box_event (GtkCellArea        *area,
-                        GtkWidget          *widget,
-                        GdkEvent           *event,
-                        const GdkRectangle *cell_area)
+static void
+gtk_cell_area_box_foreach_alloc (GtkCellArea          *area,
+                                GtkCellAreaContext   *context,
+                                GtkWidget            *widget,
+                                const GdkRectangle   *cell_area,
+                                const GdkRectangle   *background_area,
+                                GtkCellAllocCallback  callback,
+                                gpointer              callback_data)
 {
+  GtkCellAreaBox        *box      = GTK_CELL_AREA_BOX (area);
+  GtkCellAreaBoxPrivate *priv     = box->priv;
+  GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
+  GSList                *allocated_cells, *l;
+  GdkRectangle           cell_alloc, cell_background;
+  gboolean               rtl;
+
+  rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+        gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
+
+  cell_alloc = *cell_area;
+
+  /* Get a list of cells with allocation sizes decided regardless
+   * of alignments and pack order etc. */
+  allocated_cells = get_allocated_cells (box, box_context, widget, 
+                                        cell_area->width, cell_area->height);
+
+  for (l = allocated_cells; l; l = l->next)
+    {
+      AllocatedCell *cell = l->data;
+
+      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+       {
+         cell_alloc.x     = cell_area->x + cell->position;
+         cell_alloc.width = cell->size;
+       }
+      else
+       {
+         cell_alloc.y      = cell_area->y + cell->position;
+         cell_alloc.height = cell->size;
+       }
+
+      /* Stop iterating over cells if they flow out of the render area,
+       * this can happen because the render area can actually be
+       * smaller than the requested area (treeview columns can
+       * be user resizable and can be resized to be smaller than
+       * the actual requested area). */
+      if (cell_alloc.x > cell_area->x + cell_area->width ||
+         cell_alloc.x + cell_alloc.width < cell_area->x ||
+         cell_alloc.y > cell_area->y + cell_area->height)
+       break;
+
+      /* Special case for the last cell (or first cell in rtl)... let the last cell consume 
+       * the remaining space in the area (the last cell is allowed to consume the remaining 
+       * space if the space given for rendering is actually larger than allocation, this can
+       * happen in the expander GtkTreeViewColumn where only the deepest depth column
+       * receives the allocation... shallow columns recieve more width). */
+      if (!l->next)
+       {
+         if (rtl)
+           {
+             /* Fill the leading space for the first cell in the area (still last in the list) */
+             cell_alloc.width = (cell_alloc.x - cell_area->x) + cell_alloc.width;
+             cell_alloc.x     = cell_area->x;
+           }
+         else
+           {
+             cell_alloc.width  = cell_area->x + cell_area->width  - cell_alloc.x;
+             cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
+           }
+       }
+      else
+       {
+         /* If the cell we are rendering doesnt fit into the remaining space, clip it
+          * so that the underlying renderer has a chance to deal with it (for instance
+          * text renderers get a chance to ellipsize).
+          */
+         if (cell_alloc.x + cell_alloc.width > cell_area->x + cell_area->width)
+           cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
+
+         if (cell_alloc.y + cell_alloc.height > cell_area->y + cell_area->height)
+           cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
+       }
+
+      /* Add portions of the background_area to the cell_alloc
+       * to create the cell_background */
+      cell_background = cell_alloc;
+
+      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+       {
+         if (l == allocated_cells)
+           {
+             cell_background.width += cell_background.x - background_area->x;
+             cell_background.x      = background_area->x;
+           }
 
+         if (l->next == NULL)
+             cell_background.width = 
+               background_area->width - (cell_background.x - background_area->x);
+
+         cell_background.y      = background_area->y;
+         cell_background.height = background_area->height;
+       }
+      else
+       {
+         if (l == allocated_cells)
+           {
+             cell_background.height += cell_background.y - background_area->y;
+             cell_background.y       = background_area->y;
+           }
+
+         if (l->next == NULL)
+             cell_background.height = 
+               background_area->height - (cell_background.y - background_area->y);
+
+         cell_background.x     = background_area->x;
+         cell_background.width = background_area->width;
+       }
+
+      if (callback (cell->renderer, &cell_alloc, &cell_background, callback_data))
+       break;
+    }
 
-  return 0;
+  g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
+  g_slist_free (allocated_cells);
 }
 
 static void
-gtk_cell_area_box_render (GtkCellArea        *area,
-                         cairo_t            *cr,
-                         GtkWidget          *widget,
-                         const GdkRectangle *cell_area)
+gtk_cell_area_box_set_cell_property (GtkCellArea        *area,
+                                    GtkCellRenderer    *renderer,
+                                    guint               prop_id,
+                                    const GValue       *value,
+                                    GParamSpec         *pspec)
 {
+  GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area); 
+  GtkCellAreaBoxPrivate *priv = box->priv;
+  GList                 *node;
+  CellInfo              *info;
+  gboolean               rebuild = FALSE;
+  gboolean               val;
+  GtkPackType            pack_type;
+
+  node = g_list_find_custom (priv->cells, renderer, 
+                            (GCompareFunc)cell_info_find);
+  if (!node)
+    return;
+
+  info = node->data;
+
+  switch (prop_id)
+    {
+    case CELL_PROP_EXPAND:
+      val = g_value_get_boolean (value);
+
+      if (info->expand != val)
+       {
+         info->expand = val;
+         rebuild      = TRUE;
+       }
+      break;
+
+    case CELL_PROP_ALIGN:
+      val = g_value_get_boolean (value);
+
+      if (info->align != val)
+       {
+         info->align = val;
+         rebuild     = TRUE;
+       }
+      break;
+
+    case CELL_PROP_PACK_TYPE:
+      pack_type = g_value_get_enum (value);
+
+      if (info->pack != pack_type)
+       {
+         info->pack = pack_type;
+         rebuild    = TRUE;
+       }
+      break;
+    default:
+      GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
+      break;
+    }
 
+  /* Groups need to be rebuilt */
+  if (rebuild)
+    cell_groups_rebuild (box);
 }
 
-static GtkCellAreaIter *
-gtk_cell_area_box_create_iter (GtkCellArea *area)
+static void
+gtk_cell_area_box_get_cell_property (GtkCellArea        *area,
+                                    GtkCellRenderer    *renderer,
+                                    guint               prop_id,
+                                    GValue             *value,
+                                    GParamSpec         *pspec)
+{
+  GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area); 
+  GtkCellAreaBoxPrivate *priv = box->priv;
+  GList                 *node;
+  CellInfo              *info;
+
+  node = g_list_find_custom (priv->cells, renderer, 
+                            (GCompareFunc)cell_info_find);
+  if (!node)
+    return;
+
+  info = node->data;
+
+  switch (prop_id)
+    {
+    case CELL_PROP_EXPAND:
+      g_value_set_boolean (value, info->expand);
+      break;
+
+    case CELL_PROP_ALIGN:
+      g_value_set_boolean (value, info->align);
+      break;
+
+    case CELL_PROP_PACK_TYPE:
+      g_value_set_enum (value, info->pack);
+      break;
+    default:
+      GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
+      break;
+    }
+}
+
+
+static GtkCellAreaContext *
+gtk_cell_area_box_create_context (GtkCellArea *area)
 {
   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
   GtkCellAreaBoxPrivate *priv = box->priv;
-  GtkCellAreaIter       *iter =
-    (GtkCellAreaIter *)g_object_new (GTK_TYPE_CELL_AREA_BOX_ITER, NULL);
+  GtkCellAreaContext    *context =
+    (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT, 
+                                    "area", area, NULL);
 
-  priv->iters = g_slist_prepend (priv->iters, iter);
+  priv->contexts = g_slist_prepend (priv->contexts, context);
 
-  g_object_weak_ref (G_OBJECT (iter), (GWeakNotify)iter_weak_notify, box);
+  g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
 
-  return iter;
+  /* Tell the new group about our cell layout */
+  init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
+
+  return context;
 }
 
 static GtkSizeRequestMode 
@@ -591,62 +1286,37 @@ gtk_cell_area_box_get_request_mode (GtkCellArea *area)
 }
 
 static void
-get_renderer_size (GtkCellRenderer    *renderer,
-                  GtkOrientation      orientation,
-                  GtkWidget          *widget,
-                  gint                for_size,
-                  gint               *minimum_size,
-                  gint               *natural_size)
-{
-  if (orientation == GTK_ORIENTATION_HORIZONTAL)
-    {
-      if (for_size < 0)
-       gtk_cell_renderer_get_preferred_width (renderer, widget, minimum_size, natural_size);
-      else
-       gtk_cell_renderer_get_preferred_width_for_height (renderer, widget, for_size, 
-                                                         minimum_size, natural_size);
-    }
-  else /* GTK_ORIENTATION_VERTICAL */
-    {
-      if (for_size < 0)
-       gtk_cell_renderer_get_preferred_height (renderer, widget, minimum_size, natural_size);
-      else
-       gtk_cell_renderer_get_preferred_height_for_width (renderer, widget, for_size, 
-                                                         minimum_size, natural_size);
-    }
-}
-
-static void
-compute_size (GtkCellAreaBox     *box,
-             GtkOrientation      orientation,
-             GtkCellAreaBoxIter *iter,
-             GtkWidget          *widget,
-             gint                for_size,
-             gint               *minimum_size,
-             gint               *natural_size)
+compute_size (GtkCellAreaBox        *box,
+             GtkOrientation         orientation,
+             GtkCellAreaBoxContext *context,
+             GtkWidget             *widget,
+             gint                   for_size,
+             gint                  *minimum_size,
+             gint                  *natural_size)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
-  CellGroup             *group;
-  CellInfo              *info;
-  GList                 *cell_list, *group_list;
+  GtkCellArea           *area = GTK_CELL_AREA (box);
+  GList                 *list;
+  gint                   i;
   gint                   min_size = 0;
   gint                   nat_size = 0;
   
-  for (group_list = priv->groups; group_list; group_list = group_list->next)
+  for (i = 0; i < priv->groups->len; i++)
     {
-      gint group_min_size = 0;
-      gint group_nat_size = 0;
-
-      group = group_list->data;
+      CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
+      gint       group_min_size = 0;
+      gint       group_nat_size = 0;
 
-      for (cell_list = group->cells; cell_list; cell_list = cell_list->next)
+      for (list = group->cells; list; list = list->next)
        {
-         gint renderer_min_size, renderer_nat_size;
-         
-         info = cell_list->data;
+         CellInfo *info = list->data;
+         gint      renderer_min_size, renderer_nat_size;
+
+         if (!gtk_cell_renderer_get_visible (info->renderer))
+             continue;
          
-         get_renderer_size (info->renderer, orientation, widget, for_size, 
-                            &renderer_min_size, &renderer_nat_size);
+         gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size, 
+                                         &renderer_min_size, &renderer_nat_size);
 
          if (orientation == priv->orientation)
            {
@@ -679,27 +1349,32 @@ compute_size (GtkCellAreaBox     *box,
       if (orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          if (for_size < 0)
-           gtk_cell_area_box_iter_push_group_width (iter, group->id, group_min_size, group_nat_size);
+           gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
          else
-           gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
-                                                               group_min_size, group_nat_size);
+           gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
+                                                                  group_min_size, group_nat_size);
        }
       else
        {
          if (for_size < 0)
-           gtk_cell_area_box_iter_push_group_height (iter, group->id, group_min_size, group_nat_size);
+           gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
          else
-           gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
-                                                               group_min_size, group_nat_size);
+           gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
+                                                                  group_min_size, group_nat_size);
        }
     }
 
   *minimum_size = min_size;
   *natural_size = nat_size;
+
+  /* Update rtl state for focus navigation to work */
+  priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+              gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
 }
 
 GtkRequestedSize *
-get_group_sizes (CellGroup      *group,
+get_group_sizes (GtkCellArea    *area,
+                CellGroup      *group,
                 GtkOrientation  orientation,
                 GtkWidget      *widget,
                 gint           *n_sizes)
@@ -708,19 +1383,24 @@ get_group_sizes (CellGroup      *group,
   GList            *l;
   gint              i;
 
-  *n_sizes = g_list_length (group->cells);
+  *n_sizes = count_visible_cells (group, NULL);
   sizes    = g_new (GtkRequestedSize, *n_sizes);
 
-  for (l = group->cells, i = 0; l; l = l->next, i++)
+  for (l = group->cells, i = 0; l; l = l->next)
     {
       CellInfo *info = l->data;
 
+      if (!gtk_cell_renderer_get_visible (info->renderer))
+       continue;
+
       sizes[i].data = info;
       
-      get_renderer_size (info->renderer,
-                        orientation, widget, -1,
-                        &sizes[i].minimum_size,
-                        &sizes[i].natural_size);
+      gtk_cell_area_request_renderer (area, info->renderer,
+                                     orientation, widget, -1,
+                                     &sizes[i].minimum_size,
+                                     &sizes[i].natural_size);
+
+      i++;
     }
 
   return sizes;
@@ -735,40 +1415,43 @@ compute_group_size_for_opposing_orientation (GtkCellAreaBox     *box,
                                             gint               *natural_size)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
+  GtkCellArea           *area = GTK_CELL_AREA (box);
 
   /* Exception for single cell groups */
-  if (!group->cells->next)
+  if (group->n_cells == 1)
     {
       CellInfo *info = group->cells->data;
 
-      get_renderer_size (info->renderer,
-                        OPPOSITE_ORIENTATION (priv->orientation),
-                        widget, for_size, minimum_size, natural_size);
+      gtk_cell_area_request_renderer (area, info->renderer,
+                                     OPPOSITE_ORIENTATION (priv->orientation),
+                                     widget, for_size, minimum_size, natural_size);
     }
   else
     {
       GtkRequestedSize *orientation_sizes;
       CellInfo         *info;
       gint              n_sizes, i;
-      gint              n_expand_cells = count_expand_cells (group);
       gint              avail_size     = for_size;
       gint              extra_size, extra_extra;
       gint              min_size = 0, nat_size = 0;
 
-      orientation_sizes = get_group_sizes (group, priv->orientation, widget, &n_sizes);
+      orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
 
       /* First naturally allocate the cells in the group into the for_size */
       avail_size -= (n_sizes - 1) * priv->spacing;
       for (i = 0; i < n_sizes; i++)
        avail_size -= orientation_sizes[i].minimum_size;
 
-      avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
+      if (avail_size > 0)
+        avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
+      else
+        avail_size = 0;
 
       /* Calculate/distribute expand for cells */
-      if (n_expand_cells > 0)
+      if (group->expand_cells > 0)
        {
-         extra_size  = avail_size / n_expand_cells;
-         extra_extra = avail_size % n_expand_cells;
+         extra_size  = avail_size / group->expand_cells;
+         extra_extra = avail_size % group->expand_cells;
        }
       else
        extra_size = extra_extra = 0;
@@ -789,11 +1472,11 @@ compute_group_size_for_opposing_orientation (GtkCellAreaBox     *box,
                }
            }
 
-         get_renderer_size (info->renderer,
-                            OPPOSITE_ORIENTATION (priv->orientation),
-                            widget, 
-                            orientation_sizes[i].minimum_size,
-                            &cell_min, &cell_nat);
+         gtk_cell_area_request_renderer (area, info->renderer,
+                                         OPPOSITE_ORIENTATION (priv->orientation),
+                                         widget, 
+                                         orientation_sizes[i].minimum_size,
+                                         &cell_min, &cell_nat);
 
          min_size = MAX (min_size, cell_min);
          nat_size = MAX (nat_size, cell_nat);
@@ -807,16 +1490,15 @@ compute_group_size_for_opposing_orientation (GtkCellAreaBox     *box,
 }
 
 static void
-compute_size_for_opposing_orientation (GtkCellAreaBox     *box, 
-                                      GtkCellAreaBoxIter *iter
-                                      GtkWidget          *widget, 
-                                      gint                for_size,
-                                      gint               *minimum_size, 
-                                      gint               *natural_size)
+compute_size_for_opposing_orientation (GtkCellAreaBox        *box, 
+                                      GtkCellAreaBoxContext *context
+                                      GtkWidget             *widget, 
+                                      gint                   for_size,
+                                      gint                  *minimum_size, 
+                                      gint                  *natural_size)
 {
   GtkCellAreaBoxPrivate *priv = box->priv;
   CellGroup             *group;
-  GList                 *group_list;
   GtkRequestedSize      *orientation_sizes;
   gint                   n_groups, n_expand_groups, i;
   gint                   avail_size = for_size;
@@ -826,16 +1508,19 @@ compute_size_for_opposing_orientation (GtkCellAreaBox     *box,
   n_expand_groups = count_expand_groups (box);
 
   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
-    orientation_sizes = gtk_cell_area_box_iter_get_widths (iter, &n_groups);
+    orientation_sizes = gtk_cell_area_box_context_get_widths (context, &n_groups);
   else
-    orientation_sizes = gtk_cell_area_box_iter_get_heights (iter, &n_groups);
+    orientation_sizes = gtk_cell_area_box_context_get_heights (context, &n_groups);
 
   /* First start by naturally allocating space among groups of cells */
   avail_size -= (n_groups - 1) * priv->spacing;
   for (i = 0; i < n_groups; i++)
     avail_size -= orientation_sizes[i].minimum_size;
 
-  avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
+  if (avail_size > 0)
+    avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
+  else
+    avail_size = 0;
 
   /* Calculate/distribute expand for groups */
   if (n_expand_groups > 0)
@@ -850,25 +1535,26 @@ compute_size_for_opposing_orientation (GtkCellAreaBox     *box,
    * and push the height-for-width for each group accordingly while accumulating
    * the overall height-for-width for this row.
    */
-  for (group_list = priv->groups; group_list; group_list = group_list->next)
+  for (i = 0; i < n_groups; i++)
     {
       gint group_min, group_nat;
+      gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
+      
+      group = &g_array_index (priv->groups, CellGroup, group_idx);
 
-      group = group_list->data;
-
-      if (group->expand)
+      if (group->expand_cells > 0)
        {
-         orientation_sizes[group->id].minimum_size += extra_size;
+         orientation_sizes[i].minimum_size += extra_size;
          if (extra_extra)
            {
-             orientation_sizes[group->id].minimum_size++;
+             orientation_sizes[i].minimum_size++;
              extra_extra--;
            }
        }
 
       /* Now we have the allocation for the group, request it's height-for-width */
       compute_group_size_for_opposing_orientation (box, group, widget,
-                                                  orientation_sizes[group->id].minimum_size,
+                                                  orientation_sizes[i].minimum_size,
                                                   &group_min, &group_nat);
 
       min_size = MAX (min_size, group_min);
@@ -876,13 +1562,13 @@ compute_size_for_opposing_orientation (GtkCellAreaBox     *box,
 
       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
-         gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
-                                                             group_min, group_nat);
+         gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
+                                                                group_min, group_nat);
        }
       else
        {
-         gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
-                                                             group_min, group_nat);
+         gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
+                                                                group_min, group_nat);
        }
     }
 
@@ -890,29 +1576,33 @@ compute_size_for_opposing_orientation (GtkCellAreaBox     *box,
   *natural_size = nat_size;
 
   g_free (orientation_sizes);
+
+  /* Update rtl state for focus navigation to work */
+  priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+              gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
 }
 
 
 
 static void
 gtk_cell_area_box_get_preferred_width (GtkCellArea        *area,
-                                      GtkCellAreaIter    *iter,
+                                      GtkCellAreaContext *context,
                                       GtkWidget          *widget,
                                       gint               *minimum_width,
                                       gint               *natural_width)
 {
   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
-  GtkCellAreaBoxIter    *box_iter;
+  GtkCellAreaBoxContext *box_context;
   gint                   min_width, nat_width;
 
-  g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
+  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
 
-  box_iter = GTK_CELL_AREA_BOX_ITER (iter);
+  box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
 
   /* Compute the size of all renderers for current row data, 
-   * bumping cell alignments in the iter along the way */
+   * bumping cell alignments in the context along the way */
   compute_size (box, GTK_ORIENTATION_HORIZONTAL,
-               box_iter, widget, -1, &min_width, &nat_width);
+               box_context, widget, -1, &min_width, &nat_width);
 
   if (minimum_width)
     *minimum_width = min_width;
@@ -923,23 +1613,23 @@ gtk_cell_area_box_get_preferred_width (GtkCellArea        *area,
 
 static void
 gtk_cell_area_box_get_preferred_height (GtkCellArea        *area,
-                                       GtkCellAreaIter    *iter,
+                                       GtkCellAreaContext *context,
                                        GtkWidget          *widget,
                                        gint               *minimum_height,
                                        gint               *natural_height)
 {
   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
-  GtkCellAreaBoxIter    *box_iter;
+  GtkCellAreaBoxContext *box_context;
   gint                   min_height, nat_height;
 
-  g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
+  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
 
-  box_iter = GTK_CELL_AREA_BOX_ITER (iter);
+  box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
 
   /* Compute the size of all renderers for current row data, 
-   * bumping cell alignments in the iter along the way */
+   * bumping cell alignments in the context along the way */
   compute_size (box, GTK_ORIENTATION_VERTICAL,
-               box_iter, widget, -1, &min_height, &nat_height);
+               box_context, widget, -1, &min_height, &nat_height);
 
   if (minimum_height)
     *minimum_height = min_height;
@@ -950,33 +1640,33 @@ gtk_cell_area_box_get_preferred_height (GtkCellArea        *area,
 
 static void
 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea        *area,
-                                                 GtkCellAreaIter    *iter,
+                                                 GtkCellAreaContext *context,
                                                  GtkWidget          *widget,
                                                  gint                width,
                                                  gint               *minimum_height,
                                                  gint               *natural_height)
 {
   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
-  GtkCellAreaBoxIter    *box_iter;
+  GtkCellAreaBoxContext *box_context;
   GtkCellAreaBoxPrivate *priv;
   gint                   min_height, nat_height;
 
-  g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
+  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
 
-  box_iter = GTK_CELL_AREA_BOX_ITER (iter);
-  priv     = box->priv;
+  box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
+  priv        = box->priv;
 
   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
     {
       /* Add up vertical requests of height for width and push the overall 
        * cached sizes for alignments */
-      compute_size (box, priv->orientation, box_iter, widget, width, &min_height, &nat_height);
+      compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
     }
   else
     {
       /* Juice: virtually allocate cells into the for_width using the 
        * alignments and then return the overall height for that width, and cache it */
-      compute_size_for_opposing_orientation (box, box_iter, widget, width, &min_height, &nat_height);
+      compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
     }
 
   if (minimum_height)
@@ -988,33 +1678,33 @@ gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea        *area,
 
 static void
 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea        *area,
-                                                 GtkCellAreaIter    *iter,
+                                                 GtkCellAreaContext *context,
                                                  GtkWidget          *widget,
                                                  gint                height,
                                                  gint               *minimum_width,
                                                  gint               *natural_width)
 {
   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
-  GtkCellAreaBoxIter    *box_iter;
+  GtkCellAreaBoxContext *box_context;
   GtkCellAreaBoxPrivate *priv;
   gint                   min_width, nat_width;
 
-  g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
+  g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
 
-  box_iter = GTK_CELL_AREA_BOX_ITER (iter);
-  priv     = box->priv;
+  box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
+  priv        = box->priv;
 
   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
     {
       /* Add up horizontal requests of width for height and push the overall 
        * cached sizes for alignments */
-      compute_size (box, priv->orientation, box_iter, widget, height, &min_width, &nat_width);
+      compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
     }
   else
     {
       /* Juice: horizontally allocate cells into the for_height using the 
        * alignments and then return the overall width for that height, and cache it */
-      compute_size_for_opposing_orientation (box, box_iter, widget, height, &min_width, &nat_width);
+      compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
     }
 
   if (minimum_width)
@@ -1024,6 +1714,101 @@ gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea        *area,
     *natural_width = nat_width;
 }
 
+enum {
+  FOCUS_NONE,
+  FOCUS_PREV,
+  FOCUS_NEXT
+};
+
+static gboolean
+gtk_cell_area_box_focus (GtkCellArea      *area,
+                        GtkDirectionType  direction)
+{
+  GtkCellAreaBox        *box   = GTK_CELL_AREA_BOX (area);
+  GtkCellAreaBoxPrivate *priv  = box->priv;
+  gint                   cycle = FOCUS_NONE;
+  gboolean               cycled_focus = FALSE;
+  GtkCellRenderer       *focus_cell;
+
+  focus_cell = gtk_cell_area_get_focus_cell (area);
+
+  /* Special case, when there is no activatable cell, focus
+   * is painted around the entire area... in this case we
+   * let focus leave the area directly.
+   */
+  if (focus_cell && !gtk_cell_area_is_activatable (area))
+    {
+      gtk_cell_area_set_focus_cell (area, NULL);
+      return FALSE;
+    }
+
+  switch (direction)
+    {
+    case GTK_DIR_TAB_FORWARD:
+      cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
+      break;
+    case GTK_DIR_TAB_BACKWARD:
+      cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
+      break;
+    case GTK_DIR_UP: 
+      if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
+       cycle = FOCUS_PREV;
+      break;
+    case GTK_DIR_DOWN:
+      if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
+       cycle = FOCUS_NEXT;
+      break;
+    case GTK_DIR_LEFT:
+      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
+       cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
+      break;
+    case GTK_DIR_RIGHT:
+      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
+       cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
+      break;
+    default:
+      break;
+    }
+
+  if (cycle != FOCUS_NONE)
+    {
+      gboolean  found_cell = FALSE;
+      GList    *list;
+      gint      i;
+
+      /* If there is no focused cell, focus on the first (or last) one in the list */
+      if (!focus_cell)
+       found_cell = TRUE;
+
+      for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1; 
+          cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
+          i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
+       {
+         CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
+         
+         for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells); 
+              cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
+           {
+             CellInfo *info = list->data;
+
+             if (info->renderer == focus_cell)
+               found_cell = TRUE;
+             else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
+                      gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
+               {
+                  gtk_cell_area_set_focus_cell (area, info->renderer);
+                  cycled_focus = TRUE;
+               }
+           }
+       }
+    }
+
+  if (!cycled_focus)
+    gtk_cell_area_set_focus_cell (area, NULL);
+
+  return cycled_focus;
+}
+
 
 /*************************************************************
  *                    GtkCellLayoutIface                     *
@@ -1072,25 +1857,41 @@ gtk_cell_area_box_layout_reorder (GtkCellLayout      *cell_layout,
       priv->cells = g_list_delete_link (priv->cells, node);
       priv->cells = g_list_insert (priv->cells, info, position);
 
-      /* Reconstruct cell groups */
-      g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
-      g_list_free (priv->groups);
-      priv->groups = construct_cell_groups (box);
-      
-      /* Notify that size needs to be requested again */
-      flush_iters (box);
+      cell_groups_rebuild (box);
     }
 }
 
 /*************************************************************
  *                            API                            *
  *************************************************************/
+/**
+ * gtk_cell_area_box_new:
+ * 
+ * Creates a new #GtkCellAreaBox.
+ *
+ * Return value: a newly created #GtkCellAreaBox
+ */
 GtkCellArea *
 gtk_cell_area_box_new (void)
 {
   return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
 }
 
+/**
+ * gtk_cell_area_box_pack_start:
+ * @box: a #GtkCellAreaBox
+ * @renderer: the #GtkCellRenderer to add
+ * @expand: whether @renderer should receive extra space when the area receives
+ * more than its natural size
+ * @align: whether @renderer should be aligned in adjacent rows.
+ *
+ * Adds @renderer to @box, packed with reference to the start of @box.
+ *
+ * The @renderer is packed after any other #GtkCellRenderer packed with reference
+ * to the start of @box.
+ *
+ * Since: 3.0
+ */
 void
 gtk_cell_area_box_pack_start  (GtkCellAreaBox  *box,
                               GtkCellRenderer *renderer,
@@ -1116,15 +1917,24 @@ gtk_cell_area_box_pack_start  (GtkCellAreaBox  *box,
 
   priv->cells = g_list_append (priv->cells, info);
 
-  /* Reconstruct cell groups */
-  g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
-  g_list_free (priv->groups);
-  priv->groups = construct_cell_groups (box);
-
-  /* Notify that size needs to be requested again */
-  flush_iters (box);
+  cell_groups_rebuild (box);
 }
 
+/**
+ * gtk_cell_area_box_pack_end:
+ * @box: a #GtkCellAreaBox
+ * @renderer: the #GtkCellRenderer to add
+ * @expand: whether @renderer should receive extra space when the area receives
+ * more than its natural size
+ * @align: whether @renderer should be aligned in adjacent rows.
+ *
+ * Adds @renderer to @box, packed with reference to the end of @box.
+ *
+ * The @renderer is packed after (away from end of) any other #GtkCellRenderer
+ * packed with reference to the end of @box.
+ *
+ * Since: 3.0
+ */
 void
 gtk_cell_area_box_pack_end (GtkCellAreaBox  *box,
                            GtkCellRenderer *renderer,
@@ -1150,15 +1960,19 @@ gtk_cell_area_box_pack_end (GtkCellAreaBox  *box,
 
   priv->cells = g_list_append (priv->cells, info);
 
-  /* Reconstruct cell groups */
-  g_list_foreach (priv->groups, (GFunc)cell_group_free, NULL);
-  g_list_free (priv->groups);
-  priv->groups = construct_cell_groups (box);
-
-  /* Notify that size needs to be requested again */
-  flush_iters (box);
+  cell_groups_rebuild (box);
 }
 
+/**
+ * gtk_cell_area_box_get_spacing:
+ * @box: a #GtkCellAreaBox
+ *
+ * Gets the spacing added between cell renderers.
+ *
+ * Return value: the space added between cell renderers in @box.
+ *
+ * Since: 3.0
+ */
 gint
 gtk_cell_area_box_get_spacing (GtkCellAreaBox  *box)
 {
@@ -1167,6 +1981,15 @@ gtk_cell_area_box_get_spacing (GtkCellAreaBox  *box)
   return box->priv->spacing;
 }
 
+/**
+ * gtk_cell_area_box_set_spacing:
+ * @box: a #GtkCellAreaBox
+ * @spacing: the space to add between #GtkCellRenderers
+ *
+ * Sets the spacing to add between cell renderers in @box.
+ *
+ * Since: 3.0
+ */
 void
 gtk_cell_area_box_set_spacing (GtkCellAreaBox  *box,
                               gint             spacing)
@@ -1184,6 +2007,6 @@ gtk_cell_area_box_set_spacing (GtkCellAreaBox  *box,
       g_object_notify (G_OBJECT (box), "spacing");
 
       /* Notify that size needs to be requested again */
-      flush_iters (box);
+      reset_contexts (box);
     }
 }