]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellareabox.c
Fixed some glitches in GtkCellAreaBox keynav from my last commit.
[~andy/gtk] / gtk / gtkcellareabox.c
1 /* gtkcellareabox.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.com>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24
25 /**
26  * SECTION:gtkcellareabox
27  * @Short_Description: A cell area that renders #GtkCellRenderers into a row or a column
28  * @Title: GtkCellAreaBox
29  *
30  * The #GtkCellAreaBox renders cell renderers into a row or a column depending on
31  * its #GtkOrientation.
32  *
33  * GtkCellAreaBox uses a notion of <emphasis>packing</emphasis>. Packing
34  * refers to adding cell renderers with reference to a particular position 
35  * in a #GtkCellAreaBox. There are two reference positions: the
36  * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
37  * When the #GtkCellAreaBox is oriented in the %GTK_ORIENTATION_VERTICAL orientation, 
38  * the start is defined as the top of the box and the end is defined as the bottom.
39  * In the %GTK_ORIENTATION_HORIZONTAL orientation start is defined as the
40  * left side and the end is defined as the right side.
41  *
42  * Alignments of #GtkCellRenderers rendered in adjacent rows can be configured
43  * by configuring the #GtkCellAreaBox:align child cell property with
44  * gtk_cell_area_cell_set_property() or by specifying the "align" argument
45  * to gtk_cell_area_box_pack_start() and gtk_cell_area_box_pack_end().
46  */
47
48 #include "config.h"
49 #include "gtkintl.h"
50 #include "gtkorientable.h"
51 #include "gtkcelllayout.h"
52 #include "gtkcellareabox.h"
53 #include "gtkcellareaboxcontext.h"
54 #include "gtkprivate.h"
55
56
57 /* GObjectClass */
58 static void      gtk_cell_area_box_finalize                       (GObject              *object);
59 static void      gtk_cell_area_box_dispose                        (GObject              *object);
60 static void      gtk_cell_area_box_set_property                   (GObject              *object,
61                                                                    guint                 prop_id,
62                                                                    const GValue         *value,
63                                                                    GParamSpec           *pspec);
64 static void      gtk_cell_area_box_get_property                   (GObject              *object,
65                                                                    guint                 prop_id,
66                                                                    GValue               *value,
67                                                                    GParamSpec           *pspec);
68
69 /* GtkCellAreaClass */
70 static void      gtk_cell_area_box_add                            (GtkCellArea          *area,
71                                                                    GtkCellRenderer      *renderer);
72 static void      gtk_cell_area_box_remove                         (GtkCellArea          *area,
73                                                                    GtkCellRenderer      *renderer);
74 static void      gtk_cell_area_box_foreach                        (GtkCellArea          *area,
75                                                                    GtkCellCallback       callback,
76                                                                    gpointer              callback_data);
77 static void      gtk_cell_area_box_foreach_alloc                  (GtkCellArea          *area,
78                                                                    GtkCellAreaContext   *context,
79                                                                    GtkWidget            *widget,
80                                                                    const GdkRectangle   *cell_area,
81                                                                    const GdkRectangle   *background_area,
82                                                                    GtkCellAllocCallback  callback,
83                                                                    gpointer              callback_data);
84 static void      gtk_cell_area_box_set_cell_property              (GtkCellArea          *area,
85                                                                    GtkCellRenderer      *renderer,
86                                                                    guint                 prop_id,
87                                                                    const GValue         *value,
88                                                                    GParamSpec           *pspec);
89 static void      gtk_cell_area_box_get_cell_property              (GtkCellArea          *area,
90                                                                    GtkCellRenderer      *renderer,
91                                                                    guint                 prop_id,
92                                                                    GValue               *value,
93                                                                    GParamSpec           *pspec);
94 static GtkCellAreaContext *gtk_cell_area_box_create_context       (GtkCellArea          *area);
95 static GtkSizeRequestMode  gtk_cell_area_box_get_request_mode     (GtkCellArea          *area);
96 static void      gtk_cell_area_box_get_preferred_width            (GtkCellArea          *area,
97                                                                    GtkCellAreaContext   *context,
98                                                                    GtkWidget            *widget,
99                                                                    gint                 *minimum_width,
100                                                                    gint                 *natural_width);
101 static void      gtk_cell_area_box_get_preferred_height           (GtkCellArea          *area,
102                                                                    GtkCellAreaContext   *context,
103                                                                    GtkWidget            *widget,
104                                                                    gint                 *minimum_height,
105                                                                    gint                 *natural_height);
106 static void      gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea          *area,
107                                                                    GtkCellAreaContext   *context,
108                                                                    GtkWidget            *widget,
109                                                                    gint                  width,
110                                                                    gint                 *minimum_height,
111                                                                    gint                 *natural_height);
112 static void      gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea          *area,
113                                                                    GtkCellAreaContext   *context,
114                                                                    GtkWidget            *widget,
115                                                                    gint                  height,
116                                                                    gint                 *minimum_width,
117                                                                    gint                 *natural_width);
118 static gboolean  gtk_cell_area_box_focus                          (GtkCellArea          *area,
119                                                                    GtkDirectionType      direction);
120
121 /* GtkCellLayoutIface */
122 static void      gtk_cell_area_box_cell_layout_init               (GtkCellLayoutIface *iface);
123 static void      gtk_cell_area_box_layout_pack_start              (GtkCellLayout      *cell_layout,
124                                                                    GtkCellRenderer    *renderer,
125                                                                    gboolean            expand);
126 static void      gtk_cell_area_box_layout_pack_end                (GtkCellLayout      *cell_layout,
127                                                                    GtkCellRenderer    *renderer,
128                                                                    gboolean            expand);
129 static void      gtk_cell_area_box_layout_reorder                 (GtkCellLayout      *cell_layout,
130                                                                    GtkCellRenderer    *renderer,
131                                                                    gint                position);
132 static void      gtk_cell_area_box_focus_changed                  (GtkCellArea        *area,
133                                                                    GParamSpec         *pspec,
134                                                                    GtkCellAreaBox     *box);
135
136
137 /* CellInfo/CellGroup metadata handling and convenience functions */
138 typedef struct {
139   GtkCellRenderer *renderer;
140
141   guint            expand : 1; /* Whether the cell expands */
142   guint            pack   : 1; /* Whether the cell is packed from the start or end */
143   guint            align  : 1; /* Whether to align this cell's position with adjacent rows */
144 } CellInfo;
145
146 typedef struct {
147   GList *cells;
148
149   guint  id           : 8;
150   guint  n_cells      : 8;
151   guint  expand_cells : 8;
152 } CellGroup;
153
154 typedef struct {
155   GtkCellRenderer *renderer;
156
157   gint             position;
158   gint             size;
159 } AllocatedCell;
160
161 static CellInfo      *cell_info_new          (GtkCellRenderer       *renderer, 
162                                               GtkPackType            pack,
163                                               gboolean               expand,
164                                               gboolean               align);
165 static void           cell_info_free         (CellInfo              *info);
166 static gint           cell_info_find         (CellInfo              *info,
167                                               GtkCellRenderer       *renderer);
168
169 static AllocatedCell *allocated_cell_new     (GtkCellRenderer       *renderer,
170                                               gint                   position,
171                                               gint                   size);
172 static void           allocated_cell_free    (AllocatedCell         *cell);
173 static GList         *list_consecutive_cells (GtkCellAreaBox        *box);
174 static gint           count_expand_groups    (GtkCellAreaBox        *box);
175 static void           context_weak_notify    (GtkCellAreaBox        *box,
176                                               GtkCellAreaBoxContext *dead_context);
177 static void           reset_contexts         (GtkCellAreaBox        *box);
178 static void           init_context_groups    (GtkCellAreaBox        *box);
179 static void           init_context_group     (GtkCellAreaBox        *box,
180                                               GtkCellAreaBoxContext *context);
181 static GSList        *get_allocated_cells    (GtkCellAreaBox        *box,
182                                               GtkCellAreaBoxContext *context,
183                                               GtkWidget             *widget,
184                                               gint                   width,
185                                               gint                   height);
186
187
188 struct _GtkCellAreaBoxPrivate
189 {
190   GtkOrientation   orientation;
191
192   /* We hold on to the previously focused cell when navigating
193    * up and down in a horizontal box (or left and right on a vertical one)
194    * this way we always re-enter the last focused cell. */
195   GtkCellRenderer *last_focus_cell;
196   gulong           focus_cell_id;
197
198   GList           *cells;
199   GArray          *groups;
200
201   GSList          *contexts;
202
203   gint             spacing;
204
205   /* We hold on to the rtl state from a widget we are requested for
206    * so that we can navigate focus correctly */
207   gboolean         rtl;
208 };
209
210 enum {
211   PROP_0,
212   PROP_ORIENTATION,
213   PROP_SPACING
214 };
215
216 enum {
217   CELL_PROP_0,
218   CELL_PROP_EXPAND,
219   CELL_PROP_ALIGN,
220   CELL_PROP_PACK_TYPE
221 };
222
223 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
224                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
225                                                 gtk_cell_area_box_cell_layout_init)
226                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
227
228 #define OPPOSITE_ORIENTATION(orientation)                       \
229   ((orientation) == GTK_ORIENTATION_HORIZONTAL ?                \
230    GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
231
232 static void
233 gtk_cell_area_box_init (GtkCellAreaBox *box)
234 {
235   GtkCellAreaBoxPrivate *priv;
236
237   box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
238                                            GTK_TYPE_CELL_AREA_BOX,
239                                            GtkCellAreaBoxPrivate);
240   priv = box->priv;
241
242   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
243   priv->groups      = g_array_new (FALSE, TRUE, sizeof (CellGroup));
244   priv->cells       = NULL;
245   priv->contexts    = NULL;
246   priv->spacing     = 0;
247   priv->rtl         = FALSE;
248
249   /* Watch whenever focus is given to a cell, even if it's not with keynav,
250    * this way we remember upon entry of the area where focus was last time around */
251   priv->focus_cell_id = g_signal_connect (box, "notify::focus-cell", 
252                                           G_CALLBACK (gtk_cell_area_box_focus_changed), box);
253 }
254
255 static void 
256 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
257 {
258   GObjectClass     *object_class = G_OBJECT_CLASS (class);
259   GtkCellAreaClass *area_class   = GTK_CELL_AREA_CLASS (class);
260
261   /* GObjectClass */
262   object_class->finalize     = gtk_cell_area_box_finalize;
263   object_class->dispose      = gtk_cell_area_box_dispose;
264   object_class->set_property = gtk_cell_area_box_set_property;
265   object_class->get_property = gtk_cell_area_box_get_property;
266
267   /* GtkCellAreaClass */
268   area_class->add                 = gtk_cell_area_box_add;
269   area_class->remove              = gtk_cell_area_box_remove;
270   area_class->foreach             = gtk_cell_area_box_foreach;
271   area_class->foreach_alloc       = gtk_cell_area_box_foreach_alloc;
272   area_class->set_cell_property   = gtk_cell_area_box_set_cell_property;
273   area_class->get_cell_property   = gtk_cell_area_box_get_cell_property;
274   
275   area_class->create_context                 = gtk_cell_area_box_create_context;
276   area_class->get_request_mode               = gtk_cell_area_box_get_request_mode;
277   area_class->get_preferred_width            = gtk_cell_area_box_get_preferred_width;
278   area_class->get_preferred_height           = gtk_cell_area_box_get_preferred_height;
279   area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
280   area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
281
282   area_class->focus = gtk_cell_area_box_focus;
283
284   /* Properties */
285   g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
286
287   /**
288    * GtkCellAreaBox:spacing:
289    *
290    * The amount of space to reserve between cells.
291    *
292    * Since: 3.0
293    */
294   g_object_class_install_property (object_class,
295                                    PROP_SPACING,
296                                    g_param_spec_int ("spacing",
297                                                      P_("Spacing"),
298                                                      P_("Space which is inserted between cells"),
299                                                      0,
300                                                      G_MAXINT,
301                                                      0,
302                                                      GTK_PARAM_READWRITE));
303
304   /* Cell Properties */
305   /**
306    * GtkCellAreaBox:expand:
307    *
308    * Whether the cell renderer should receive extra space when the area receives
309    * more than its natural size.
310    *
311    * Since: 3.0
312    */
313   gtk_cell_area_class_install_cell_property (area_class,
314                                              CELL_PROP_EXPAND,
315                                              g_param_spec_boolean 
316                                              ("expand",
317                                               P_("Expand"),
318                                               P_("Whether the cell expands"),
319                                               FALSE,
320                                               GTK_PARAM_READWRITE));
321   
322   /**
323    * GtkCellAreaBox:align:
324    *
325    * Whether the cell renderer should be aligned in adjacent rows.
326    *
327    * Since: 3.0
328    */
329   gtk_cell_area_class_install_cell_property (area_class,
330                                              CELL_PROP_ALIGN,
331                                              g_param_spec_boolean
332                                              ("align",
333                                               P_("Align"),
334                                               P_("Whether cell should align with adjacent rows"),
335                                               TRUE,
336                                               GTK_PARAM_READWRITE));
337
338   /**
339    * GtkCellAreaBox:pack-type:
340    *
341    * A GtkPackType indicating whether the cell renderer is packed with reference to the 
342    * start or end of the area.
343    *
344    * Since: 3.0
345    */
346   gtk_cell_area_class_install_cell_property (area_class,
347                                              CELL_PROP_PACK_TYPE,
348                                              g_param_spec_enum
349                                              ("pack-type",
350                                               P_("Pack Type"),
351                                               P_("A GtkPackType indicating whether the cell is packed with "
352                                                  "reference to the start or end of the cell area"),
353                                               GTK_TYPE_PACK_TYPE, GTK_PACK_START,
354                                               GTK_PARAM_READWRITE));
355
356   g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
357 }
358
359
360 /*************************************************************
361  *    CellInfo/CellGroup basics and convenience functions    *
362  *************************************************************/
363 static CellInfo *
364 cell_info_new  (GtkCellRenderer *renderer, 
365                 GtkPackType      pack,
366                 gboolean         expand,
367                 gboolean         align)
368 {
369   CellInfo *info = g_slice_new (CellInfo);
370   
371   info->renderer = g_object_ref_sink (renderer);
372   info->pack     = pack;
373   info->expand   = expand;
374   info->align    = align;
375
376   return info;
377 }
378
379 static void
380 cell_info_free (CellInfo *info)
381 {
382   g_object_unref (info->renderer);
383
384   g_slice_free (CellInfo, info);
385 }
386
387 static gint
388 cell_info_find (CellInfo        *info,
389                 GtkCellRenderer *renderer)
390 {
391   return (info->renderer == renderer) ? 0 : -1;
392 }
393
394 static AllocatedCell *
395 allocated_cell_new (GtkCellRenderer *renderer,
396                     gint             position,
397                     gint             size)
398 {
399   AllocatedCell *cell = g_slice_new (AllocatedCell);
400
401   cell->renderer = renderer;
402   cell->position = position;
403   cell->size     = size;
404
405   return cell;
406 }
407
408 static void
409 allocated_cell_free (AllocatedCell *cell)
410 {
411   g_slice_free (AllocatedCell, cell);
412 }
413
414 static GList *
415 list_consecutive_cells (GtkCellAreaBox *box)
416 {
417   GtkCellAreaBoxPrivate *priv = box->priv;
418   GList                 *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
419   CellInfo              *info;
420
421   /* List cells in consecutive order taking their 
422    * PACK_START/PACK_END options into account 
423    */
424   for (l = priv->cells; l; l = l->next)
425     {
426       info = l->data;
427       
428       if (info->pack == GTK_PACK_START)
429         consecutive_cells = g_list_prepend (consecutive_cells, info);
430     }
431
432   for (l = priv->cells; l; l = l->next)
433     {
434       info = l->data;
435       
436       if (info->pack == GTK_PACK_END)
437         pack_end_cells = g_list_prepend (pack_end_cells, info);
438     }
439
440   consecutive_cells = g_list_reverse (consecutive_cells);
441   consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
442
443   return consecutive_cells;
444 }
445
446 static void
447 cell_groups_clear (GtkCellAreaBox     *box)
448 {
449   GtkCellAreaBoxPrivate *priv  = box->priv;
450   gint                   i;
451
452   for (i = 0; i < priv->groups->len; i++)
453     {
454       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
455
456       g_list_free (group->cells);
457     }
458
459   g_array_set_size (priv->groups, 0);
460 }
461
462 static void
463 cell_groups_rebuild (GtkCellAreaBox *box)
464 {
465   GtkCellAreaBoxPrivate *priv  = box->priv;
466   CellGroup              group = { 0, };
467   CellGroup             *group_ptr;
468   GList                 *cells, *l;
469   guint                  id = 0;
470
471   cell_groups_clear (box);
472
473   if (!priv->cells)
474     return;
475
476   cells = list_consecutive_cells (box);
477
478   /* First group is implied */
479   g_array_append_val (priv->groups, group);
480   group_ptr = &g_array_index (priv->groups, CellGroup, id);
481
482   for (l = cells; l; l = l->next)
483     {
484       CellInfo *info = l->data;
485
486       /* A new group starts with any aligned cell, the first group is implied */
487       if (info->align && l != cells)
488         {
489           memset (&group, 0x0, sizeof (CellGroup));
490           group.id = ++id;
491
492           g_array_append_val (priv->groups, group);
493           group_ptr = &g_array_index (priv->groups, CellGroup, id);
494         }
495
496       group_ptr->cells = g_list_prepend (group_ptr->cells, info);
497       group_ptr->n_cells++;
498
499       /* A group expands if it contains any expand cells */
500       if (info->expand)
501         group_ptr->expand_cells++;
502     }
503
504   g_list_free (cells);
505
506   for (id = 0; id < priv->groups->len; id++)
507     {
508       group_ptr = &g_array_index (priv->groups, CellGroup, id);
509
510       group_ptr->cells = g_list_reverse (group_ptr->cells);
511     }
512
513   /* Contexts need to be updated with the new grouping information */
514   init_context_groups (box);
515 }
516
517 static gint
518 count_visible_cells (CellGroup *group, 
519                      gint      *expand_cells)
520 {
521   GList *l;
522   gint   visible_cells = 0;
523   gint   n_expand_cells = 0;
524
525   for (l = group->cells; l; l = l->next)
526     {
527       CellInfo *info = l->data;
528
529       if (gtk_cell_renderer_get_visible (info->renderer))
530         {
531           visible_cells++;
532
533           if (info->expand)
534             n_expand_cells++;
535         }
536     }
537
538   if (expand_cells)
539     *expand_cells = n_expand_cells;
540
541   return visible_cells;
542 }
543
544 static gint
545 count_expand_groups (GtkCellAreaBox  *box)
546 {
547   GtkCellAreaBoxPrivate *priv = box->priv;
548   gint                   i;
549   gint                   expand_groups = 0;
550
551   for (i = 0; i < priv->groups->len; i++)
552     {
553       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
554
555       if (group->expand_cells > 0)
556         expand_groups++;
557     }
558
559   return expand_groups;
560 }
561
562 static void 
563 context_weak_notify (GtkCellAreaBox        *box,
564                      GtkCellAreaBoxContext *dead_context)
565 {
566   GtkCellAreaBoxPrivate *priv = box->priv;
567
568   priv->contexts = g_slist_remove (priv->contexts, dead_context);
569 }
570
571 static void
572 init_context_group (GtkCellAreaBox        *box,
573                     GtkCellAreaBoxContext *context)
574 {
575   GtkCellAreaBoxPrivate *priv = box->priv;
576   gint                  *expand_groups, i;
577
578   expand_groups = g_new (gboolean, priv->groups->len);
579
580   for (i = 0; i < priv->groups->len; i++)
581     {
582       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
583
584       expand_groups[i] = (group->expand_cells > 0);
585     }
586
587   /* This call implies reseting the request info */
588   gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups);
589   g_free (expand_groups);
590 }
591
592 static void
593 init_context_groups (GtkCellAreaBox *box)
594 {
595   GtkCellAreaBoxPrivate *priv = box->priv;
596   GSList                *l;
597
598   /* When the box's groups are reconstructed, contexts need to
599    * be reinitialized.
600    */
601   for (l = priv->contexts; l; l = l->next)
602     {
603       GtkCellAreaBoxContext *context = l->data;
604
605       init_context_group (box, context);
606     }
607 }
608
609 static void
610 reset_contexts (GtkCellAreaBox *box)
611 {
612   GtkCellAreaBoxPrivate *priv = box->priv;
613   GSList                *l;
614
615   /* When the box layout changes, contexts need to
616    * be reset and sizes for the box get requested again
617    */
618   for (l = priv->contexts; l; l = l->next)
619     {
620       GtkCellAreaContext *context = l->data;
621
622       gtk_cell_area_context_reset (context);
623     }
624 }
625
626 /* Fall back on a completely unaligned dynamic allocation of cells
627  * when not allocated for the said orientation, alignment of cells
628  * is not done when each area gets a different size in the orientation
629  * of the box.
630  */
631 static GSList *
632 allocate_cells_manually (GtkCellAreaBox        *box,
633                          GtkWidget             *widget,
634                          gint                   width,
635                          gint                   height)
636 {
637   GtkCellAreaBoxPrivate    *priv = box->priv;
638   GList                    *cells, *l;
639   GSList                   *allocated_cells = NULL;
640   GtkRequestedSize         *sizes;
641   gint                      i;
642   gint                      nvisible = 0, nexpand = 0, group_expand;
643   gint                      avail_size, extra_size, extra_extra, full_size;
644   gint                      position = 0, for_size;
645   gboolean                  rtl;
646
647   if (!priv->cells)
648     return NULL;
649
650   /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
651   rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
652          gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
653
654   cells = list_consecutive_cells (box);
655
656   /* Count the visible and expand cells */
657   for (i = 0; i < priv->groups->len; i++)
658     {
659       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
660
661       nvisible += count_visible_cells (group, &group_expand);
662       nexpand  += group_expand;
663     }
664
665   if (nvisible <= 0)
666     {
667       g_list_free (cells);
668       return NULL;
669     }
670
671   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
672     {
673       full_size = avail_size = width;
674       for_size  = height;
675     }
676   else
677     {
678       full_size = avail_size = height;
679       for_size  = width;
680     }
681
682   /* Go ahead and collect the requests on the fly */
683   sizes = g_new0 (GtkRequestedSize, nvisible);
684   for (l = cells, i = 0; l; l = l->next)
685     {
686       CellInfo *info = l->data;
687
688       if (!gtk_cell_renderer_get_visible (info->renderer))
689         continue;
690
691       gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
692                                       priv->orientation,
693                                       widget, for_size,
694                                       &sizes[i].minimum_size,
695                                       &sizes[i].natural_size);
696
697       avail_size -= sizes[i].minimum_size;
698
699       sizes[i].data = info;
700
701       i++;
702     }
703
704   /* Naturally distribute the allocation */
705   avail_size -= (nvisible - 1) * priv->spacing;
706   if (avail_size > 0)
707     avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
708   else
709     avail_size = 0;
710
711   /* Calculate/distribute expand for cells */
712   if (nexpand > 0)
713     {
714       extra_size  = avail_size / nexpand;
715       extra_extra = avail_size % nexpand;
716     }
717   else
718     extra_size = extra_extra = 0;
719
720   /* Create the allocated cells */
721   for (i = 0; i < nvisible; i++)
722     {
723       CellInfo      *info = sizes[i].data;
724       AllocatedCell *cell;
725
726       if (info->expand)
727         {
728           sizes[i].minimum_size += extra_size;
729           if (extra_extra)
730             {
731               sizes[i].minimum_size++;
732               extra_extra--;
733             }
734         }
735       
736       if (rtl)
737         cell = allocated_cell_new (info->renderer, 
738                                    full_size - (position + sizes[i].minimum_size),
739                                    sizes[i].minimum_size);
740       else
741         cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
742
743       allocated_cells = g_slist_prepend (allocated_cells, cell);
744               
745       position += sizes[i].minimum_size;
746       position += priv->spacing;
747     }
748
749   g_free (sizes);
750   g_list_free (cells);
751
752   /* Note it might not be important to reverse the list here at all,
753    * we have the correct positions, no need to allocate from left to right */
754   return g_slist_reverse (allocated_cells);
755 }
756
757 /* Returns an allocation for each cell in the orientation of the box,
758  * used in ->render()/->event() implementations to get a straight-forward
759  * list of allocated cells to operate on.
760  */
761 static GSList *
762 get_allocated_cells (GtkCellAreaBox        *box,
763                      GtkCellAreaBoxContext *context,
764                      GtkWidget             *widget,
765                      gint                   width,
766                      gint                   height)
767 {
768   GtkCellAreaBoxAllocation *group_allocs;
769   GtkCellArea              *area = GTK_CELL_AREA (box);
770   GtkCellAreaBoxPrivate    *priv = box->priv;
771   GList                    *cell_list;
772   GSList                   *allocated_cells = NULL;
773   gint                      i, j, n_allocs;
774   gint                      for_size, full_size;
775   gboolean                  rtl;
776
777   group_allocs = gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
778   if (!group_allocs)
779     return allocate_cells_manually (box, widget, width, height);
780
781   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
782     {
783       full_size = width;
784       for_size  = height;
785     }
786   else
787     {
788       full_size = height;
789       for_size  = width;
790     }
791
792   /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
793   rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
794          gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
795
796   for (i = 0; i < n_allocs; i++)
797     {
798       /* We dont always allocate all groups, sometimes the requested group has only invisible
799        * cells for every row, hence the usage of group_allocs[i].group_idx here
800        */
801       CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
802
803       /* Exception for single cell groups */
804       if (group->n_cells == 1)
805         {
806           CellInfo      *info = group->cells->data;
807           AllocatedCell *cell;
808
809           if (rtl)
810             cell = allocated_cell_new (info->renderer, 
811                                        full_size - (group_allocs[i].position + group_allocs[i].size),
812                                        group_allocs[i].size);
813           else
814             cell = allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
815
816           allocated_cells = g_slist_prepend (allocated_cells, cell);
817         }
818       else
819         {
820           GtkRequestedSize *sizes;
821           gint              avail_size, position;
822           gint              visible_cells, expand_cells;
823           gint              extra_size, extra_extra;
824
825           visible_cells = count_visible_cells (group, &expand_cells);
826
827           /* If this row has no visible cells in this group, just
828            * skip the allocation */
829           if (visible_cells == 0)
830             continue;
831
832           /* Offset the allocation to the group position and allocate into 
833            * the group's available size */
834           position   = group_allocs[i].position;
835           avail_size = group_allocs[i].size;
836
837           sizes = g_new (GtkRequestedSize, visible_cells);
838
839           for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
840             {
841               CellInfo *info = cell_list->data;
842
843               if (!gtk_cell_renderer_get_visible (info->renderer))
844                 continue;
845
846               gtk_cell_area_request_renderer (area, info->renderer,
847                                               priv->orientation,
848                                               widget, for_size,
849                                               &sizes[j].minimum_size,
850                                               &sizes[j].natural_size);
851
852               sizes[j].data = info;
853               avail_size   -= sizes[j].minimum_size;
854
855               j++;
856             }
857
858           /* Distribute cells naturally within the group */
859           avail_size -= (visible_cells - 1) * priv->spacing;
860           if (avail_size > 0)
861             avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
862           else
863             avail_size = 0;
864
865           /* Calculate/distribute expand for cells */
866           if (expand_cells > 0)
867             {
868               extra_size  = avail_size / expand_cells;
869               extra_extra = avail_size % expand_cells;
870             }
871           else
872             extra_size = extra_extra = 0;
873
874           /* Create the allocated cells (loop only over visible cells here) */
875           for (j = 0; j < visible_cells; j++)
876             {
877               CellInfo      *info = sizes[j].data;
878               AllocatedCell *cell;
879
880               if (info->expand)
881                 {
882                   sizes[j].minimum_size += extra_size;
883                   if (extra_extra)
884                     {
885                       sizes[j].minimum_size++;
886                       extra_extra--;
887                     }
888                 }
889               
890               if (rtl)
891                 cell = allocated_cell_new (info->renderer, 
892                                            full_size - (position + sizes[j].minimum_size),
893                                            sizes[j].minimum_size);
894               else
895                 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
896
897               allocated_cells = g_slist_prepend (allocated_cells, cell);
898               
899               position += sizes[j].minimum_size;
900               position += priv->spacing;
901             }
902
903           g_free (sizes);
904         }
905     }
906
907   /* Note it might not be important to reverse the list here at all,
908    * we have the correct positions, no need to allocate from left to right */
909   return g_slist_reverse (allocated_cells);
910 }
911
912
913 static void
914 gtk_cell_area_box_focus_changed (GtkCellArea        *area,
915                                  GParamSpec         *pspec,
916                                  GtkCellAreaBox     *box)
917 {
918   if (gtk_cell_area_get_focus_cell (area))
919     box->priv->last_focus_cell = gtk_cell_area_get_focus_cell (area);
920 }
921
922 /*************************************************************
923  *                      GObjectClass                         *
924  *************************************************************/
925 static void
926 gtk_cell_area_box_finalize (GObject *object)
927 {
928   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (object);
929   GtkCellAreaBoxPrivate *priv = box->priv;
930   GSList                *l;
931
932   /* Unref/free the context list */
933   for (l = priv->contexts; l; l = l->next)
934     g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
935
936   g_slist_free (priv->contexts);
937   priv->contexts = NULL;
938
939   /* Free the cell grouping info */
940   cell_groups_clear (box);
941   g_array_free (priv->groups, TRUE);
942   
943   G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
944 }
945
946 static void
947 gtk_cell_area_box_dispose (GObject *object)
948 {
949   G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
950 }
951
952 static void
953 gtk_cell_area_box_set_property (GObject       *object,
954                                 guint          prop_id,
955                                 const GValue  *value,
956                                 GParamSpec    *pspec)
957 {
958   GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
959
960   switch (prop_id)
961     {
962     case PROP_ORIENTATION:
963       box->priv->orientation = g_value_get_enum (value);
964
965       /* Notify that size needs to be requested again */
966       reset_contexts (box);
967       break;
968     case PROP_SPACING:
969       gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
970       break;
971     default:
972       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
973       break;
974     }
975 }
976
977 static void
978 gtk_cell_area_box_get_property (GObject     *object,
979                                 guint        prop_id,
980                                 GValue      *value,
981                                 GParamSpec  *pspec)
982 {
983   GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
984
985   switch (prop_id)
986     {
987     case PROP_ORIENTATION:
988       g_value_set_enum (value, box->priv->orientation);
989       break;
990     case PROP_SPACING:
991       g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
992       break;
993     default:
994       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
995       break;
996     }
997 }
998
999 /*************************************************************
1000  *                    GtkCellAreaClass                       *
1001  *************************************************************/
1002 static void      
1003 gtk_cell_area_box_add (GtkCellArea        *area,
1004                        GtkCellRenderer    *renderer)
1005 {
1006   gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
1007                                 renderer, FALSE, TRUE);
1008 }
1009
1010 static void
1011 gtk_cell_area_box_remove (GtkCellArea        *area,
1012                           GtkCellRenderer    *renderer)
1013 {
1014   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1015   GtkCellAreaBoxPrivate *priv = box->priv;
1016   GList                 *node;
1017
1018   if (priv->last_focus_cell == renderer)
1019     priv->last_focus_cell = NULL;
1020
1021   node = g_list_find_custom (priv->cells, renderer, 
1022                              (GCompareFunc)cell_info_find);
1023
1024   if (node)
1025     {
1026       CellInfo *info = node->data;
1027
1028       cell_info_free (info);
1029
1030       priv->cells = g_list_delete_link (priv->cells, node);
1031
1032       /* Reconstruct cell groups */
1033       cell_groups_rebuild (box);
1034     }
1035   else
1036     g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1037 }
1038
1039 static void
1040 gtk_cell_area_box_foreach (GtkCellArea        *area,
1041                            GtkCellCallback     callback,
1042                            gpointer            callback_data)
1043 {
1044   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1045   GtkCellAreaBoxPrivate *priv = box->priv;
1046   GList                 *list;
1047
1048   for (list = priv->cells; list; list = list->next)
1049     {
1050       CellInfo *info = list->data;
1051
1052       if (callback (info->renderer, callback_data))
1053         break;
1054     }
1055 }
1056
1057 static void
1058 gtk_cell_area_box_foreach_alloc (GtkCellArea          *area,
1059                                  GtkCellAreaContext   *context,
1060                                  GtkWidget            *widget,
1061                                  const GdkRectangle   *cell_area,
1062                                  const GdkRectangle   *background_area,
1063                                  GtkCellAllocCallback  callback,
1064                                  gpointer              callback_data)
1065 {
1066   GtkCellAreaBox        *box      = GTK_CELL_AREA_BOX (area);
1067   GtkCellAreaBoxPrivate *priv     = box->priv;
1068   GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1069   GSList                *allocated_cells, *l;
1070   GdkRectangle           cell_alloc, cell_background;
1071   gboolean               rtl;
1072
1073   rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1074          gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1075
1076   cell_alloc = *cell_area;
1077
1078   /* Get a list of cells with allocation sizes decided regardless
1079    * of alignments and pack order etc. */
1080   allocated_cells = get_allocated_cells (box, box_context, widget, 
1081                                          cell_area->width, cell_area->height);
1082
1083   for (l = allocated_cells; l; l = l->next)
1084     {
1085       AllocatedCell *cell = l->data;
1086
1087       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1088         {
1089           cell_alloc.x     = cell_area->x + cell->position;
1090           cell_alloc.width = cell->size;
1091         }
1092       else
1093         {
1094           cell_alloc.y      = cell_area->y + cell->position;
1095           cell_alloc.height = cell->size;
1096         }
1097
1098       /* Stop iterating over cells if they flow out of the render area,
1099        * this can happen because the render area can actually be
1100        * smaller than the requested area (treeview columns can
1101        * be user resizable and can be resized to be smaller than
1102        * the actual requested area). */
1103       if (cell_alloc.x > cell_area->x + cell_area->width ||
1104           cell_alloc.x + cell_alloc.width < cell_area->x ||
1105           cell_alloc.y > cell_area->y + cell_area->height)
1106         break;
1107
1108       /* Special case for the last cell (or first cell in rtl)... let the last cell consume 
1109        * the remaining space in the area (the last cell is allowed to consume the remaining 
1110        * space if the space given for rendering is actually larger than allocation, this can
1111        * happen in the expander GtkTreeViewColumn where only the deepest depth column
1112        * receives the allocation... shallow columns recieve more width). */
1113       if (!l->next)
1114         {
1115           if (rtl)
1116             {
1117               /* Fill the leading space for the first cell in the area (still last in the list) */
1118               cell_alloc.width = (cell_alloc.x - cell_area->x) + cell_alloc.width;
1119               cell_alloc.x     = cell_area->x;
1120             }
1121           else
1122             {
1123               cell_alloc.width  = cell_area->x + cell_area->width  - cell_alloc.x;
1124               cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1125             }
1126         }
1127       else
1128         {
1129           /* If the cell we are rendering doesnt fit into the remaining space, clip it
1130            * so that the underlying renderer has a chance to deal with it (for instance
1131            * text renderers get a chance to ellipsize).
1132            */
1133           if (cell_alloc.x + cell_alloc.width > cell_area->x + cell_area->width)
1134             cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1135
1136           if (cell_alloc.y + cell_alloc.height > cell_area->y + cell_area->height)
1137             cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1138         }
1139
1140       /* Add portions of the background_area to the cell_alloc
1141        * to create the cell_background */
1142       cell_background = cell_alloc;
1143
1144       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1145         {
1146           if (l == allocated_cells)
1147             {
1148               cell_background.width += cell_background.x - background_area->x;
1149               cell_background.x      = background_area->x;
1150             }
1151
1152           if (l->next == NULL)
1153               cell_background.width = 
1154                 background_area->width - (cell_background.x - background_area->x);
1155
1156           cell_background.y      = background_area->y;
1157           cell_background.height = background_area->height;
1158         }
1159       else
1160         {
1161           if (l == allocated_cells)
1162             {
1163               cell_background.height += cell_background.y - background_area->y;
1164               cell_background.y       = background_area->y;
1165             }
1166
1167           if (l->next == NULL)
1168               cell_background.height = 
1169                 background_area->height - (cell_background.y - background_area->y);
1170
1171           cell_background.x     = background_area->x;
1172           cell_background.width = background_area->width;
1173         }
1174
1175       if (callback (cell->renderer, &cell_alloc, &cell_background, callback_data))
1176         break;
1177     }
1178
1179   g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1180   g_slist_free (allocated_cells);
1181 }
1182
1183 static void
1184 gtk_cell_area_box_set_cell_property (GtkCellArea        *area,
1185                                      GtkCellRenderer    *renderer,
1186                                      guint               prop_id,
1187                                      const GValue       *value,
1188                                      GParamSpec         *pspec)
1189 {
1190   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area); 
1191   GtkCellAreaBoxPrivate *priv = box->priv;
1192   GList                 *node;
1193   CellInfo              *info;
1194   gboolean               rebuild = FALSE;
1195   gboolean               val;
1196   GtkPackType            pack_type;
1197
1198   node = g_list_find_custom (priv->cells, renderer, 
1199                              (GCompareFunc)cell_info_find);
1200   if (!node)
1201     return;
1202
1203   info = node->data;
1204
1205   switch (prop_id)
1206     {
1207     case CELL_PROP_EXPAND:
1208       val = g_value_get_boolean (value);
1209
1210       if (info->expand != val)
1211         {
1212           info->expand = val;
1213           rebuild      = TRUE;
1214         }
1215       break;
1216
1217     case CELL_PROP_ALIGN:
1218       val = g_value_get_boolean (value);
1219
1220       if (info->align != val)
1221         {
1222           info->align = val;
1223           rebuild     = TRUE;
1224         }
1225       break;
1226
1227     case CELL_PROP_PACK_TYPE:
1228       pack_type = g_value_get_enum (value);
1229
1230       if (info->pack != pack_type)
1231         {
1232           info->pack = pack_type;
1233           rebuild    = TRUE;
1234         }
1235       break;
1236     default:
1237       GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1238       break;
1239     }
1240
1241   /* Groups need to be rebuilt */
1242   if (rebuild)
1243     cell_groups_rebuild (box);
1244 }
1245
1246 static void
1247 gtk_cell_area_box_get_cell_property (GtkCellArea        *area,
1248                                      GtkCellRenderer    *renderer,
1249                                      guint               prop_id,
1250                                      GValue             *value,
1251                                      GParamSpec         *pspec)
1252 {
1253   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area); 
1254   GtkCellAreaBoxPrivate *priv = box->priv;
1255   GList                 *node;
1256   CellInfo              *info;
1257
1258   node = g_list_find_custom (priv->cells, renderer, 
1259                              (GCompareFunc)cell_info_find);
1260   if (!node)
1261     return;
1262
1263   info = node->data;
1264
1265   switch (prop_id)
1266     {
1267     case CELL_PROP_EXPAND:
1268       g_value_set_boolean (value, info->expand);
1269       break;
1270
1271     case CELL_PROP_ALIGN:
1272       g_value_set_boolean (value, info->align);
1273       break;
1274
1275     case CELL_PROP_PACK_TYPE:
1276       g_value_set_enum (value, info->pack);
1277       break;
1278     default:
1279       GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1280       break;
1281     }
1282 }
1283
1284
1285 static GtkCellAreaContext *
1286 gtk_cell_area_box_create_context (GtkCellArea *area)
1287 {
1288   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1289   GtkCellAreaBoxPrivate *priv = box->priv;
1290   GtkCellAreaContext    *context =
1291     (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT, 
1292                                      "area", area, NULL);
1293
1294   priv->contexts = g_slist_prepend (priv->contexts, context);
1295
1296   g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1297
1298   /* Tell the new group about our cell layout */
1299   init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1300
1301   return context;
1302 }
1303
1304 static GtkSizeRequestMode 
1305 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1306 {
1307   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1308   GtkCellAreaBoxPrivate *priv = box->priv;
1309
1310   return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1311     GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1312     GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1313 }
1314
1315 static void
1316 compute_size (GtkCellAreaBox        *box,
1317               GtkOrientation         orientation,
1318               GtkCellAreaBoxContext *context,
1319               GtkWidget             *widget,
1320               gint                   for_size,
1321               gint                  *minimum_size,
1322               gint                  *natural_size)
1323 {
1324   GtkCellAreaBoxPrivate *priv = box->priv;
1325   GtkCellArea           *area = GTK_CELL_AREA (box);
1326   GList                 *list;
1327   gint                   i;
1328   gint                   min_size = 0;
1329   gint                   nat_size = 0;
1330   
1331   for (i = 0; i < priv->groups->len; i++)
1332     {
1333       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1334       gint       group_min_size = 0;
1335       gint       group_nat_size = 0;
1336
1337       for (list = group->cells; list; list = list->next)
1338         {
1339           CellInfo *info = list->data;
1340           gint      renderer_min_size, renderer_nat_size;
1341
1342           if (!gtk_cell_renderer_get_visible (info->renderer))
1343               continue;
1344           
1345           gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size, 
1346                                           &renderer_min_size, &renderer_nat_size);
1347
1348           if (orientation == priv->orientation)
1349             {
1350               if (min_size > 0)
1351                 {
1352                   min_size += priv->spacing;
1353                   nat_size += priv->spacing;
1354                 }
1355               
1356               if (group_min_size > 0)
1357                 {
1358                   group_min_size += priv->spacing;
1359                   group_nat_size += priv->spacing;
1360                 }
1361               
1362               min_size       += renderer_min_size;
1363               nat_size       += renderer_nat_size;
1364               group_min_size += renderer_min_size;
1365               group_nat_size += renderer_nat_size;
1366             }
1367           else
1368             {
1369               min_size       = MAX (min_size, renderer_min_size);
1370               nat_size       = MAX (nat_size, renderer_nat_size);
1371               group_min_size = MAX (group_min_size, renderer_min_size);
1372               group_nat_size = MAX (group_nat_size, renderer_nat_size);
1373             }
1374         }
1375
1376       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1377         {
1378           if (for_size < 0)
1379             gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1380           else
1381             gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1382                                                                    group_min_size, group_nat_size);
1383         }
1384       else
1385         {
1386           if (for_size < 0)
1387             gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1388           else
1389             gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1390                                                                    group_min_size, group_nat_size);
1391         }
1392     }
1393
1394   *minimum_size = min_size;
1395   *natural_size = nat_size;
1396
1397   /* Update rtl state for focus navigation to work */
1398   priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1399                gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1400 }
1401
1402 GtkRequestedSize *
1403 get_group_sizes (GtkCellArea    *area,
1404                  CellGroup      *group,
1405                  GtkOrientation  orientation,
1406                  GtkWidget      *widget,
1407                  gint           *n_sizes)
1408 {
1409   GtkRequestedSize *sizes;
1410   GList            *l;
1411   gint              i;
1412
1413   *n_sizes = count_visible_cells (group, NULL);
1414   sizes    = g_new (GtkRequestedSize, *n_sizes);
1415
1416   for (l = group->cells, i = 0; l; l = l->next)
1417     {
1418       CellInfo *info = l->data;
1419
1420       if (!gtk_cell_renderer_get_visible (info->renderer))
1421         continue;
1422
1423       sizes[i].data = info;
1424       
1425       gtk_cell_area_request_renderer (area, info->renderer,
1426                                       orientation, widget, -1,
1427                                       &sizes[i].minimum_size,
1428                                       &sizes[i].natural_size);
1429
1430       i++;
1431     }
1432
1433   return sizes;
1434 }
1435
1436 static void
1437 compute_group_size_for_opposing_orientation (GtkCellAreaBox     *box,
1438                                              CellGroup          *group,
1439                                              GtkWidget          *widget, 
1440                                              gint                for_size,
1441                                              gint               *minimum_size, 
1442                                              gint               *natural_size)
1443 {
1444   GtkCellAreaBoxPrivate *priv = box->priv;
1445   GtkCellArea           *area = GTK_CELL_AREA (box);
1446
1447   /* Exception for single cell groups */
1448   if (group->n_cells == 1)
1449     {
1450       CellInfo *info = group->cells->data;
1451
1452       gtk_cell_area_request_renderer (area, info->renderer,
1453                                       OPPOSITE_ORIENTATION (priv->orientation),
1454                                       widget, for_size, minimum_size, natural_size);
1455     }
1456   else
1457     {
1458       GtkRequestedSize *orientation_sizes;
1459       CellInfo         *info;
1460       gint              n_sizes, i;
1461       gint              avail_size     = for_size;
1462       gint              extra_size, extra_extra;
1463       gint              min_size = 0, nat_size = 0;
1464
1465       orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1466
1467       /* First naturally allocate the cells in the group into the for_size */
1468       avail_size -= (n_sizes - 1) * priv->spacing;
1469       for (i = 0; i < n_sizes; i++)
1470         avail_size -= orientation_sizes[i].minimum_size;
1471
1472       if (avail_size > 0)
1473         avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1474       else
1475         avail_size = 0;
1476
1477       /* Calculate/distribute expand for cells */
1478       if (group->expand_cells > 0)
1479         {
1480           extra_size  = avail_size / group->expand_cells;
1481           extra_extra = avail_size % group->expand_cells;
1482         }
1483       else
1484         extra_size = extra_extra = 0;
1485
1486       for (i = 0; i < n_sizes; i++)
1487         {
1488           gint cell_min, cell_nat;
1489
1490           info = orientation_sizes[i].data;
1491
1492           if (info->expand)
1493             {
1494               orientation_sizes[i].minimum_size += extra_size;
1495               if (extra_extra)
1496                 {
1497                   orientation_sizes[i].minimum_size++;
1498                   extra_extra--;
1499                 }
1500             }
1501
1502           gtk_cell_area_request_renderer (area, info->renderer,
1503                                           OPPOSITE_ORIENTATION (priv->orientation),
1504                                           widget, 
1505                                           orientation_sizes[i].minimum_size,
1506                                           &cell_min, &cell_nat);
1507
1508           min_size = MAX (min_size, cell_min);
1509           nat_size = MAX (nat_size, cell_nat);
1510         }
1511
1512       *minimum_size = min_size;
1513       *natural_size = nat_size;
1514
1515       g_free (orientation_sizes);
1516     }
1517 }
1518
1519 static void
1520 compute_size_for_opposing_orientation (GtkCellAreaBox        *box, 
1521                                        GtkCellAreaBoxContext *context, 
1522                                        GtkWidget             *widget, 
1523                                        gint                   for_size,
1524                                        gint                  *minimum_size, 
1525                                        gint                  *natural_size)
1526 {
1527   GtkCellAreaBoxPrivate *priv = box->priv;
1528   CellGroup             *group;
1529   GtkRequestedSize      *orientation_sizes;
1530   gint                   n_groups, n_expand_groups, i;
1531   gint                   avail_size = for_size;
1532   gint                   extra_size, extra_extra;
1533   gint                   min_size = 0, nat_size = 0;
1534
1535   n_expand_groups = count_expand_groups (box);
1536
1537   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1538     orientation_sizes = gtk_cell_area_box_context_get_widths (context, &n_groups);
1539   else
1540     orientation_sizes = gtk_cell_area_box_context_get_heights (context, &n_groups);
1541
1542   /* First start by naturally allocating space among groups of cells */
1543   avail_size -= (n_groups - 1) * priv->spacing;
1544   for (i = 0; i < n_groups; i++)
1545     avail_size -= orientation_sizes[i].minimum_size;
1546
1547   if (avail_size > 0)
1548     avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1549   else
1550     avail_size = 0;
1551
1552   /* Calculate/distribute expand for groups */
1553   if (n_expand_groups > 0)
1554     {
1555       extra_size  = avail_size / n_expand_groups;
1556       extra_extra = avail_size % n_expand_groups;
1557     }
1558   else
1559     extra_size = extra_extra = 0;
1560
1561   /* Now we need to naturally allocate sizes for cells in each group
1562    * and push the height-for-width for each group accordingly while accumulating
1563    * the overall height-for-width for this row.
1564    */
1565   for (i = 0; i < n_groups; i++)
1566     {
1567       gint group_min, group_nat;
1568       gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1569       
1570       group = &g_array_index (priv->groups, CellGroup, group_idx);
1571
1572       if (group->expand_cells > 0)
1573         {
1574           orientation_sizes[i].minimum_size += extra_size;
1575           if (extra_extra)
1576             {
1577               orientation_sizes[i].minimum_size++;
1578               extra_extra--;
1579             }
1580         }
1581
1582       /* Now we have the allocation for the group, request it's height-for-width */
1583       compute_group_size_for_opposing_orientation (box, group, widget,
1584                                                    orientation_sizes[i].minimum_size,
1585                                                    &group_min, &group_nat);
1586
1587       min_size = MAX (min_size, group_min);
1588       nat_size = MAX (nat_size, group_nat);
1589
1590       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1591         {
1592           gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1593                                                                  group_min, group_nat);
1594         }
1595       else
1596         {
1597           gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1598                                                                  group_min, group_nat);
1599         }
1600     }
1601
1602   *minimum_size = min_size;
1603   *natural_size = nat_size;
1604
1605   g_free (orientation_sizes);
1606
1607   /* Update rtl state for focus navigation to work */
1608   priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1609                gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1610 }
1611
1612
1613
1614 static void
1615 gtk_cell_area_box_get_preferred_width (GtkCellArea        *area,
1616                                        GtkCellAreaContext *context,
1617                                        GtkWidget          *widget,
1618                                        gint               *minimum_width,
1619                                        gint               *natural_width)
1620 {
1621   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1622   GtkCellAreaBoxContext *box_context;
1623   gint                   min_width, nat_width;
1624
1625   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1626
1627   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1628
1629   /* Compute the size of all renderers for current row data, 
1630    * bumping cell alignments in the context along the way */
1631   compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1632                 box_context, widget, -1, &min_width, &nat_width);
1633
1634   if (minimum_width)
1635     *minimum_width = min_width;
1636
1637   if (natural_width)
1638     *natural_width = nat_width;
1639 }
1640
1641 static void
1642 gtk_cell_area_box_get_preferred_height (GtkCellArea        *area,
1643                                         GtkCellAreaContext *context,
1644                                         GtkWidget          *widget,
1645                                         gint               *minimum_height,
1646                                         gint               *natural_height)
1647 {
1648   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1649   GtkCellAreaBoxContext *box_context;
1650   gint                   min_height, nat_height;
1651
1652   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1653
1654   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1655
1656   /* Compute the size of all renderers for current row data, 
1657    * bumping cell alignments in the context along the way */
1658   compute_size (box, GTK_ORIENTATION_VERTICAL,
1659                 box_context, widget, -1, &min_height, &nat_height);
1660
1661   if (minimum_height)
1662     *minimum_height = min_height;
1663
1664   if (natural_height)
1665     *natural_height = nat_height;
1666 }
1667
1668 static void
1669 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea        *area,
1670                                                   GtkCellAreaContext *context,
1671                                                   GtkWidget          *widget,
1672                                                   gint                width,
1673                                                   gint               *minimum_height,
1674                                                   gint               *natural_height)
1675 {
1676   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1677   GtkCellAreaBoxContext *box_context;
1678   GtkCellAreaBoxPrivate *priv;
1679   gint                   min_height, nat_height;
1680
1681   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1682
1683   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1684   priv        = box->priv;
1685
1686   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1687     {
1688       /* Add up vertical requests of height for width and push the overall 
1689        * cached sizes for alignments */
1690       compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1691     }
1692   else
1693     {
1694       /* Juice: virtually allocate cells into the for_width using the 
1695        * alignments and then return the overall height for that width, and cache it */
1696       compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1697     }
1698
1699   if (minimum_height)
1700     *minimum_height = min_height;
1701
1702   if (natural_height)
1703     *natural_height = nat_height;
1704 }
1705
1706 static void
1707 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea        *area,
1708                                                   GtkCellAreaContext *context,
1709                                                   GtkWidget          *widget,
1710                                                   gint                height,
1711                                                   gint               *minimum_width,
1712                                                   gint               *natural_width)
1713 {
1714   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1715   GtkCellAreaBoxContext *box_context;
1716   GtkCellAreaBoxPrivate *priv;
1717   gint                   min_width, nat_width;
1718
1719   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1720
1721   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1722   priv        = box->priv;
1723
1724   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1725     {
1726       /* Add up horizontal requests of width for height and push the overall 
1727        * cached sizes for alignments */
1728       compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1729     }
1730   else
1731     {
1732       /* Juice: horizontally allocate cells into the for_height using the 
1733        * alignments and then return the overall width for that height, and cache it */
1734       compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1735     }
1736
1737   if (minimum_width)
1738     *minimum_width = min_width;
1739
1740   if (natural_width)
1741     *natural_width = nat_width;
1742 }
1743
1744 enum {
1745   FOCUS_NONE,
1746   FOCUS_PREV,
1747   FOCUS_NEXT,
1748   FOCUS_LAST_CELL
1749 };
1750
1751 static gboolean
1752 gtk_cell_area_box_focus (GtkCellArea      *area,
1753                          GtkDirectionType  direction)
1754 {
1755   GtkCellAreaBox        *box   = GTK_CELL_AREA_BOX (area);
1756   GtkCellAreaBoxPrivate *priv  = box->priv;
1757   gint                   cycle = FOCUS_NONE;
1758   gboolean               cycled_focus = FALSE;
1759   GtkCellRenderer       *focus_cell;
1760
1761   focus_cell = gtk_cell_area_get_focus_cell (area);
1762
1763   /* Special case, when there is no activatable cell, focus
1764    * is painted around the entire area... in this case we
1765    * let focus leave the area directly.
1766    */
1767   if (focus_cell && !gtk_cell_area_is_activatable (area))
1768     {
1769       gtk_cell_area_set_focus_cell (area, NULL);
1770       return FALSE;
1771     }
1772
1773   switch (direction)
1774     {
1775     case GTK_DIR_TAB_FORWARD:
1776       cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1777       break;
1778     case GTK_DIR_TAB_BACKWARD:
1779       cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1780       break;
1781     case GTK_DIR_UP: 
1782       if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1783         cycle = FOCUS_PREV;
1784       else if (!focus_cell)
1785         cycle = FOCUS_LAST_CELL;
1786       break;
1787     case GTK_DIR_DOWN:
1788       if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1789         cycle = FOCUS_NEXT;
1790       else if (!focus_cell)
1791         cycle = FOCUS_LAST_CELL;
1792       break;
1793     case GTK_DIR_LEFT:
1794       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1795         cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1796       else if (!focus_cell)
1797         cycle = FOCUS_LAST_CELL;
1798       break;
1799     case GTK_DIR_RIGHT:
1800       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1801         cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1802       else if (!focus_cell)
1803         cycle = FOCUS_LAST_CELL;
1804       break;
1805     default:
1806       break;
1807     }
1808
1809   if (cycle == FOCUS_LAST_CELL)
1810     {
1811       gtk_cell_area_set_focus_cell (area, priv->last_focus_cell);
1812       cycled_focus = TRUE;
1813     }
1814   else if (cycle != FOCUS_NONE)
1815     {
1816       gboolean  found_cell = FALSE;
1817       GList    *list;
1818       gint      i;
1819
1820       /* If there is no focused cell, focus on the first (or last) one in the list */
1821       if (!focus_cell)
1822         found_cell = TRUE;
1823
1824       for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1; 
1825            cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
1826            i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
1827         {
1828           CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1829           
1830           for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells); 
1831                cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
1832             {
1833               CellInfo *info = list->data;
1834
1835               if (info->renderer == focus_cell)
1836                 found_cell = TRUE;
1837               else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
1838                        gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
1839                 {
1840                   gtk_cell_area_set_focus_cell (area, info->renderer);
1841                   cycled_focus = TRUE;
1842                 }
1843             }
1844         }
1845     }
1846
1847   if (!cycled_focus)
1848     gtk_cell_area_set_focus_cell (area, NULL);
1849
1850   return cycled_focus;
1851 }
1852
1853
1854 /*************************************************************
1855  *                    GtkCellLayoutIface                     *
1856  *************************************************************/
1857 static void
1858 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1859 {
1860   iface->pack_start = gtk_cell_area_box_layout_pack_start;
1861   iface->pack_end   = gtk_cell_area_box_layout_pack_end;
1862   iface->reorder    = gtk_cell_area_box_layout_reorder;
1863 }
1864
1865 static void
1866 gtk_cell_area_box_layout_pack_start (GtkCellLayout      *cell_layout,
1867                                      GtkCellRenderer    *renderer,
1868                                      gboolean            expand)
1869 {
1870   gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1871 }
1872
1873 static void
1874 gtk_cell_area_box_layout_pack_end (GtkCellLayout      *cell_layout,
1875                                    GtkCellRenderer    *renderer,
1876                                    gboolean            expand)
1877 {
1878   gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1879 }
1880
1881 static void
1882 gtk_cell_area_box_layout_reorder (GtkCellLayout      *cell_layout,
1883                                   GtkCellRenderer    *renderer,
1884                                   gint                position)
1885 {
1886   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (cell_layout);
1887   GtkCellAreaBoxPrivate *priv = box->priv;
1888   GList                 *node;
1889   CellInfo              *info;
1890   
1891   node = g_list_find_custom (priv->cells, renderer, 
1892                              (GCompareFunc)cell_info_find);
1893
1894   if (node)
1895     {
1896       info = node->data;
1897
1898       priv->cells = g_list_delete_link (priv->cells, node);
1899       priv->cells = g_list_insert (priv->cells, info, position);
1900
1901       cell_groups_rebuild (box);
1902     }
1903 }
1904
1905 /*************************************************************
1906  *                            API                            *
1907  *************************************************************/
1908 /**
1909  * gtk_cell_area_box_new:
1910  * 
1911  * Creates a new #GtkCellAreaBox.
1912  *
1913  * Return value: a newly created #GtkCellAreaBox
1914  */
1915 GtkCellArea *
1916 gtk_cell_area_box_new (void)
1917 {
1918   return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
1919 }
1920
1921 /**
1922  * gtk_cell_area_box_pack_start:
1923  * @box: a #GtkCellAreaBox
1924  * @renderer: the #GtkCellRenderer to add
1925  * @expand: whether @renderer should receive extra space when the area receives
1926  * more than its natural size
1927  * @align: whether @renderer should be aligned in adjacent rows.
1928  *
1929  * Adds @renderer to @box, packed with reference to the start of @box.
1930  *
1931  * The @renderer is packed after any other #GtkCellRenderer packed with reference
1932  * to the start of @box.
1933  *
1934  * Since: 3.0
1935  */
1936 void
1937 gtk_cell_area_box_pack_start  (GtkCellAreaBox  *box,
1938                                GtkCellRenderer *renderer,
1939                                gboolean         expand,
1940                                gboolean         align)
1941 {
1942   GtkCellAreaBoxPrivate *priv;
1943   CellInfo              *info;
1944
1945   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1946   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1947
1948   priv = box->priv;
1949
1950   if (g_list_find_custom (priv->cells, renderer, 
1951                           (GCompareFunc)cell_info_find))
1952     {
1953       g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
1954       return;
1955     }
1956
1957   info = cell_info_new (renderer, GTK_PACK_START, expand, align);
1958
1959   priv->cells = g_list_append (priv->cells, info);
1960
1961   cell_groups_rebuild (box);
1962 }
1963
1964 /**
1965  * gtk_cell_area_box_pack_end:
1966  * @box: a #GtkCellAreaBox
1967  * @renderer: the #GtkCellRenderer to add
1968  * @expand: whether @renderer should receive extra space when the area receives
1969  * more than its natural size
1970  * @align: whether @renderer should be aligned in adjacent rows.
1971  *
1972  * Adds @renderer to @box, packed with reference to the end of @box.
1973  *
1974  * The @renderer is packed after (away from end of) any other #GtkCellRenderer
1975  * packed with reference to the end of @box.
1976  *
1977  * Since: 3.0
1978  */
1979 void
1980 gtk_cell_area_box_pack_end (GtkCellAreaBox  *box,
1981                             GtkCellRenderer *renderer,
1982                             gboolean         expand, 
1983                             gboolean         align)
1984 {
1985   GtkCellAreaBoxPrivate *priv;
1986   CellInfo              *info;
1987
1988   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1989   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1990
1991   priv = box->priv;
1992
1993   if (g_list_find_custom (priv->cells, renderer, 
1994                           (GCompareFunc)cell_info_find))
1995     {
1996       g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
1997       return;
1998     }
1999
2000   info = cell_info_new (renderer, GTK_PACK_END, expand, align);
2001
2002   priv->cells = g_list_append (priv->cells, info);
2003
2004   cell_groups_rebuild (box);
2005 }
2006
2007 /**
2008  * gtk_cell_area_box_get_spacing:
2009  * @box: a #GtkCellAreaBox
2010  *
2011  * Gets the spacing added between cell renderers.
2012  *
2013  * Return value: the space added between cell renderers in @box.
2014  *
2015  * Since: 3.0
2016  */
2017 gint
2018 gtk_cell_area_box_get_spacing (GtkCellAreaBox  *box)
2019 {
2020   g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2021
2022   return box->priv->spacing;
2023 }
2024
2025 /**
2026  * gtk_cell_area_box_set_spacing:
2027  * @box: a #GtkCellAreaBox
2028  * @spacing: the space to add between #GtkCellRenderers
2029  *
2030  * Sets the spacing to add between cell renderers in @box.
2031  *
2032  * Since: 3.0
2033  */
2034 void
2035 gtk_cell_area_box_set_spacing (GtkCellAreaBox  *box,
2036                                gint             spacing)
2037 {
2038   GtkCellAreaBoxPrivate *priv;
2039
2040   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2041
2042   priv = box->priv;
2043
2044   if (priv->spacing != spacing)
2045     {
2046       priv->spacing = spacing;
2047
2048       g_object_notify (G_OBJECT (box), "spacing");
2049
2050       /* Notify that size needs to be requested again */
2051       reset_contexts (box);
2052     }
2053 }