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