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