3 * Copyright (C) 2010 Openismus GmbH
6 * Tristan Van Berkom <tristanvb@openismus.com>
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.
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.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 * SECTION:gtkcellareabox
25 * @Short_Description: A cell area that renders GtkCellRenderers
26 * into a row or a column
27 * @Title: GtkCellAreaBox
29 * The #GtkCellAreaBox renders cell renderers into a row or a column
30 * depending on its #GtkOrientation.
32 * GtkCellAreaBox uses a notion of <emphasis>packing</emphasis>. Packing
33 * refers to adding cell renderers with reference to a particular position
34 * in a #GtkCellAreaBox. There are two reference positions: the
35 * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
36 * When the #GtkCellAreaBox is oriented in the %GTK_ORIENTATION_VERTICAL
37 * orientation, the start is defined as the top of the box and the end is
38 * defined as the bottom. In the %GTK_ORIENTATION_HORIZONTAL orientation
39 * start is defined as the left side and the end is defined as the right
42 * Alignments of #GtkCellRenderers rendered in adjacent rows can be
43 * configured by configuring the #GtkCellAreaBox:align child cell property
44 * with gtk_cell_area_cell_set_property() or by specifying the "align"
45 * argument to gtk_cell_area_box_pack_start() and gtk_cell_area_box_pack_end().
50 #include "gtkorientable.h"
51 #include "gtkcelllayout.h"
52 #include "gtkcellareabox.h"
53 #include "gtkcellareaboxcontextprivate.h"
54 #include "gtktypebuiltins.h"
55 #include "gtkprivate.h"
59 static void gtk_cell_area_box_finalize (GObject *object);
60 static void gtk_cell_area_box_dispose (GObject *object);
61 static void gtk_cell_area_box_set_property (GObject *object,
65 static void gtk_cell_area_box_get_property (GObject *object,
70 /* GtkCellAreaClass */
71 static void gtk_cell_area_box_add (GtkCellArea *area,
72 GtkCellRenderer *renderer);
73 static void gtk_cell_area_box_remove (GtkCellArea *area,
74 GtkCellRenderer *renderer);
75 static void gtk_cell_area_box_foreach (GtkCellArea *area,
76 GtkCellCallback callback,
77 gpointer callback_data);
78 static void gtk_cell_area_box_foreach_alloc (GtkCellArea *area,
79 GtkCellAreaContext *context,
81 const GdkRectangle *cell_area,
82 const GdkRectangle *background_area,
83 GtkCellAllocCallback callback,
84 gpointer callback_data);
85 static void gtk_cell_area_box_apply_attributes (GtkCellArea *area,
86 GtkTreeModel *tree_model,
89 gboolean is_expanded);
90 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
91 GtkCellRenderer *renderer,
95 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
96 GtkCellRenderer *renderer,
100 static GtkCellAreaContext *gtk_cell_area_box_create_context (GtkCellArea *area);
101 static GtkCellAreaContext *gtk_cell_area_box_copy_context (GtkCellArea *area,
102 GtkCellAreaContext *context);
103 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
104 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
105 GtkCellAreaContext *context,
108 gint *natural_width);
109 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
110 GtkCellAreaContext *context,
112 gint *minimum_height,
113 gint *natural_height);
114 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
115 GtkCellAreaContext *context,
118 gint *minimum_height,
119 gint *natural_height);
120 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
121 GtkCellAreaContext *context,
125 gint *natural_width);
126 static gboolean gtk_cell_area_box_focus (GtkCellArea *area,
127 GtkDirectionType direction);
129 /* GtkCellLayoutIface */
130 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
131 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
132 GtkCellRenderer *renderer,
134 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
135 GtkCellRenderer *renderer,
137 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
138 GtkCellRenderer *renderer,
140 static void gtk_cell_area_box_focus_changed (GtkCellArea *area,
142 GtkCellAreaBox *box);
145 /* CellInfo/CellGroup metadata handling and convenience functions */
147 GtkCellRenderer *renderer;
149 guint expand : 1; /* Whether the cell expands */
150 guint pack : 1; /* Whether it is packed from the start or end */
151 guint align : 1; /* Whether to align its position with adjacent rows */
152 guint fixed : 1; /* Whether to require the same size for all rows */
160 guint expand_cells : 8;
166 GtkCellRenderer *renderer;
172 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
177 static void cell_info_free (CellInfo *info);
178 static gint cell_info_find (CellInfo *info,
179 GtkCellRenderer *renderer);
181 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
184 static void allocated_cell_free (AllocatedCell *cell);
185 static GList *list_consecutive_cells (GtkCellAreaBox *box);
186 static gint count_expand_groups (GtkCellAreaBox *box);
187 static void context_weak_notify (GtkCellAreaBox *box,
188 GtkCellAreaBoxContext *dead_context);
189 static void reset_contexts (GtkCellAreaBox *box);
190 static void init_context_groups (GtkCellAreaBox *box);
191 static void init_context_group (GtkCellAreaBox *box,
192 GtkCellAreaBoxContext *context);
193 static GSList *get_allocated_cells (GtkCellAreaBox *box,
194 GtkCellAreaBoxContext *context,
200 struct _GtkCellAreaBoxPrivate
202 /* We hold on to the previously focused cell when navigating
203 * up and down in a horizontal box (or left and right on a vertical one)
204 * this way we always re-enter the last focused cell.
206 GtkCellRenderer *last_focus_cell;
207 gulong focus_cell_id;
214 GtkOrientation orientation;
217 /* We hold on to the rtl state from a widget we are requested for
218 * so that we can navigate focus correctly
233 CELL_PROP_FIXED_SIZE,
237 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
238 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
239 gtk_cell_area_box_cell_layout_init)
240 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
242 #define OPPOSITE_ORIENTATION(orientation) \
243 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
244 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
247 gtk_cell_area_box_init (GtkCellAreaBox *box)
249 GtkCellAreaBoxPrivate *priv;
251 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
252 GTK_TYPE_CELL_AREA_BOX,
253 GtkCellAreaBoxPrivate);
256 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
257 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
259 priv->contexts = NULL;
263 /* Watch whenever focus is given to a cell, even if it's not with keynav,
264 * this way we remember upon entry of the area where focus was last time
267 priv->focus_cell_id = g_signal_connect (box, "notify::focus-cell",
268 G_CALLBACK (gtk_cell_area_box_focus_changed), box);
272 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
274 GObjectClass *object_class = G_OBJECT_CLASS (class);
275 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
278 object_class->finalize = gtk_cell_area_box_finalize;
279 object_class->dispose = gtk_cell_area_box_dispose;
280 object_class->set_property = gtk_cell_area_box_set_property;
281 object_class->get_property = gtk_cell_area_box_get_property;
283 /* GtkCellAreaClass */
284 area_class->add = gtk_cell_area_box_add;
285 area_class->remove = gtk_cell_area_box_remove;
286 area_class->foreach = gtk_cell_area_box_foreach;
287 area_class->foreach_alloc = gtk_cell_area_box_foreach_alloc;
288 area_class->apply_attributes = gtk_cell_area_box_apply_attributes;
289 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
290 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
292 area_class->create_context = gtk_cell_area_box_create_context;
293 area_class->copy_context = gtk_cell_area_box_copy_context;
294 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
295 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
296 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
297 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
298 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
300 area_class->focus = gtk_cell_area_box_focus;
303 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
306 * GtkCellAreaBox:spacing:
308 * The amount of space to reserve between cells.
312 g_object_class_install_property (object_class,
314 g_param_spec_int ("spacing",
316 P_("Space which is inserted between cells"),
320 GTK_PARAM_READWRITE));
322 /* Cell Properties */
324 * GtkCellAreaBox:expand:
326 * Whether the cell renderer should receive extra space
327 * when the area receives more than its natural size.
331 gtk_cell_area_class_install_cell_property (area_class,
336 P_("Whether the cell expands"),
338 GTK_PARAM_READWRITE));
341 * GtkCellAreaBox:align:
343 * Whether the cell renderer should be aligned in adjacent rows.
347 gtk_cell_area_class_install_cell_property (area_class,
352 P_("Whether cell should align with adjacent rows"),
354 GTK_PARAM_READWRITE));
357 * GtkCellAreaBox:fixed-size:
359 * Whether the cell renderer should require the same size
360 * for all rows for which it was requested.
364 gtk_cell_area_class_install_cell_property (area_class,
365 CELL_PROP_FIXED_SIZE,
369 P_("Whether cells should be the same size in all rows"),
371 GTK_PARAM_READWRITE));
374 * GtkCellAreaBox:pack-type:
376 * A GtkPackType indicating whether the cell renderer is packed
377 * with reference to the start or end of the area.
381 gtk_cell_area_class_install_cell_property (area_class,
386 P_("A GtkPackType indicating whether the cell is packed with "
387 "reference to the start or end of the cell area"),
388 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
389 GTK_PARAM_READWRITE));
391 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
395 /*************************************************************
396 * CellInfo/CellGroup basics and convenience functions *
397 *************************************************************/
399 cell_info_new (GtkCellRenderer *renderer,
405 CellInfo *info = g_slice_new (CellInfo);
407 info->renderer = g_object_ref_sink (renderer);
409 info->expand = expand;
417 cell_info_free (CellInfo *info)
419 g_object_unref (info->renderer);
421 g_slice_free (CellInfo, info);
425 cell_info_find (CellInfo *info,
426 GtkCellRenderer *renderer)
428 return (info->renderer == renderer) ? 0 : -1;
431 static AllocatedCell *
432 allocated_cell_new (GtkCellRenderer *renderer,
436 AllocatedCell *cell = g_slice_new (AllocatedCell);
438 cell->renderer = renderer;
439 cell->position = position;
446 allocated_cell_free (AllocatedCell *cell)
448 g_slice_free (AllocatedCell, cell);
452 list_consecutive_cells (GtkCellAreaBox *box)
454 GtkCellAreaBoxPrivate *priv = box->priv;
455 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
458 /* List cells in consecutive order taking their
459 * PACK_START/PACK_END options into account
461 for (l = priv->cells; l; l = l->next)
465 if (info->pack == GTK_PACK_START)
466 consecutive_cells = g_list_prepend (consecutive_cells, info);
469 for (l = priv->cells; l; l = l->next)
473 if (info->pack == GTK_PACK_END)
474 pack_end_cells = g_list_prepend (pack_end_cells, info);
477 consecutive_cells = g_list_reverse (consecutive_cells);
478 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
480 return consecutive_cells;
484 cell_groups_clear (GtkCellAreaBox *box)
486 GtkCellAreaBoxPrivate *priv = box->priv;
489 for (i = 0; i < priv->groups->len; i++)
491 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
493 g_list_free (group->cells);
496 g_array_set_size (priv->groups, 0);
500 cell_groups_rebuild (GtkCellAreaBox *box)
502 GtkCellAreaBoxPrivate *priv = box->priv;
503 CellGroup group = { 0, };
504 CellGroup *group_ptr;
507 gboolean last_cell_fixed = FALSE;
509 cell_groups_clear (box);
514 cells = list_consecutive_cells (box);
516 /* First group is implied */
517 g_array_append_val (priv->groups, group);
518 group_ptr = &g_array_index (priv->groups, CellGroup, id);
520 for (l = cells; l; l = l->next)
522 CellInfo *info = l->data;
524 /* A new group starts with any aligned cell, or
525 * at the beginning and end of a fixed size cell.
526 * the first group is implied */
527 if ((info->align || info->fixed || last_cell_fixed) && l != cells)
529 memset (&group, 0x0, sizeof (CellGroup));
532 g_array_append_val (priv->groups, group);
533 group_ptr = &g_array_index (priv->groups, CellGroup, id);
536 group_ptr->cells = g_list_prepend (group_ptr->cells, info);
537 group_ptr->n_cells++;
539 /* Not every group is aligned, some are floating
540 * fixed size cells */
542 group_ptr->align = TRUE;
544 /* A group expands if it contains any expand cells */
546 group_ptr->expand_cells++;
548 last_cell_fixed = info->fixed;
553 for (id = 0; id < priv->groups->len; id++)
555 group_ptr = &g_array_index (priv->groups, CellGroup, id);
557 group_ptr->cells = g_list_reverse (group_ptr->cells);
560 /* Contexts need to be updated with the new grouping information */
561 init_context_groups (box);
565 count_visible_cells (CellGroup *group,
569 gint visible_cells = 0;
570 gint n_expand_cells = 0;
572 for (l = group->cells; l; l = l->next)
574 CellInfo *info = l->data;
576 if (gtk_cell_renderer_get_visible (info->renderer))
586 *expand_cells = n_expand_cells;
588 return visible_cells;
592 count_expand_groups (GtkCellAreaBox *box)
594 GtkCellAreaBoxPrivate *priv = box->priv;
596 gint expand_groups = 0;
598 for (i = 0; i < priv->groups->len; i++)
600 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
602 if (group->expand_cells > 0)
606 return expand_groups;
610 context_weak_notify (GtkCellAreaBox *box,
611 GtkCellAreaBoxContext *dead_context)
613 GtkCellAreaBoxPrivate *priv = box->priv;
615 priv->contexts = g_slist_remove (priv->contexts, dead_context);
619 init_context_group (GtkCellAreaBox *box,
620 GtkCellAreaBoxContext *context)
622 GtkCellAreaBoxPrivate *priv = box->priv;
623 gint *expand_groups, *align_groups, i;
625 expand_groups = g_new (gboolean, priv->groups->len);
626 align_groups = g_new (gboolean, priv->groups->len);
628 for (i = 0; i < priv->groups->len; i++)
630 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
632 expand_groups[i] = (group->expand_cells > 0);
633 align_groups[i] = group->align;
636 /* This call implies resetting the request info */
637 _gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups, align_groups);
638 g_free (expand_groups);
639 g_free (align_groups);
643 init_context_groups (GtkCellAreaBox *box)
645 GtkCellAreaBoxPrivate *priv = box->priv;
648 /* When the box's groups are reconstructed,
649 * contexts need to be reinitialized.
651 for (l = priv->contexts; l; l = l->next)
653 GtkCellAreaBoxContext *context = l->data;
655 init_context_group (box, context);
660 reset_contexts (GtkCellAreaBox *box)
662 GtkCellAreaBoxPrivate *priv = box->priv;
665 /* When the box layout changes, contexts need to
666 * be reset and sizes for the box get requested again
668 for (l = priv->contexts; l; l = l->next)
670 GtkCellAreaContext *context = l->data;
672 gtk_cell_area_context_reset (context);
676 /* Fall back on a completely unaligned dynamic allocation of cells
677 * when not allocated for the said orientation, alignment of cells
678 * is not done when each area gets a different size in the orientation
682 allocate_cells_manually (GtkCellAreaBox *box,
687 GtkCellAreaBoxPrivate *priv = box->priv;
689 GSList *allocated_cells = NULL;
690 GtkRequestedSize *sizes;
692 gint nvisible = 0, nexpand = 0, group_expand;
693 gint avail_size, extra_size, extra_extra, full_size;
694 gint position = 0, for_size;
700 /* For vertical oriented boxes, we just let the cell renderers
701 * realign themselves for rtl
703 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
704 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
706 cells = list_consecutive_cells (box);
708 /* Count the visible and expand cells */
709 for (i = 0; i < priv->groups->len; i++)
711 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
713 nvisible += count_visible_cells (group, &group_expand);
714 nexpand += group_expand;
723 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
725 full_size = avail_size = width;
730 full_size = avail_size = height;
734 /* Go ahead and collect the requests on the fly */
735 sizes = g_new0 (GtkRequestedSize, nvisible);
736 for (l = cells, i = 0; l; l = l->next)
738 CellInfo *info = l->data;
740 if (!gtk_cell_renderer_get_visible (info->renderer))
743 gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
746 &sizes[i].minimum_size,
747 &sizes[i].natural_size);
749 avail_size -= sizes[i].minimum_size;
751 sizes[i].data = info;
756 /* Naturally distribute the allocation */
757 avail_size -= (nvisible - 1) * priv->spacing;
759 avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
763 /* Calculate/distribute expand for cells */
766 extra_size = avail_size / nexpand;
767 extra_extra = avail_size % nexpand;
770 extra_size = extra_extra = 0;
772 /* Create the allocated cells */
773 for (i = 0; i < nvisible; i++)
775 CellInfo *info = sizes[i].data;
780 sizes[i].minimum_size += extra_size;
783 sizes[i].minimum_size++;
789 cell = allocated_cell_new (info->renderer,
790 full_size - (position + sizes[i].minimum_size),
791 sizes[i].minimum_size);
793 cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
795 allocated_cells = g_slist_prepend (allocated_cells, cell);
797 position += sizes[i].minimum_size;
798 position += priv->spacing;
804 /* Note it might not be important to reverse the list here at all,
805 * we have the correct positions, no need to allocate from left to right
807 return g_slist_reverse (allocated_cells);
810 /* Returns an allocation for each cell in the orientation of the box,
811 * used in ->render()/->event() implementations to get a straight-forward
812 * list of allocated cells to operate on.
815 get_allocated_cells (GtkCellAreaBox *box,
816 GtkCellAreaBoxContext *context,
821 GtkCellAreaBoxAllocation *group_allocs;
822 GtkCellArea *area = GTK_CELL_AREA (box);
823 GtkCellAreaBoxPrivate *priv = box->priv;
825 GSList *allocated_cells = NULL;
826 gint i, j, n_allocs, position;
827 gint for_size, full_size;
830 group_allocs = _gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
832 return allocate_cells_manually (box, widget, width, height);
834 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
845 /* For vertical oriented boxes, we just let the cell renderers
846 * realign themselves for rtl
848 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
849 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
851 for (position = 0, i = 0; i < n_allocs; i++)
853 /* We dont always allocate all groups, sometimes the requested
854 * group has only invisible cells for every row, hence the usage
855 * of group_allocs[i].group_idx here
857 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
859 /* Exception for single cell groups */
860 if (group->n_cells == 1)
862 CellInfo *info = group->cells->data;
864 gint cell_position, cell_size;
866 if (!gtk_cell_renderer_get_visible (info->renderer))
869 /* If were not aligned, place the cell after the last cell */
871 position = cell_position = group_allocs[i].position;
873 cell_position = position;
875 /* If not a fixed size, use only the requested size for this row */
877 cell_size = group_allocs[i].size;
881 gtk_cell_area_request_renderer (area, info->renderer,
886 cell_size = MIN (cell_size, group_allocs[i].size);
890 cell = allocated_cell_new (info->renderer,
891 full_size - (cell_position + cell_size), cell_size);
893 cell = allocated_cell_new (info->renderer, cell_position, cell_size);
895 position += cell_size;
896 position += priv->spacing;
898 allocated_cells = g_slist_prepend (allocated_cells, cell);
902 GtkRequestedSize *sizes;
903 gint avail_size, cell_position;
904 gint visible_cells, expand_cells;
905 gint extra_size, extra_extra;
907 visible_cells = count_visible_cells (group, &expand_cells);
909 /* If this row has no visible cells in this group, just
910 * skip the allocation
912 if (visible_cells == 0)
915 /* If were not aligned, place the cell after the last cell
916 * and eat up the extra space
920 avail_size = group_allocs[i].size;
921 position = cell_position = group_allocs[i].position;
925 avail_size = group_allocs[i].size + (group_allocs[i].position - position);
926 cell_position = position;
929 sizes = g_new (GtkRequestedSize, visible_cells);
931 for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
933 CellInfo *info = cell_list->data;
935 if (!gtk_cell_renderer_get_visible (info->renderer))
938 gtk_cell_area_request_renderer (area, info->renderer,
941 &sizes[j].minimum_size,
942 &sizes[j].natural_size);
944 sizes[j].data = info;
945 avail_size -= sizes[j].minimum_size;
950 /* Distribute cells naturally within the group */
951 avail_size -= (visible_cells - 1) * priv->spacing;
953 avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
957 /* Calculate/distribute expand for cells */
958 if (expand_cells > 0)
960 extra_size = avail_size / expand_cells;
961 extra_extra = avail_size % expand_cells;
964 extra_size = extra_extra = 0;
966 /* Create the allocated cells (loop only over visible cells here) */
967 for (j = 0; j < visible_cells; j++)
969 CellInfo *info = sizes[j].data;
974 sizes[j].minimum_size += extra_size;
977 sizes[j].minimum_size++;
983 cell = allocated_cell_new (info->renderer,
984 full_size - (cell_position + sizes[j].minimum_size),
985 sizes[j].minimum_size);
987 cell = allocated_cell_new (info->renderer, cell_position, sizes[j].minimum_size);
989 allocated_cells = g_slist_prepend (allocated_cells, cell);
991 cell_position += sizes[j].minimum_size;
992 cell_position += priv->spacing;
997 position = cell_position;
1001 g_free (group_allocs);
1003 /* Note it might not be important to reverse the list here at all,
1004 * we have the correct positions, no need to allocate from left to right
1006 return g_slist_reverse (allocated_cells);
1011 gtk_cell_area_box_focus_changed (GtkCellArea *area,
1013 GtkCellAreaBox *box)
1015 if (gtk_cell_area_get_focus_cell (area))
1016 box->priv->last_focus_cell = gtk_cell_area_get_focus_cell (area);
1019 /*************************************************************
1021 *************************************************************/
1023 gtk_cell_area_box_finalize (GObject *object)
1025 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1026 GtkCellAreaBoxPrivate *priv = box->priv;
1029 /* Unref/free the context list */
1030 for (l = priv->contexts; l; l = l->next)
1031 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
1033 g_slist_free (priv->contexts);
1034 priv->contexts = NULL;
1036 /* Free the cell grouping info */
1037 cell_groups_clear (box);
1038 g_array_free (priv->groups, TRUE);
1040 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
1044 gtk_cell_area_box_dispose (GObject *object)
1046 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
1050 gtk_cell_area_box_set_property (GObject *object,
1052 const GValue *value,
1055 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1059 case PROP_ORIENTATION:
1060 box->priv->orientation = g_value_get_enum (value);
1062 /* Notify that size needs to be requested again */
1063 reset_contexts (box);
1067 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
1070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1076 gtk_cell_area_box_get_property (GObject *object,
1081 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1085 case PROP_ORIENTATION:
1086 g_value_set_enum (value, box->priv->orientation);
1089 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
1092 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1097 /*************************************************************
1098 * GtkCellAreaClass *
1099 *************************************************************/
1101 gtk_cell_area_box_add (GtkCellArea *area,
1102 GtkCellRenderer *renderer)
1104 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
1105 renderer, FALSE, FALSE, TRUE);
1109 gtk_cell_area_box_remove (GtkCellArea *area,
1110 GtkCellRenderer *renderer)
1112 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1113 GtkCellAreaBoxPrivate *priv = box->priv;
1116 if (priv->last_focus_cell == renderer)
1117 priv->last_focus_cell = NULL;
1119 node = g_list_find_custom (priv->cells, renderer,
1120 (GCompareFunc)cell_info_find);
1124 CellInfo *info = node->data;
1126 cell_info_free (info);
1128 priv->cells = g_list_delete_link (priv->cells, node);
1130 /* Reconstruct cell groups */
1131 cell_groups_rebuild (box);
1134 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1138 gtk_cell_area_box_foreach (GtkCellArea *area,
1139 GtkCellCallback callback,
1140 gpointer callback_data)
1142 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1143 GtkCellAreaBoxPrivate *priv = box->priv;
1146 for (list = priv->cells; list; list = list->next)
1148 CellInfo *info = list->data;
1150 if (callback (info->renderer, callback_data))
1156 gtk_cell_area_box_foreach_alloc (GtkCellArea *area,
1157 GtkCellAreaContext *context,
1159 const GdkRectangle *cell_area,
1160 const GdkRectangle *background_area,
1161 GtkCellAllocCallback callback,
1162 gpointer callback_data)
1164 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1165 GtkCellAreaBoxPrivate *priv = box->priv;
1166 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1167 GSList *allocated_cells, *l;
1168 GdkRectangle cell_alloc, cell_background;
1171 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1172 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1174 cell_alloc = *cell_area;
1176 /* Get a list of cells with allocation sizes decided regardless
1177 * of alignments and pack order etc.
1179 allocated_cells = get_allocated_cells (box, box_context, widget,
1180 cell_area->width, cell_area->height);
1182 for (l = allocated_cells; l; l = l->next)
1184 AllocatedCell *cell = l->data;
1186 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1188 cell_alloc.x = cell_area->x + cell->position;
1189 cell_alloc.width = cell->size;
1193 cell_alloc.y = cell_area->y + cell->position;
1194 cell_alloc.height = cell->size;
1197 /* Stop iterating over cells if they flow out of the render
1198 * area, this can happen because the render area can actually
1199 * be smaller than the requested area (treeview columns can
1200 * be user resizable and can be resized to be smaller than
1201 * the actual requested area).
1203 if (cell_alloc.x > cell_area->x + cell_area->width ||
1204 cell_alloc.x + cell_alloc.width < cell_area->x ||
1205 cell_alloc.y > cell_area->y + cell_area->height)
1208 /* Special case for the last cell (or first cell in rtl)...
1209 * let the last cell consume the remaining space in the area
1210 * (the last cell is allowed to consume the remaining space if
1211 * the space given for rendering is actually larger than allocation,
1212 * this can happen in the expander GtkTreeViewColumn where only the
1213 * deepest depth column receives the allocation... shallow columns
1214 * receive more width). */
1219 /* Fill the leading space for the first cell in the area
1220 * (still last in the list)
1222 cell_alloc.width = (cell_alloc.x - cell_area->x) + cell_alloc.width;
1223 cell_alloc.x = cell_area->x;
1227 cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1228 cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1233 /* If the cell we are rendering doesnt fit into the remaining space,
1234 * clip it so that the underlying renderer has a chance to deal with
1235 * it (for instance text renderers get a chance to ellipsize).
1237 if (cell_alloc.x + cell_alloc.width > cell_area->x + cell_area->width)
1238 cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1240 if (cell_alloc.y + cell_alloc.height > cell_area->y + cell_area->height)
1241 cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1244 /* Add portions of the background_area to the cell_alloc
1245 * to create the cell_background
1247 cell_background = cell_alloc;
1249 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1251 if (l == allocated_cells)
1253 /* Add the depth to the first cell */
1256 cell_background.width += background_area->width - cell_area->width;
1257 cell_background.x = background_area->x + background_area->width - cell_background.width;
1261 cell_background.width += cell_area->x - background_area->x;
1262 cell_background.x = background_area->x;
1266 if (l->next == NULL)
1268 /* Grant this cell the remaining space */
1269 int remain = cell_background.x - background_area->x;
1272 cell_background.x -= remain;
1274 cell_background.width = background_area->width - remain;
1277 cell_background.y = background_area->y;
1278 cell_background.height = background_area->height;
1282 if (l == allocated_cells)
1284 cell_background.height += cell_background.y - background_area->y;
1285 cell_background.y = background_area->y;
1288 if (l->next == NULL)
1289 cell_background.height =
1290 background_area->height - (cell_background.y - background_area->y);
1292 cell_background.x = background_area->x;
1293 cell_background.width = background_area->width;
1296 if (callback (cell->renderer, &cell_alloc, &cell_background, callback_data))
1300 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1301 g_slist_free (allocated_cells);
1305 gtk_cell_area_box_apply_attributes (GtkCellArea *area,
1306 GtkTreeModel *tree_model,
1308 gboolean is_expander,
1309 gboolean is_expanded)
1311 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1312 GtkCellAreaBoxPrivate *priv = box->priv;
1315 /* Call the parent class to apply the attributes */
1317 (gtk_cell_area_box_parent_class)->apply_attributes (area, tree_model, iter,
1318 is_expander, is_expanded);
1320 /* Update visible state for cell groups */
1321 for (i = 0; i < priv->groups->len; i++)
1323 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1326 group->visible = FALSE;
1328 for (list = group->cells; list && group->visible == FALSE; list = list->next)
1330 CellInfo *info = list->data;
1332 if (gtk_cell_renderer_get_visible (info->renderer))
1333 group->visible = TRUE;
1339 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
1340 GtkCellRenderer *renderer,
1342 const GValue *value,
1345 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1346 GtkCellAreaBoxPrivate *priv = box->priv;
1349 gboolean rebuild = FALSE;
1351 GtkPackType pack_type;
1353 node = g_list_find_custom (priv->cells, renderer,
1354 (GCompareFunc)cell_info_find);
1362 case CELL_PROP_EXPAND:
1363 val = g_value_get_boolean (value);
1365 if (info->expand != val)
1372 case CELL_PROP_ALIGN:
1373 val = g_value_get_boolean (value);
1375 if (info->align != val)
1382 case CELL_PROP_FIXED_SIZE:
1383 val = g_value_get_boolean (value);
1385 if (info->fixed != val)
1392 case CELL_PROP_PACK_TYPE:
1393 pack_type = g_value_get_enum (value);
1395 if (info->pack != pack_type)
1397 info->pack = pack_type;
1402 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1406 /* Groups need to be rebuilt */
1408 cell_groups_rebuild (box);
1412 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
1413 GtkCellRenderer *renderer,
1418 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1419 GtkCellAreaBoxPrivate *priv = box->priv;
1423 node = g_list_find_custom (priv->cells, renderer,
1424 (GCompareFunc)cell_info_find);
1432 case CELL_PROP_EXPAND:
1433 g_value_set_boolean (value, info->expand);
1436 case CELL_PROP_ALIGN:
1437 g_value_set_boolean (value, info->align);
1440 case CELL_PROP_FIXED_SIZE:
1441 g_value_set_boolean (value, info->fixed);
1444 case CELL_PROP_PACK_TYPE:
1445 g_value_set_enum (value, info->pack);
1448 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1454 static GtkCellAreaContext *
1455 gtk_cell_area_box_create_context (GtkCellArea *area)
1457 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1458 GtkCellAreaBoxPrivate *priv = box->priv;
1459 GtkCellAreaContext *context =
1460 (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
1461 "area", area, NULL);
1463 priv->contexts = g_slist_prepend (priv->contexts, context);
1465 g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1467 /* Tell the new group about our cell layout */
1468 init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1473 static GtkCellAreaContext *
1474 gtk_cell_area_box_copy_context (GtkCellArea *area,
1475 GtkCellAreaContext *context)
1477 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1478 GtkCellAreaBoxPrivate *priv = box->priv;
1479 GtkCellAreaContext *copy =
1480 (GtkCellAreaContext *)_gtk_cell_area_box_context_copy (GTK_CELL_AREA_BOX (area),
1481 GTK_CELL_AREA_BOX_CONTEXT (context));
1483 priv->contexts = g_slist_prepend (priv->contexts, copy);
1485 g_object_weak_ref (G_OBJECT (copy), (GWeakNotify)context_weak_notify, box);
1490 static GtkSizeRequestMode
1491 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1493 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1494 GtkCellAreaBoxPrivate *priv = box->priv;
1496 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1497 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1498 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1502 compute_size (GtkCellAreaBox *box,
1503 GtkOrientation orientation,
1504 GtkCellAreaBoxContext *context,
1510 GtkCellAreaBoxPrivate *priv = box->priv;
1511 GtkCellArea *area = GTK_CELL_AREA (box);
1517 for (i = 0; i < priv->groups->len; i++)
1519 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1520 gint group_min_size = 0;
1521 gint group_nat_size = 0;
1523 for (list = group->cells; list; list = list->next)
1525 CellInfo *info = list->data;
1526 gint renderer_min_size, renderer_nat_size;
1528 if (!gtk_cell_renderer_get_visible (info->renderer))
1531 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1532 &renderer_min_size, &renderer_nat_size);
1534 if (orientation == priv->orientation)
1538 min_size += priv->spacing;
1539 nat_size += priv->spacing;
1542 if (group_min_size > 0)
1544 group_min_size += priv->spacing;
1545 group_nat_size += priv->spacing;
1548 min_size += renderer_min_size;
1549 nat_size += renderer_nat_size;
1550 group_min_size += renderer_min_size;
1551 group_nat_size += renderer_nat_size;
1555 min_size = MAX (min_size, renderer_min_size);
1556 nat_size = MAX (nat_size, renderer_nat_size);
1557 group_min_size = MAX (group_min_size, renderer_min_size);
1558 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1562 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1565 _gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1567 _gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1568 group_min_size, group_nat_size);
1573 _gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1575 _gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1576 group_min_size, group_nat_size);
1580 *minimum_size = min_size;
1581 *natural_size = nat_size;
1583 /* Update rtl state for focus navigation to work */
1584 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1585 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1588 static GtkRequestedSize *
1589 get_group_sizes (GtkCellArea *area,
1591 GtkOrientation orientation,
1595 GtkRequestedSize *sizes;
1599 *n_sizes = count_visible_cells (group, NULL);
1600 sizes = g_new (GtkRequestedSize, *n_sizes);
1602 for (l = group->cells, i = 0; l; l = l->next)
1604 CellInfo *info = l->data;
1606 if (!gtk_cell_renderer_get_visible (info->renderer))
1609 sizes[i].data = info;
1611 gtk_cell_area_request_renderer (area, info->renderer,
1612 orientation, widget, -1,
1613 &sizes[i].minimum_size,
1614 &sizes[i].natural_size);
1623 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1630 GtkCellAreaBoxPrivate *priv = box->priv;
1631 GtkCellArea *area = GTK_CELL_AREA (box);
1633 /* Exception for single cell groups */
1634 if (group->n_cells == 1)
1636 CellInfo *info = group->cells->data;
1638 gtk_cell_area_request_renderer (area, info->renderer,
1639 OPPOSITE_ORIENTATION (priv->orientation),
1640 widget, for_size, minimum_size, natural_size);
1644 GtkRequestedSize *orientation_sizes;
1647 gint avail_size = for_size;
1648 gint extra_size, extra_extra;
1649 gint min_size = 0, nat_size = 0;
1651 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1653 /* First naturally allocate the cells in the group into the for_size */
1654 avail_size -= (n_sizes - 1) * priv->spacing;
1655 for (i = 0; i < n_sizes; i++)
1656 avail_size -= orientation_sizes[i].minimum_size;
1659 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1663 /* Calculate/distribute expand for cells */
1664 if (group->expand_cells > 0)
1666 extra_size = avail_size / group->expand_cells;
1667 extra_extra = avail_size % group->expand_cells;
1670 extra_size = extra_extra = 0;
1672 for (i = 0; i < n_sizes; i++)
1674 gint cell_min, cell_nat;
1676 info = orientation_sizes[i].data;
1680 orientation_sizes[i].minimum_size += extra_size;
1683 orientation_sizes[i].minimum_size++;
1688 gtk_cell_area_request_renderer (area, info->renderer,
1689 OPPOSITE_ORIENTATION (priv->orientation),
1691 orientation_sizes[i].minimum_size,
1692 &cell_min, &cell_nat);
1694 min_size = MAX (min_size, cell_min);
1695 nat_size = MAX (nat_size, cell_nat);
1698 *minimum_size = min_size;
1699 *natural_size = nat_size;
1701 g_free (orientation_sizes);
1706 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1707 GtkCellAreaBoxContext *context,
1713 GtkCellAreaBoxPrivate *priv = box->priv;
1715 GtkRequestedSize *orientation_sizes;
1716 gint n_groups, n_expand_groups, i;
1717 gint avail_size = for_size;
1718 gint extra_size, extra_extra;
1719 gint min_size = 0, nat_size = 0;
1721 n_expand_groups = count_expand_groups (box);
1723 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1724 orientation_sizes = _gtk_cell_area_box_context_get_widths (context, &n_groups);
1726 orientation_sizes = _gtk_cell_area_box_context_get_heights (context, &n_groups);
1728 /* First start by naturally allocating space among groups of cells */
1729 avail_size -= (n_groups - 1) * priv->spacing;
1730 for (i = 0; i < n_groups; i++)
1731 avail_size -= orientation_sizes[i].minimum_size;
1734 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1738 /* Calculate/distribute expand for groups */
1739 if (n_expand_groups > 0)
1741 extra_size = avail_size / n_expand_groups;
1742 extra_extra = avail_size % n_expand_groups;
1745 extra_size = extra_extra = 0;
1747 /* Now we need to naturally allocate sizes for cells in each group
1748 * and push the height-for-width for each group accordingly while
1749 * accumulating the overall height-for-width for this row.
1751 for (i = 0; i < n_groups; i++)
1753 gint group_min, group_nat;
1754 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1756 group = &g_array_index (priv->groups, CellGroup, group_idx);
1758 if (group->expand_cells > 0)
1760 orientation_sizes[i].minimum_size += extra_size;
1763 orientation_sizes[i].minimum_size++;
1768 /* Now we have the allocation for the group,
1769 * request its height-for-width
1771 compute_group_size_for_opposing_orientation (box, group, widget,
1772 orientation_sizes[i].minimum_size,
1773 &group_min, &group_nat);
1775 min_size = MAX (min_size, group_min);
1776 nat_size = MAX (nat_size, group_nat);
1778 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1780 _gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1781 group_min, group_nat);
1785 _gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1786 group_min, group_nat);
1790 *minimum_size = min_size;
1791 *natural_size = nat_size;
1793 g_free (orientation_sizes);
1795 /* Update rtl state for focus navigation to work */
1796 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1797 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1803 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1804 GtkCellAreaContext *context,
1806 gint *minimum_width,
1807 gint *natural_width)
1809 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1810 GtkCellAreaBoxContext *box_context;
1811 gint min_width, nat_width;
1813 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1815 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1817 /* Compute the size of all renderers for current row data,
1818 * bumping cell alignments in the context along the way
1820 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1821 box_context, widget, -1, &min_width, &nat_width);
1824 *minimum_width = min_width;
1827 *natural_width = nat_width;
1831 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1832 GtkCellAreaContext *context,
1834 gint *minimum_height,
1835 gint *natural_height)
1837 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1838 GtkCellAreaBoxContext *box_context;
1839 gint min_height, nat_height;
1841 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1843 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1845 /* Compute the size of all renderers for current row data,
1846 * bumping cell alignments in the context along the way
1848 compute_size (box, GTK_ORIENTATION_VERTICAL,
1849 box_context, widget, -1, &min_height, &nat_height);
1852 *minimum_height = min_height;
1855 *natural_height = nat_height;
1859 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1860 GtkCellAreaContext *context,
1863 gint *minimum_height,
1864 gint *natural_height)
1866 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1867 GtkCellAreaBoxContext *box_context;
1868 GtkCellAreaBoxPrivate *priv;
1869 gint min_height, nat_height;
1871 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1873 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1876 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1878 /* Add up vertical requests of height for width and push
1879 * the overall cached sizes for alignments
1881 compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1885 /* Juice: virtually allocate cells into the for_width using the
1886 * alignments and then return the overall height for that width,
1889 compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1893 *minimum_height = min_height;
1896 *natural_height = nat_height;
1900 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1901 GtkCellAreaContext *context,
1904 gint *minimum_width,
1905 gint *natural_width)
1907 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1908 GtkCellAreaBoxContext *box_context;
1909 GtkCellAreaBoxPrivate *priv;
1910 gint min_width, nat_width;
1912 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1914 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1917 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1919 /* Add up horizontal requests of width for height and push
1920 * the overall cached sizes for alignments
1922 compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1926 /* Juice: horizontally allocate cells into the for_height using the
1927 * alignments and then return the overall width for that height,
1930 compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1934 *minimum_width = min_width;
1937 *natural_width = nat_width;
1948 gtk_cell_area_box_focus (GtkCellArea *area,
1949 GtkDirectionType direction)
1951 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1952 GtkCellAreaBoxPrivate *priv = box->priv;
1953 gint cycle = FOCUS_NONE;
1954 gboolean cycled_focus = FALSE;
1955 GtkCellRenderer *focus_cell;
1957 focus_cell = gtk_cell_area_get_focus_cell (area);
1959 /* Special case, when there is no activatable cell, focus
1960 * is painted around the entire area... in this case we
1961 * let focus leave the area directly.
1963 if (focus_cell && !gtk_cell_area_is_activatable (area))
1965 gtk_cell_area_set_focus_cell (area, NULL);
1971 case GTK_DIR_TAB_FORWARD:
1972 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1974 case GTK_DIR_TAB_BACKWARD:
1975 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1978 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1980 else if (!focus_cell)
1981 cycle = FOCUS_LAST_CELL;
1984 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1986 else if (!focus_cell)
1987 cycle = FOCUS_LAST_CELL;
1990 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1991 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1992 else if (!focus_cell)
1993 cycle = FOCUS_LAST_CELL;
1996 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1997 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1998 else if (!focus_cell)
1999 cycle = FOCUS_LAST_CELL;
2005 if (cycle == FOCUS_LAST_CELL)
2007 gtk_cell_area_set_focus_cell (area, priv->last_focus_cell);
2008 cycled_focus = TRUE;
2010 else if (cycle != FOCUS_NONE)
2012 gboolean found_cell = FALSE;
2016 /* If there is no focused cell, focus on the first (or last) one */
2020 for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
2021 cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
2022 i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
2024 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
2026 for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
2027 cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
2029 CellInfo *info = list->data;
2031 if (info->renderer == focus_cell)
2033 else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
2034 gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
2036 gtk_cell_area_set_focus_cell (area, info->renderer);
2037 cycled_focus = TRUE;
2044 gtk_cell_area_set_focus_cell (area, NULL);
2046 return cycled_focus;
2050 /*************************************************************
2051 * GtkCellLayoutIface *
2052 *************************************************************/
2054 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
2056 iface->pack_start = gtk_cell_area_box_layout_pack_start;
2057 iface->pack_end = gtk_cell_area_box_layout_pack_end;
2058 iface->reorder = gtk_cell_area_box_layout_reorder;
2062 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
2063 GtkCellRenderer *renderer,
2066 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2070 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
2071 GtkCellRenderer *renderer,
2074 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2078 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
2079 GtkCellRenderer *renderer,
2082 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
2083 GtkCellAreaBoxPrivate *priv = box->priv;
2087 node = g_list_find_custom (priv->cells, renderer,
2088 (GCompareFunc)cell_info_find);
2094 priv->cells = g_list_delete_link (priv->cells, node);
2095 priv->cells = g_list_insert (priv->cells, info, position);
2097 cell_groups_rebuild (box);
2101 /*************************************************************
2102 * Private interaction with GtkCellAreaBoxContext *
2103 *************************************************************/
2105 _gtk_cell_area_box_group_visible (GtkCellAreaBox *box,
2108 GtkCellAreaBoxPrivate *priv = box->priv;
2111 g_assert (group_idx >= 0 && group_idx < priv->groups->len);
2113 group = &g_array_index (priv->groups, CellGroup, group_idx);
2115 return group->visible;
2119 /*************************************************************
2121 *************************************************************/
2123 * gtk_cell_area_box_new:
2125 * Creates a new #GtkCellAreaBox.
2127 * Return value: a newly created #GtkCellAreaBox
2132 gtk_cell_area_box_new (void)
2134 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
2138 * gtk_cell_area_box_pack_start:
2139 * @box: a #GtkCellAreaBox
2140 * @renderer: the #GtkCellRenderer to add
2141 * @expand: whether @renderer should receive extra space when the area receives
2142 * more than its natural size
2143 * @align: whether @renderer should be aligned in adjacent rows
2144 * @fixed: whether @renderer should have the same size in all rows
2146 * Adds @renderer to @box, packed with reference to the start of @box.
2148 * The @renderer is packed after any other #GtkCellRenderer packed
2149 * with reference to the start of @box.
2154 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
2155 GtkCellRenderer *renderer,
2160 GtkCellAreaBoxPrivate *priv;
2163 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2164 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2168 if (g_list_find_custom (priv->cells, renderer,
2169 (GCompareFunc)cell_info_find))
2171 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
2175 info = cell_info_new (renderer, GTK_PACK_START, expand, align, fixed);
2177 priv->cells = g_list_append (priv->cells, info);
2179 cell_groups_rebuild (box);
2183 * gtk_cell_area_box_pack_end:
2184 * @box: a #GtkCellAreaBox
2185 * @renderer: the #GtkCellRenderer to add
2186 * @expand: whether @renderer should receive extra space when the area receives
2187 * more than its natural size
2188 * @align: whether @renderer should be aligned in adjacent rows
2189 * @fixed: whether @renderer should have the same size in all rows
2191 * Adds @renderer to @box, packed with reference to the end of @box.
2193 * The @renderer is packed after (away from end of) any other
2194 * #GtkCellRenderer packed with reference to the end of @box.
2199 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
2200 GtkCellRenderer *renderer,
2205 GtkCellAreaBoxPrivate *priv;
2208 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2209 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2213 if (g_list_find_custom (priv->cells, renderer,
2214 (GCompareFunc)cell_info_find))
2216 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
2220 info = cell_info_new (renderer, GTK_PACK_END, expand, align, fixed);
2222 priv->cells = g_list_append (priv->cells, info);
2224 cell_groups_rebuild (box);
2228 * gtk_cell_area_box_get_spacing:
2229 * @box: a #GtkCellAreaBox
2231 * Gets the spacing added between cell renderers.
2233 * Return value: the space added between cell renderers in @box.
2238 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
2240 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2242 return box->priv->spacing;
2246 * gtk_cell_area_box_set_spacing:
2247 * @box: a #GtkCellAreaBox
2248 * @spacing: the space to add between #GtkCellRenderers
2250 * Sets the spacing to add between cell renderers in @box.
2255 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
2258 GtkCellAreaBoxPrivate *priv;
2260 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2264 if (priv->spacing != spacing)
2266 priv->spacing = spacing;
2268 g_object_notify (G_OBJECT (box), "spacing");
2270 /* Notify that size needs to be requested again */
2271 reset_contexts (box);