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