]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellareabox.c
wayland: Remove non-existing gdkscreen-wayland.h from SOURCES
[~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   /* We hold on to the previously focused cell when navigating
205    * up and down in a horizontal box (or left and right on a vertical one)
206    * this way we always re-enter the last focused cell.
207    */
208   GtkCellRenderer *last_focus_cell;
209   gulong           focus_cell_id;
210
211   GList           *cells;
212   GArray          *groups;
213
214   GSList          *contexts;
215
216   GtkOrientation   orientation;
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 resetting 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 (!gtk_cell_renderer_get_visible (info->renderer))
869             continue;
870
871           /* If were not aligned, place the cell after the last cell */
872           if (info->align)
873             position = cell_position = group_allocs[i].position;
874           else
875             cell_position = position;
876
877           /* If not a fixed size, use only the requested size for this row */
878           if (info->fixed)
879             cell_size = group_allocs[i].size;
880           else
881             {
882               gint dummy;
883               gtk_cell_area_request_renderer (area, info->renderer,
884                                               priv->orientation,
885                                               widget, for_size,
886                                               &dummy,
887                                               &cell_size);
888               cell_size = MIN (cell_size, group_allocs[i].size);
889             }
890
891           if (rtl)
892             cell = allocated_cell_new (info->renderer,
893                                        full_size - (cell_position + cell_size), cell_size);
894           else
895             cell = allocated_cell_new (info->renderer, cell_position, cell_size);
896
897           position += cell_size;
898           position += priv->spacing;
899
900           allocated_cells = g_slist_prepend (allocated_cells, cell);
901         }
902       else
903         {
904           GtkRequestedSize *sizes;
905           gint              avail_size, cell_position;
906           gint              visible_cells, expand_cells;
907           gint              extra_size, extra_extra;
908
909           visible_cells = count_visible_cells (group, &expand_cells);
910
911           /* If this row has no visible cells in this group, just
912            * skip the allocation
913            */
914           if (visible_cells == 0)
915             continue;
916
917           /* If were not aligned, place the cell after the last cell 
918            * and eat up the extra space
919            */
920           if (group->align)
921             {
922               avail_size = group_allocs[i].size;
923               position   = cell_position = group_allocs[i].position;
924             }
925           else
926             {
927               avail_size    = group_allocs[i].size + (group_allocs[i].position - position);
928               cell_position = position;
929             }
930
931           sizes = g_new (GtkRequestedSize, visible_cells);
932
933           for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
934             {
935               CellInfo *info = cell_list->data;
936
937               if (!gtk_cell_renderer_get_visible (info->renderer))
938                 continue;
939
940               gtk_cell_area_request_renderer (area, info->renderer,
941                                               priv->orientation,
942                                               widget, for_size,
943                                               &sizes[j].minimum_size,
944                                               &sizes[j].natural_size);
945
946               sizes[j].data = info;
947               avail_size   -= sizes[j].minimum_size;
948
949               j++;
950             }
951
952           /* Distribute cells naturally within the group */
953           avail_size -= (visible_cells - 1) * priv->spacing;
954           if (avail_size > 0)
955             avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
956           else
957             avail_size = 0;
958
959           /* Calculate/distribute expand for cells */
960           if (expand_cells > 0)
961             {
962               extra_size  = avail_size / expand_cells;
963               extra_extra = avail_size % expand_cells;
964             }
965           else
966             extra_size = extra_extra = 0;
967
968           /* Create the allocated cells (loop only over visible cells here) */
969           for (j = 0; j < visible_cells; j++)
970             {
971               CellInfo      *info = sizes[j].data;
972               AllocatedCell *cell;
973
974               if (info->expand)
975                 {
976                   sizes[j].minimum_size += extra_size;
977                   if (extra_extra)
978                     {
979                       sizes[j].minimum_size++;
980                       extra_extra--;
981                     }
982                 }
983
984               if (rtl)
985                 cell = allocated_cell_new (info->renderer,
986                                            full_size - (cell_position + sizes[j].minimum_size),
987                                            sizes[j].minimum_size);
988               else
989                 cell = allocated_cell_new (info->renderer, cell_position, sizes[j].minimum_size);
990
991               allocated_cells = g_slist_prepend (allocated_cells, cell);
992
993               cell_position += sizes[j].minimum_size;
994               cell_position += priv->spacing;
995             }
996
997           g_free (sizes);
998
999           position = cell_position;
1000         }
1001     }
1002
1003   g_free (group_allocs);
1004
1005   /* Note it might not be important to reverse the list here at all,
1006    * we have the correct positions, no need to allocate from left to right
1007    */
1008   return g_slist_reverse (allocated_cells);
1009 }
1010
1011
1012 static void
1013 gtk_cell_area_box_focus_changed (GtkCellArea        *area,
1014                                  GParamSpec         *pspec,
1015                                  GtkCellAreaBox     *box)
1016 {
1017   if (gtk_cell_area_get_focus_cell (area))
1018     box->priv->last_focus_cell = gtk_cell_area_get_focus_cell (area);
1019 }
1020
1021 /*************************************************************
1022  *                      GObjectClass                         *
1023  *************************************************************/
1024 static void
1025 gtk_cell_area_box_finalize (GObject *object)
1026 {
1027   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (object);
1028   GtkCellAreaBoxPrivate *priv = box->priv;
1029   GSList                *l;
1030
1031   /* Unref/free the context list */
1032   for (l = priv->contexts; l; l = l->next)
1033     g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
1034
1035   g_slist_free (priv->contexts);
1036   priv->contexts = NULL;
1037
1038   /* Free the cell grouping info */
1039   cell_groups_clear (box);
1040   g_array_free (priv->groups, TRUE);
1041
1042   G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
1043 }
1044
1045 static void
1046 gtk_cell_area_box_dispose (GObject *object)
1047 {
1048   G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
1049 }
1050
1051 static void
1052 gtk_cell_area_box_set_property (GObject       *object,
1053                                 guint          prop_id,
1054                                 const GValue  *value,
1055                                 GParamSpec    *pspec)
1056 {
1057   GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1058
1059   switch (prop_id)
1060     {
1061     case PROP_ORIENTATION:
1062       box->priv->orientation = g_value_get_enum (value);
1063
1064       /* Notify that size needs to be requested again */
1065       reset_contexts (box);
1066
1067       break;
1068     case PROP_SPACING:
1069       gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
1070       break;
1071     default:
1072       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1073       break;
1074     }
1075 }
1076
1077 static void
1078 gtk_cell_area_box_get_property (GObject     *object,
1079                                 guint        prop_id,
1080                                 GValue      *value,
1081                                 GParamSpec  *pspec)
1082 {
1083   GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1084
1085   switch (prop_id)
1086     {
1087     case PROP_ORIENTATION:
1088       g_value_set_enum (value, box->priv->orientation);
1089       break;
1090     case PROP_SPACING:
1091       g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
1092       break;
1093     default:
1094       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1095       break;
1096     }
1097 }
1098
1099 /*************************************************************
1100  *                    GtkCellAreaClass                       *
1101  *************************************************************/
1102 static void
1103 gtk_cell_area_box_add (GtkCellArea        *area,
1104                        GtkCellRenderer    *renderer)
1105 {
1106   gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
1107                                 renderer, FALSE, FALSE, TRUE);
1108 }
1109
1110 static void
1111 gtk_cell_area_box_remove (GtkCellArea        *area,
1112                           GtkCellRenderer    *renderer)
1113 {
1114   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1115   GtkCellAreaBoxPrivate *priv = box->priv;
1116   GList                 *node;
1117
1118   if (priv->last_focus_cell == renderer)
1119     priv->last_focus_cell = NULL;
1120
1121   node = g_list_find_custom (priv->cells, renderer,
1122                              (GCompareFunc)cell_info_find);
1123
1124   if (node)
1125     {
1126       CellInfo *info = node->data;
1127
1128       cell_info_free (info);
1129
1130       priv->cells = g_list_delete_link (priv->cells, node);
1131
1132       /* Reconstruct cell groups */
1133       cell_groups_rebuild (box);
1134     }
1135   else
1136     g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1137 }
1138
1139 static void
1140 gtk_cell_area_box_foreach (GtkCellArea        *area,
1141                            GtkCellCallback     callback,
1142                            gpointer            callback_data)
1143 {
1144   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1145   GtkCellAreaBoxPrivate *priv = box->priv;
1146   GList                 *list;
1147
1148   for (list = priv->cells; list; list = list->next)
1149     {
1150       CellInfo *info = list->data;
1151
1152       if (callback (info->renderer, callback_data))
1153         break;
1154     }
1155 }
1156
1157 static void
1158 gtk_cell_area_box_foreach_alloc (GtkCellArea          *area,
1159                                  GtkCellAreaContext   *context,
1160                                  GtkWidget            *widget,
1161                                  const GdkRectangle   *cell_area,
1162                                  const GdkRectangle   *background_area,
1163                                  GtkCellAllocCallback  callback,
1164                                  gpointer              callback_data)
1165 {
1166   GtkCellAreaBox        *box      = GTK_CELL_AREA_BOX (area);
1167   GtkCellAreaBoxPrivate *priv     = box->priv;
1168   GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1169   GSList                *allocated_cells, *l;
1170   GdkRectangle           cell_alloc, cell_background;
1171   gboolean               rtl;
1172
1173   rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1174          gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1175
1176   cell_alloc = *cell_area;
1177
1178   /* Get a list of cells with allocation sizes decided regardless
1179    * of alignments and pack order etc.
1180    */
1181   allocated_cells = get_allocated_cells (box, box_context, widget,
1182                                          cell_area->width, cell_area->height);
1183
1184   for (l = allocated_cells; l; l = l->next)
1185     {
1186       AllocatedCell *cell = l->data;
1187
1188       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1189         {
1190           cell_alloc.x     = cell_area->x + cell->position;
1191           cell_alloc.width = cell->size;
1192         }
1193       else
1194         {
1195           cell_alloc.y      = cell_area->y + cell->position;
1196           cell_alloc.height = cell->size;
1197         }
1198
1199       /* Stop iterating over cells if they flow out of the render
1200        * area, this can happen because the render area can actually
1201        * be smaller than the requested area (treeview columns can
1202        * be user resizable and can be resized to be smaller than
1203        * the actual requested area).
1204        */
1205       if (cell_alloc.x > cell_area->x + cell_area->width ||
1206           cell_alloc.x + cell_alloc.width < cell_area->x ||
1207           cell_alloc.y > cell_area->y + cell_area->height)
1208         break;
1209
1210       /* Special case for the last cell (or first cell in rtl)...
1211        * let the last cell consume the remaining space in the area
1212        * (the last cell is allowed to consume the remaining space if
1213        * the space given for rendering is actually larger than allocation,
1214        * this can happen in the expander GtkTreeViewColumn where only the
1215        * deepest depth column receives the allocation... shallow columns
1216        * receive more width). */
1217       if (!l->next)
1218         {
1219           if (rtl)
1220             {
1221               /* Fill the leading space for the first cell in the area
1222                * (still last in the list)
1223                */
1224               cell_alloc.width = (cell_alloc.x - cell_area->x) + cell_alloc.width;
1225               cell_alloc.x     = cell_area->x;
1226             }
1227           else
1228             {
1229               cell_alloc.width  = cell_area->x + cell_area->width  - cell_alloc.x;
1230               cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1231             }
1232         }
1233       else
1234         {
1235           /* If the cell we are rendering doesnt fit into the remaining space,
1236            * clip it so that the underlying renderer has a chance to deal with
1237            * it (for instance text renderers get a chance to ellipsize).
1238            */
1239           if (cell_alloc.x + cell_alloc.width > cell_area->x + cell_area->width)
1240             cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1241
1242           if (cell_alloc.y + cell_alloc.height > cell_area->y + cell_area->height)
1243             cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1244         }
1245
1246       /* Add portions of the background_area to the cell_alloc
1247        * to create the cell_background
1248        */
1249       cell_background = cell_alloc;
1250
1251       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1252         {
1253           if (l == allocated_cells)
1254             {
1255               /* Add the depth to the first cell */
1256               if (rtl)
1257                 {
1258                   cell_background.width += background_area->width - cell_area->width;
1259                   cell_background.x      = background_area->x + background_area->width - cell_background.width;
1260                 }
1261               else
1262                 {
1263                   cell_background.width += cell_area->x - background_area->x;
1264                   cell_background.x      = background_area->x;
1265                 }
1266             }
1267
1268           if (l->next == NULL)
1269             {
1270               /* Grant this cell the remaining space */
1271               int remain = cell_background.x - background_area->x;
1272
1273               if (rtl)
1274                 cell_background.x -= remain;
1275               else
1276                 cell_background.width = background_area->width - remain;
1277             }
1278
1279           cell_background.y      = background_area->y;
1280           cell_background.height = background_area->height;
1281         }
1282       else
1283         {
1284           if (l == allocated_cells)
1285             {
1286               cell_background.height += cell_background.y - background_area->y;
1287               cell_background.y       = background_area->y;
1288             }
1289
1290           if (l->next == NULL)
1291               cell_background.height =
1292                 background_area->height - (cell_background.y - background_area->y);
1293
1294           cell_background.x     = background_area->x;
1295           cell_background.width = background_area->width;
1296         }
1297
1298       if (callback (cell->renderer, &cell_alloc, &cell_background, callback_data))
1299         break;
1300     }
1301
1302   g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1303   g_slist_free (allocated_cells);
1304 }
1305
1306 static void
1307 gtk_cell_area_box_apply_attributes (GtkCellArea          *area,
1308                                     GtkTreeModel         *tree_model,
1309                                     GtkTreeIter          *iter,
1310                                     gboolean              is_expander,
1311                                     gboolean              is_expanded)
1312 {
1313   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1314   GtkCellAreaBoxPrivate *priv = box->priv;
1315   gint                   i;
1316
1317   /* Call the parent class to apply the attributes */
1318   GTK_CELL_AREA_CLASS
1319     (gtk_cell_area_box_parent_class)->apply_attributes (area, tree_model, iter, 
1320                                                         is_expander, is_expanded);
1321
1322   /* Update visible state for cell groups */
1323   for (i = 0; i < priv->groups->len; i++)
1324     {
1325       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1326       GList     *list;
1327
1328       group->visible = FALSE;
1329
1330       for (list = group->cells; list && group->visible == FALSE; list = list->next)
1331         {
1332           CellInfo *info = list->data;
1333
1334           if (gtk_cell_renderer_get_visible (info->renderer))
1335             group->visible = TRUE;
1336         }
1337     }
1338 }
1339
1340 static void
1341 gtk_cell_area_box_set_cell_property (GtkCellArea        *area,
1342                                      GtkCellRenderer    *renderer,
1343                                      guint               prop_id,
1344                                      const GValue       *value,
1345                                      GParamSpec         *pspec)
1346 {
1347   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1348   GtkCellAreaBoxPrivate *priv = box->priv;
1349   GList                 *node;
1350   CellInfo              *info;
1351   gboolean               rebuild = FALSE;
1352   gboolean               val;
1353   GtkPackType            pack_type;
1354
1355   node = g_list_find_custom (priv->cells, renderer,
1356                              (GCompareFunc)cell_info_find);
1357   if (!node)
1358     return;
1359
1360   info = node->data;
1361
1362   switch (prop_id)
1363     {
1364     case CELL_PROP_EXPAND:
1365       val = g_value_get_boolean (value);
1366
1367       if (info->expand != val)
1368         {
1369           info->expand = val;
1370           rebuild      = TRUE;
1371         }
1372       break;
1373
1374     case CELL_PROP_ALIGN:
1375       val = g_value_get_boolean (value);
1376
1377       if (info->align != val)
1378         {
1379           info->align = val;
1380           rebuild     = TRUE;
1381         }
1382       break;
1383
1384     case CELL_PROP_FIXED_SIZE:
1385       val = g_value_get_boolean (value);
1386
1387       if (info->fixed != val)
1388         {
1389           info->fixed = val;
1390           rebuild     = TRUE;
1391         }
1392       break;
1393
1394     case CELL_PROP_PACK_TYPE:
1395       pack_type = g_value_get_enum (value);
1396
1397       if (info->pack != pack_type)
1398         {
1399           info->pack = pack_type;
1400           rebuild    = TRUE;
1401         }
1402       break;
1403     default:
1404       GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1405       break;
1406     }
1407
1408   /* Groups need to be rebuilt */
1409   if (rebuild)
1410     cell_groups_rebuild (box);
1411 }
1412
1413 static void
1414 gtk_cell_area_box_get_cell_property (GtkCellArea        *area,
1415                                      GtkCellRenderer    *renderer,
1416                                      guint               prop_id,
1417                                      GValue             *value,
1418                                      GParamSpec         *pspec)
1419 {
1420   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1421   GtkCellAreaBoxPrivate *priv = box->priv;
1422   GList                 *node;
1423   CellInfo              *info;
1424
1425   node = g_list_find_custom (priv->cells, renderer,
1426                              (GCompareFunc)cell_info_find);
1427   if (!node)
1428     return;
1429
1430   info = node->data;
1431
1432   switch (prop_id)
1433     {
1434     case CELL_PROP_EXPAND:
1435       g_value_set_boolean (value, info->expand);
1436       break;
1437
1438     case CELL_PROP_ALIGN:
1439       g_value_set_boolean (value, info->align);
1440       break;
1441
1442     case CELL_PROP_FIXED_SIZE:
1443       g_value_set_boolean (value, info->fixed);
1444       break;
1445
1446     case CELL_PROP_PACK_TYPE:
1447       g_value_set_enum (value, info->pack);
1448       break;
1449     default:
1450       GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1451       break;
1452     }
1453 }
1454
1455
1456 static GtkCellAreaContext *
1457 gtk_cell_area_box_create_context (GtkCellArea *area)
1458 {
1459   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1460   GtkCellAreaBoxPrivate *priv = box->priv;
1461   GtkCellAreaContext    *context =
1462     (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
1463                                      "area", area, NULL);
1464
1465   priv->contexts = g_slist_prepend (priv->contexts, context);
1466
1467   g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1468
1469   /* Tell the new group about our cell layout */
1470   init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1471
1472   return context;
1473 }
1474
1475 static GtkCellAreaContext *
1476 gtk_cell_area_box_copy_context (GtkCellArea        *area,
1477                                 GtkCellAreaContext *context)
1478 {
1479   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1480   GtkCellAreaBoxPrivate *priv = box->priv;
1481   GtkCellAreaContext    *copy =
1482     (GtkCellAreaContext *)_gtk_cell_area_box_context_copy (GTK_CELL_AREA_BOX (area),
1483                                                           GTK_CELL_AREA_BOX_CONTEXT (context));
1484
1485   priv->contexts = g_slist_prepend (priv->contexts, copy);
1486
1487   g_object_weak_ref (G_OBJECT (copy), (GWeakNotify)context_weak_notify, box);
1488
1489   return copy;
1490 }
1491
1492 static GtkSizeRequestMode
1493 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1494 {
1495   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (area);
1496   GtkCellAreaBoxPrivate *priv = box->priv;
1497
1498   return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1499     GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1500     GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1501 }
1502
1503 static void
1504 compute_size (GtkCellAreaBox        *box,
1505               GtkOrientation         orientation,
1506               GtkCellAreaBoxContext *context,
1507               GtkWidget             *widget,
1508               gint                   for_size,
1509               gint                  *minimum_size,
1510               gint                  *natural_size)
1511 {
1512   GtkCellAreaBoxPrivate *priv = box->priv;
1513   GtkCellArea           *area = GTK_CELL_AREA (box);
1514   GList                 *list;
1515   gint                   i;
1516   gint                   min_size = 0;
1517   gint                   nat_size = 0;
1518
1519   for (i = 0; i < priv->groups->len; i++)
1520     {
1521       CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1522       gint       group_min_size = 0;
1523       gint       group_nat_size = 0;
1524
1525       for (list = group->cells; list; list = list->next)
1526         {
1527           CellInfo *info = list->data;
1528           gint      renderer_min_size, renderer_nat_size;
1529
1530           if (!gtk_cell_renderer_get_visible (info->renderer))
1531               continue;
1532
1533           gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1534                                           &renderer_min_size, &renderer_nat_size);
1535
1536           if (orientation == priv->orientation)
1537             {
1538               if (min_size > 0)
1539                 {
1540                   min_size += priv->spacing;
1541                   nat_size += priv->spacing;
1542                 }
1543
1544               if (group_min_size > 0)
1545                 {
1546                   group_min_size += priv->spacing;
1547                   group_nat_size += priv->spacing;
1548                 }
1549
1550               min_size       += renderer_min_size;
1551               nat_size       += renderer_nat_size;
1552               group_min_size += renderer_min_size;
1553               group_nat_size += renderer_nat_size;
1554             }
1555           else
1556             {
1557               min_size       = MAX (min_size, renderer_min_size);
1558               nat_size       = MAX (nat_size, renderer_nat_size);
1559               group_min_size = MAX (group_min_size, renderer_min_size);
1560               group_nat_size = MAX (group_nat_size, renderer_nat_size);
1561             }
1562         }
1563
1564       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1565         {
1566           if (for_size < 0)
1567             _gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1568           else
1569             _gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1570                                                                    group_min_size, group_nat_size);
1571         }
1572       else
1573         {
1574           if (for_size < 0)
1575             _gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1576           else
1577             _gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1578                                                                    group_min_size, group_nat_size);
1579         }
1580     }
1581
1582   *minimum_size = min_size;
1583   *natural_size = nat_size;
1584
1585   /* Update rtl state for focus navigation to work */
1586   priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1587                gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1588 }
1589
1590 static GtkRequestedSize *
1591 get_group_sizes (GtkCellArea    *area,
1592                  CellGroup      *group,
1593                  GtkOrientation  orientation,
1594                  GtkWidget      *widget,
1595                  gint           *n_sizes)
1596 {
1597   GtkRequestedSize *sizes;
1598   GList            *l;
1599   gint              i;
1600
1601   *n_sizes = count_visible_cells (group, NULL);
1602   sizes    = g_new (GtkRequestedSize, *n_sizes);
1603
1604   for (l = group->cells, i = 0; l; l = l->next)
1605     {
1606       CellInfo *info = l->data;
1607
1608       if (!gtk_cell_renderer_get_visible (info->renderer))
1609         continue;
1610
1611       sizes[i].data = info;
1612
1613       gtk_cell_area_request_renderer (area, info->renderer,
1614                                       orientation, widget, -1,
1615                                       &sizes[i].minimum_size,
1616                                       &sizes[i].natural_size);
1617
1618       i++;
1619     }
1620
1621   return sizes;
1622 }
1623
1624 static void
1625 compute_group_size_for_opposing_orientation (GtkCellAreaBox     *box,
1626                                              CellGroup          *group,
1627                                              GtkWidget          *widget,
1628                                              gint                for_size,
1629                                              gint               *minimum_size,
1630                                              gint               *natural_size)
1631 {
1632   GtkCellAreaBoxPrivate *priv = box->priv;
1633   GtkCellArea           *area = GTK_CELL_AREA (box);
1634
1635   /* Exception for single cell groups */
1636   if (group->n_cells == 1)
1637     {
1638       CellInfo *info = group->cells->data;
1639
1640       gtk_cell_area_request_renderer (area, info->renderer,
1641                                       OPPOSITE_ORIENTATION (priv->orientation),
1642                                       widget, for_size, minimum_size, natural_size);
1643     }
1644   else
1645     {
1646       GtkRequestedSize *orientation_sizes;
1647       CellInfo         *info;
1648       gint              n_sizes, i;
1649       gint              avail_size     = for_size;
1650       gint              extra_size, extra_extra;
1651       gint              min_size = 0, nat_size = 0;
1652
1653       orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1654
1655       /* First naturally allocate the cells in the group into the for_size */
1656       avail_size -= (n_sizes - 1) * priv->spacing;
1657       for (i = 0; i < n_sizes; i++)
1658         avail_size -= orientation_sizes[i].minimum_size;
1659
1660       if (avail_size > 0)
1661         avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1662       else
1663         avail_size = 0;
1664
1665       /* Calculate/distribute expand for cells */
1666       if (group->expand_cells > 0)
1667         {
1668           extra_size  = avail_size / group->expand_cells;
1669           extra_extra = avail_size % group->expand_cells;
1670         }
1671       else
1672         extra_size = extra_extra = 0;
1673
1674       for (i = 0; i < n_sizes; i++)
1675         {
1676           gint cell_min, cell_nat;
1677
1678           info = orientation_sizes[i].data;
1679
1680           if (info->expand)
1681             {
1682               orientation_sizes[i].minimum_size += extra_size;
1683               if (extra_extra)
1684                 {
1685                   orientation_sizes[i].minimum_size++;
1686                   extra_extra--;
1687                 }
1688             }
1689
1690           gtk_cell_area_request_renderer (area, info->renderer,
1691                                           OPPOSITE_ORIENTATION (priv->orientation),
1692                                           widget,
1693                                           orientation_sizes[i].minimum_size,
1694                                           &cell_min, &cell_nat);
1695
1696           min_size = MAX (min_size, cell_min);
1697           nat_size = MAX (nat_size, cell_nat);
1698         }
1699
1700       *minimum_size = min_size;
1701       *natural_size = nat_size;
1702
1703       g_free (orientation_sizes);
1704     }
1705 }
1706
1707 static void
1708 compute_size_for_opposing_orientation (GtkCellAreaBox        *box,
1709                                        GtkCellAreaBoxContext *context,
1710                                        GtkWidget             *widget,
1711                                        gint                   for_size,
1712                                        gint                  *minimum_size,
1713                                        gint                  *natural_size)
1714 {
1715   GtkCellAreaBoxPrivate *priv = box->priv;
1716   CellGroup             *group;
1717   GtkRequestedSize      *orientation_sizes;
1718   gint                   n_groups, n_expand_groups, i;
1719   gint                   avail_size = for_size;
1720   gint                   extra_size, extra_extra;
1721   gint                   min_size = 0, nat_size = 0;
1722
1723   n_expand_groups = count_expand_groups (box);
1724
1725   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1726     orientation_sizes = _gtk_cell_area_box_context_get_widths (context, &n_groups);
1727   else
1728     orientation_sizes = _gtk_cell_area_box_context_get_heights (context, &n_groups);
1729
1730   /* First start by naturally allocating space among groups of cells */
1731   avail_size -= (n_groups - 1) * priv->spacing;
1732   for (i = 0; i < n_groups; i++)
1733     avail_size -= orientation_sizes[i].minimum_size;
1734
1735   if (avail_size > 0)
1736     avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1737   else
1738     avail_size = 0;
1739
1740   /* Calculate/distribute expand for groups */
1741   if (n_expand_groups > 0)
1742     {
1743       extra_size  = avail_size / n_expand_groups;
1744       extra_extra = avail_size % n_expand_groups;
1745     }
1746   else
1747     extra_size = extra_extra = 0;
1748
1749   /* Now we need to naturally allocate sizes for cells in each group
1750    * and push the height-for-width for each group accordingly while
1751    * accumulating the overall height-for-width for this row.
1752    */
1753   for (i = 0; i < n_groups; i++)
1754     {
1755       gint group_min, group_nat;
1756       gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1757
1758       group = &g_array_index (priv->groups, CellGroup, group_idx);
1759
1760       if (group->expand_cells > 0)
1761         {
1762           orientation_sizes[i].minimum_size += extra_size;
1763           if (extra_extra)
1764             {
1765               orientation_sizes[i].minimum_size++;
1766               extra_extra--;
1767             }
1768         }
1769
1770       /* Now we have the allocation for the group,
1771        * request its height-for-width
1772        */
1773       compute_group_size_for_opposing_orientation (box, group, widget,
1774                                                    orientation_sizes[i].minimum_size,
1775                                                    &group_min, &group_nat);
1776
1777       min_size = MAX (min_size, group_min);
1778       nat_size = MAX (nat_size, group_nat);
1779
1780       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1781         {
1782           _gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1783                                                                  group_min, group_nat);
1784         }
1785       else
1786         {
1787           _gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1788                                                                  group_min, group_nat);
1789         }
1790     }
1791
1792   *minimum_size = min_size;
1793   *natural_size = nat_size;
1794
1795   g_free (orientation_sizes);
1796
1797   /* Update rtl state for focus navigation to work */
1798   priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1799                gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1800 }
1801
1802
1803
1804 static void
1805 gtk_cell_area_box_get_preferred_width (GtkCellArea        *area,
1806                                        GtkCellAreaContext *context,
1807                                        GtkWidget          *widget,
1808                                        gint               *minimum_width,
1809                                        gint               *natural_width)
1810 {
1811   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1812   GtkCellAreaBoxContext *box_context;
1813   gint                   min_width, nat_width;
1814
1815   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1816
1817   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1818
1819   /* Compute the size of all renderers for current row data,
1820    * bumping cell alignments in the context along the way
1821    */
1822   compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1823                 box_context, widget, -1, &min_width, &nat_width);
1824
1825   if (minimum_width)
1826     *minimum_width = min_width;
1827
1828   if (natural_width)
1829     *natural_width = nat_width;
1830 }
1831
1832 static void
1833 gtk_cell_area_box_get_preferred_height (GtkCellArea        *area,
1834                                         GtkCellAreaContext *context,
1835                                         GtkWidget          *widget,
1836                                         gint               *minimum_height,
1837                                         gint               *natural_height)
1838 {
1839   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1840   GtkCellAreaBoxContext *box_context;
1841   gint                   min_height, nat_height;
1842
1843   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1844
1845   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1846
1847   /* Compute the size of all renderers for current row data,
1848    * bumping cell alignments in the context along the way
1849    */
1850   compute_size (box, GTK_ORIENTATION_VERTICAL,
1851                 box_context, widget, -1, &min_height, &nat_height);
1852
1853   if (minimum_height)
1854     *minimum_height = min_height;
1855
1856   if (natural_height)
1857     *natural_height = nat_height;
1858 }
1859
1860 static void
1861 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea        *area,
1862                                                   GtkCellAreaContext *context,
1863                                                   GtkWidget          *widget,
1864                                                   gint                width,
1865                                                   gint               *minimum_height,
1866                                                   gint               *natural_height)
1867 {
1868   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1869   GtkCellAreaBoxContext *box_context;
1870   GtkCellAreaBoxPrivate *priv;
1871   gint                   min_height, nat_height;
1872
1873   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1874
1875   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1876   priv        = box->priv;
1877
1878   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1879     {
1880       /* Add up vertical requests of height for width and push
1881        * the overall cached sizes for alignments
1882        */
1883       compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1884     }
1885   else
1886     {
1887       /* Juice: virtually allocate cells into the for_width using the
1888        * alignments and then return the overall height for that width,
1889        * and cache it
1890        */
1891       compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1892     }
1893
1894   if (minimum_height)
1895     *minimum_height = min_height;
1896
1897   if (natural_height)
1898     *natural_height = nat_height;
1899 }
1900
1901 static void
1902 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea        *area,
1903                                                   GtkCellAreaContext *context,
1904                                                   GtkWidget          *widget,
1905                                                   gint                height,
1906                                                   gint               *minimum_width,
1907                                                   gint               *natural_width)
1908 {
1909   GtkCellAreaBox        *box = GTK_CELL_AREA_BOX (area);
1910   GtkCellAreaBoxContext *box_context;
1911   GtkCellAreaBoxPrivate *priv;
1912   gint                   min_width, nat_width;
1913
1914   g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1915
1916   box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1917   priv        = box->priv;
1918
1919   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1920     {
1921       /* Add up horizontal requests of width for height and push
1922        * the overall cached sizes for alignments
1923        */
1924       compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1925     }
1926   else
1927     {
1928       /* Juice: horizontally allocate cells into the for_height using the
1929        * alignments and then return the overall width for that height,
1930        * and cache it
1931        */
1932       compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1933     }
1934
1935   if (minimum_width)
1936     *minimum_width = min_width;
1937
1938   if (natural_width)
1939     *natural_width = nat_width;
1940 }
1941
1942 enum {
1943   FOCUS_NONE,
1944   FOCUS_PREV,
1945   FOCUS_NEXT,
1946   FOCUS_LAST_CELL
1947 };
1948
1949 static gboolean
1950 gtk_cell_area_box_focus (GtkCellArea      *area,
1951                          GtkDirectionType  direction)
1952 {
1953   GtkCellAreaBox        *box   = GTK_CELL_AREA_BOX (area);
1954   GtkCellAreaBoxPrivate *priv  = box->priv;
1955   gint                   cycle = FOCUS_NONE;
1956   gboolean               cycled_focus = FALSE;
1957   GtkCellRenderer       *focus_cell;
1958
1959   focus_cell = gtk_cell_area_get_focus_cell (area);
1960
1961   /* Special case, when there is no activatable cell, focus
1962    * is painted around the entire area... in this case we
1963    * let focus leave the area directly.
1964    */
1965   if (focus_cell && !gtk_cell_area_is_activatable (area))
1966     {
1967       gtk_cell_area_set_focus_cell (area, NULL);
1968       return FALSE;
1969     }
1970
1971   switch (direction)
1972     {
1973     case GTK_DIR_TAB_FORWARD:
1974       cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1975       break;
1976     case GTK_DIR_TAB_BACKWARD:
1977       cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1978       break;
1979     case GTK_DIR_UP:
1980       if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1981         cycle = FOCUS_PREV;
1982       else if (!focus_cell)
1983         cycle = FOCUS_LAST_CELL;
1984       break;
1985     case GTK_DIR_DOWN:
1986       if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1987         cycle = FOCUS_NEXT;
1988       else if (!focus_cell)
1989         cycle = FOCUS_LAST_CELL;
1990       break;
1991     case GTK_DIR_LEFT:
1992       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1993         cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1994       else if (!focus_cell)
1995         cycle = FOCUS_LAST_CELL;
1996       break;
1997     case GTK_DIR_RIGHT:
1998       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1999         cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
2000       else if (!focus_cell)
2001         cycle = FOCUS_LAST_CELL;
2002       break;
2003     default:
2004       break;
2005     }
2006
2007   if (cycle == FOCUS_LAST_CELL)
2008     {
2009       gtk_cell_area_set_focus_cell (area, priv->last_focus_cell);
2010       cycled_focus = TRUE;
2011     }
2012   else if (cycle != FOCUS_NONE)
2013     {
2014       gboolean  found_cell = FALSE;
2015       GList    *list;
2016       gint      i;
2017
2018       /* If there is no focused cell, focus on the first (or last) one */
2019       if (!focus_cell)
2020         found_cell = TRUE;
2021
2022       for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
2023            cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
2024            i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
2025         {
2026           CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
2027
2028           for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
2029                cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
2030             {
2031               CellInfo *info = list->data;
2032
2033               if (info->renderer == focus_cell)
2034                 found_cell = TRUE;
2035               else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
2036                        gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
2037                 {
2038                   gtk_cell_area_set_focus_cell (area, info->renderer);
2039                   cycled_focus = TRUE;
2040                 }
2041             }
2042         }
2043     }
2044
2045   if (!cycled_focus)
2046     gtk_cell_area_set_focus_cell (area, NULL);
2047
2048   return cycled_focus;
2049 }
2050
2051
2052 /*************************************************************
2053  *                    GtkCellLayoutIface                     *
2054  *************************************************************/
2055 static void
2056 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
2057 {
2058   iface->pack_start = gtk_cell_area_box_layout_pack_start;
2059   iface->pack_end   = gtk_cell_area_box_layout_pack_end;
2060   iface->reorder    = gtk_cell_area_box_layout_reorder;
2061 }
2062
2063 static void
2064 gtk_cell_area_box_layout_pack_start (GtkCellLayout      *cell_layout,
2065                                      GtkCellRenderer    *renderer,
2066                                      gboolean            expand)
2067 {
2068   gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2069 }
2070
2071 static void
2072 gtk_cell_area_box_layout_pack_end (GtkCellLayout      *cell_layout,
2073                                    GtkCellRenderer    *renderer,
2074                                    gboolean            expand)
2075 {
2076   gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2077 }
2078
2079 static void
2080 gtk_cell_area_box_layout_reorder (GtkCellLayout      *cell_layout,
2081                                   GtkCellRenderer    *renderer,
2082                                   gint                position)
2083 {
2084   GtkCellAreaBox        *box  = GTK_CELL_AREA_BOX (cell_layout);
2085   GtkCellAreaBoxPrivate *priv = box->priv;
2086   GList                 *node;
2087   CellInfo              *info;
2088
2089   node = g_list_find_custom (priv->cells, renderer,
2090                              (GCompareFunc)cell_info_find);
2091
2092   if (node)
2093     {
2094       info = node->data;
2095
2096       priv->cells = g_list_delete_link (priv->cells, node);
2097       priv->cells = g_list_insert (priv->cells, info, position);
2098
2099       cell_groups_rebuild (box);
2100     }
2101 }
2102
2103 /*************************************************************
2104  *       Private interaction with GtkCellAreaBoxContext      *
2105  *************************************************************/
2106 gboolean
2107 _gtk_cell_area_box_group_visible (GtkCellAreaBox  *box,
2108                                   gint             group_idx)
2109 {
2110   GtkCellAreaBoxPrivate *priv  = box->priv;
2111   CellGroup *group;
2112   
2113   g_assert (group_idx >= 0 && group_idx < priv->groups->len);
2114
2115   group = &g_array_index (priv->groups, CellGroup, group_idx);
2116
2117   return group->visible;
2118 }
2119
2120
2121 /*************************************************************
2122  *                            API                            *
2123  *************************************************************/
2124 /**
2125  * gtk_cell_area_box_new:
2126  *
2127  * Creates a new #GtkCellAreaBox.
2128  *
2129  * Return value: a newly created #GtkCellAreaBox
2130  *
2131  * Since: 3.0
2132  */
2133 GtkCellArea *
2134 gtk_cell_area_box_new (void)
2135 {
2136   return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
2137 }
2138
2139 /**
2140  * gtk_cell_area_box_pack_start:
2141  * @box: a #GtkCellAreaBox
2142  * @renderer: the #GtkCellRenderer to add
2143  * @expand: whether @renderer should receive extra space when the area receives
2144  * more than its natural size
2145  * @align: whether @renderer should be aligned in adjacent rows
2146  * @fixed: whether @renderer should have the same size in all rows
2147  *
2148  * Adds @renderer to @box, packed with reference to the start of @box.
2149  *
2150  * The @renderer is packed after any other #GtkCellRenderer packed
2151  * with reference to the start of @box.
2152  *
2153  * Since: 3.0
2154  */
2155 void
2156 gtk_cell_area_box_pack_start  (GtkCellAreaBox  *box,
2157                                GtkCellRenderer *renderer,
2158                                gboolean         expand,
2159                                gboolean         align,
2160                                gboolean         fixed)
2161 {
2162   GtkCellAreaBoxPrivate *priv;
2163   CellInfo              *info;
2164
2165   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2166   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2167
2168   priv = box->priv;
2169
2170   if (g_list_find_custom (priv->cells, renderer,
2171                           (GCompareFunc)cell_info_find))
2172     {
2173       g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
2174       return;
2175     }
2176
2177   info = cell_info_new (renderer, GTK_PACK_START, expand, align, fixed);
2178
2179   priv->cells = g_list_append (priv->cells, info);
2180
2181   cell_groups_rebuild (box);
2182 }
2183
2184 /**
2185  * gtk_cell_area_box_pack_end:
2186  * @box: a #GtkCellAreaBox
2187  * @renderer: the #GtkCellRenderer to add
2188  * @expand: whether @renderer should receive extra space when the area receives
2189  * more than its natural size
2190  * @align: whether @renderer should be aligned in adjacent rows
2191  * @fixed: whether @renderer should have the same size in all rows
2192  *
2193  * Adds @renderer to @box, packed with reference to the end of @box.
2194  *
2195  * The @renderer is packed after (away from end of) any other
2196  * #GtkCellRenderer packed with reference to the end of @box.
2197  *
2198  * Since: 3.0
2199  */
2200 void
2201 gtk_cell_area_box_pack_end (GtkCellAreaBox  *box,
2202                             GtkCellRenderer *renderer,
2203                             gboolean         expand,
2204                             gboolean         align,
2205                             gboolean         fixed)
2206 {
2207   GtkCellAreaBoxPrivate *priv;
2208   CellInfo              *info;
2209
2210   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2211   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2212
2213   priv = box->priv;
2214
2215   if (g_list_find_custom (priv->cells, renderer,
2216                           (GCompareFunc)cell_info_find))
2217     {
2218       g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
2219       return;
2220     }
2221
2222   info = cell_info_new (renderer, GTK_PACK_END, expand, align, fixed);
2223
2224   priv->cells = g_list_append (priv->cells, info);
2225
2226   cell_groups_rebuild (box);
2227 }
2228
2229 /**
2230  * gtk_cell_area_box_get_spacing:
2231  * @box: a #GtkCellAreaBox
2232  *
2233  * Gets the spacing added between cell renderers.
2234  *
2235  * Return value: the space added between cell renderers in @box.
2236  *
2237  * Since: 3.0
2238  */
2239 gint
2240 gtk_cell_area_box_get_spacing (GtkCellAreaBox  *box)
2241 {
2242   g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2243
2244   return box->priv->spacing;
2245 }
2246
2247 /**
2248  * gtk_cell_area_box_set_spacing:
2249  * @box: a #GtkCellAreaBox
2250  * @spacing: the space to add between #GtkCellRenderers
2251  *
2252  * Sets the spacing to add between cell renderers in @box.
2253  *
2254  * Since: 3.0
2255  */
2256 void
2257 gtk_cell_area_box_set_spacing (GtkCellAreaBox  *box,
2258                                gint             spacing)
2259 {
2260   GtkCellAreaBoxPrivate *priv;
2261
2262   g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2263
2264   priv = box->priv;
2265
2266   if (priv->spacing != spacing)
2267     {
2268       priv->spacing = spacing;
2269
2270       g_object_notify (G_OBJECT (box), "spacing");
2271
2272       /* Notify that size needs to be requested again */
2273       reset_contexts (box);
2274     }
2275 }