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, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * SECTION:gtkcellareabox
27 * @Short_Description: A cell area that renders GtkCellRenderers
28 * into a row or a column
29 * @Title: GtkCellAreaBox
31 * The #GtkCellAreaBox renders cell renderers into a row or a column
32 * depending on its #GtkOrientation.
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
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().
52 #include "gtkorientable.h"
53 #include "gtkcelllayout.h"
54 #include "gtkcellareabox.h"
55 #include "gtkcellareaboxcontext.h"
56 #include "gtktypebuiltins.h"
57 #include "gtkprivate.h"
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,
67 static void gtk_cell_area_box_get_property (GObject *object,
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,
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,
91 gboolean is_expanded);
92 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
93 GtkCellRenderer *renderer,
97 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
98 GtkCellRenderer *renderer,
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,
110 gint *natural_width);
111 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
112 GtkCellAreaContext *context,
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,
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,
127 gint *natural_width);
128 static gboolean gtk_cell_area_box_focus (GtkCellArea *area,
129 GtkDirectionType direction);
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,
136 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
137 GtkCellRenderer *renderer,
139 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
140 GtkCellRenderer *renderer,
142 static void gtk_cell_area_box_focus_changed (GtkCellArea *area,
144 GtkCellAreaBox *box);
147 /* CellInfo/CellGroup metadata handling and convenience functions */
149 GtkCellRenderer *renderer;
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 */
162 guint expand_cells : 8;
168 GtkCellRenderer *renderer;
174 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
179 static void cell_info_free (CellInfo *info);
180 static gint cell_info_find (CellInfo *info,
181 GtkCellRenderer *renderer);
183 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
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,
202 struct _GtkCellAreaBoxPrivate
204 GtkOrientation orientation;
206 /* We hold on to the previously focused cell when navigating
207 * up and down in a horizontal box (or left and right on a vertical one)
208 * this way we always re-enter the last focused cell.
210 GtkCellRenderer *last_focus_cell;
211 gulong focus_cell_id;
220 /* We hold on to the rtl state from a widget we are requested for
221 * so that we can navigate focus correctly
236 CELL_PROP_FIXED_SIZE,
240 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
241 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
242 gtk_cell_area_box_cell_layout_init)
243 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
245 #define OPPOSITE_ORIENTATION(orientation) \
246 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
247 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
250 gtk_cell_area_box_init (GtkCellAreaBox *box)
252 GtkCellAreaBoxPrivate *priv;
254 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
255 GTK_TYPE_CELL_AREA_BOX,
256 GtkCellAreaBoxPrivate);
259 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
260 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
262 priv->contexts = NULL;
266 /* Watch whenever focus is given to a cell, even if it's not with keynav,
267 * this way we remember upon entry of the area where focus was last time
270 priv->focus_cell_id = g_signal_connect (box, "notify::focus-cell",
271 G_CALLBACK (gtk_cell_area_box_focus_changed), box);
275 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
277 GObjectClass *object_class = G_OBJECT_CLASS (class);
278 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
281 object_class->finalize = gtk_cell_area_box_finalize;
282 object_class->dispose = gtk_cell_area_box_dispose;
283 object_class->set_property = gtk_cell_area_box_set_property;
284 object_class->get_property = gtk_cell_area_box_get_property;
286 /* GtkCellAreaClass */
287 area_class->add = gtk_cell_area_box_add;
288 area_class->remove = gtk_cell_area_box_remove;
289 area_class->foreach = gtk_cell_area_box_foreach;
290 area_class->foreach_alloc = gtk_cell_area_box_foreach_alloc;
291 area_class->apply_attributes = gtk_cell_area_box_apply_attributes;
292 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
293 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
295 area_class->create_context = gtk_cell_area_box_create_context;
296 area_class->copy_context = gtk_cell_area_box_copy_context;
297 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
298 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
299 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
300 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
301 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
303 area_class->focus = gtk_cell_area_box_focus;
306 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
309 * GtkCellAreaBox:spacing:
311 * The amount of space to reserve between cells.
315 g_object_class_install_property (object_class,
317 g_param_spec_int ("spacing",
319 P_("Space which is inserted between cells"),
323 GTK_PARAM_READWRITE));
325 /* Cell Properties */
327 * GtkCellAreaBox:expand:
329 * Whether the cell renderer should receive extra space
330 * when the area receives more than its natural size.
334 gtk_cell_area_class_install_cell_property (area_class,
339 P_("Whether the cell expands"),
341 GTK_PARAM_READWRITE));
344 * GtkCellAreaBox:align:
346 * Whether the cell renderer should be aligned in adjacent rows.
350 gtk_cell_area_class_install_cell_property (area_class,
355 P_("Whether cell should align with adjacent rows"),
357 GTK_PARAM_READWRITE));
360 * GtkCellAreaBox:fixed-size:
362 * Whether the cell renderer should require the same size
363 * for all rows for which it was requested.
367 gtk_cell_area_class_install_cell_property (area_class,
368 CELL_PROP_FIXED_SIZE,
372 P_("Whether cells should be the same size in all rows"),
374 GTK_PARAM_READWRITE));
377 * GtkCellAreaBox:pack-type:
379 * A GtkPackType indicating whether the cell renderer is packed
380 * with reference to the start or end of the area.
384 gtk_cell_area_class_install_cell_property (area_class,
389 P_("A GtkPackType indicating whether the cell is packed with "
390 "reference to the start or end of the cell area"),
391 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
392 GTK_PARAM_READWRITE));
394 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
398 /*************************************************************
399 * CellInfo/CellGroup basics and convenience functions *
400 *************************************************************/
402 cell_info_new (GtkCellRenderer *renderer,
408 CellInfo *info = g_slice_new (CellInfo);
410 info->renderer = g_object_ref_sink (renderer);
412 info->expand = expand;
420 cell_info_free (CellInfo *info)
422 g_object_unref (info->renderer);
424 g_slice_free (CellInfo, info);
428 cell_info_find (CellInfo *info,
429 GtkCellRenderer *renderer)
431 return (info->renderer == renderer) ? 0 : -1;
434 static AllocatedCell *
435 allocated_cell_new (GtkCellRenderer *renderer,
439 AllocatedCell *cell = g_slice_new (AllocatedCell);
441 cell->renderer = renderer;
442 cell->position = position;
449 allocated_cell_free (AllocatedCell *cell)
451 g_slice_free (AllocatedCell, cell);
455 list_consecutive_cells (GtkCellAreaBox *box)
457 GtkCellAreaBoxPrivate *priv = box->priv;
458 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
461 /* List cells in consecutive order taking their
462 * PACK_START/PACK_END options into account
464 for (l = priv->cells; l; l = l->next)
468 if (info->pack == GTK_PACK_START)
469 consecutive_cells = g_list_prepend (consecutive_cells, info);
472 for (l = priv->cells; l; l = l->next)
476 if (info->pack == GTK_PACK_END)
477 pack_end_cells = g_list_prepend (pack_end_cells, info);
480 consecutive_cells = g_list_reverse (consecutive_cells);
481 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
483 return consecutive_cells;
487 cell_groups_clear (GtkCellAreaBox *box)
489 GtkCellAreaBoxPrivate *priv = box->priv;
492 for (i = 0; i < priv->groups->len; i++)
494 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
496 g_list_free (group->cells);
499 g_array_set_size (priv->groups, 0);
503 cell_groups_rebuild (GtkCellAreaBox *box)
505 GtkCellAreaBoxPrivate *priv = box->priv;
506 CellGroup group = { 0, };
507 CellGroup *group_ptr;
510 gboolean last_cell_fixed = FALSE;
512 cell_groups_clear (box);
517 cells = list_consecutive_cells (box);
519 /* First group is implied */
520 g_array_append_val (priv->groups, group);
521 group_ptr = &g_array_index (priv->groups, CellGroup, id);
523 for (l = cells; l; l = l->next)
525 CellInfo *info = l->data;
527 /* A new group starts with any aligned cell, or
528 * at the beginning and end of a fixed size cell.
529 * the first group is implied */
530 if ((info->align || info->fixed || last_cell_fixed) && l != cells)
532 memset (&group, 0x0, sizeof (CellGroup));
535 g_array_append_val (priv->groups, group);
536 group_ptr = &g_array_index (priv->groups, CellGroup, id);
539 group_ptr->cells = g_list_prepend (group_ptr->cells, info);
540 group_ptr->n_cells++;
542 /* Not every group is aligned, some are floating
543 * fixed size cells */
545 group_ptr->align = TRUE;
547 /* A group expands if it contains any expand cells */
549 group_ptr->expand_cells++;
551 last_cell_fixed = info->fixed;
556 for (id = 0; id < priv->groups->len; id++)
558 group_ptr = &g_array_index (priv->groups, CellGroup, id);
560 group_ptr->cells = g_list_reverse (group_ptr->cells);
563 /* Contexts need to be updated with the new grouping information */
564 init_context_groups (box);
568 count_visible_cells (CellGroup *group,
572 gint visible_cells = 0;
573 gint n_expand_cells = 0;
575 for (l = group->cells; l; l = l->next)
577 CellInfo *info = l->data;
579 if (gtk_cell_renderer_get_visible (info->renderer))
589 *expand_cells = n_expand_cells;
591 return visible_cells;
595 count_expand_groups (GtkCellAreaBox *box)
597 GtkCellAreaBoxPrivate *priv = box->priv;
599 gint expand_groups = 0;
601 for (i = 0; i < priv->groups->len; i++)
603 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
605 if (group->expand_cells > 0)
609 return expand_groups;
613 context_weak_notify (GtkCellAreaBox *box,
614 GtkCellAreaBoxContext *dead_context)
616 GtkCellAreaBoxPrivate *priv = box->priv;
618 priv->contexts = g_slist_remove (priv->contexts, dead_context);
622 init_context_group (GtkCellAreaBox *box,
623 GtkCellAreaBoxContext *context)
625 GtkCellAreaBoxPrivate *priv = box->priv;
626 gint *expand_groups, *align_groups, i;
628 expand_groups = g_new (gboolean, priv->groups->len);
629 align_groups = g_new (gboolean, priv->groups->len);
631 for (i = 0; i < priv->groups->len; i++)
633 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
635 expand_groups[i] = (group->expand_cells > 0);
636 align_groups[i] = group->align;
639 /* This call implies reseting the request info */
640 gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups, align_groups);
641 g_free (expand_groups);
642 g_free (align_groups);
646 init_context_groups (GtkCellAreaBox *box)
648 GtkCellAreaBoxPrivate *priv = box->priv;
651 /* When the box's groups are reconstructed,
652 * contexts need to be reinitialized.
654 for (l = priv->contexts; l; l = l->next)
656 GtkCellAreaBoxContext *context = l->data;
658 init_context_group (box, context);
663 reset_contexts (GtkCellAreaBox *box)
665 GtkCellAreaBoxPrivate *priv = box->priv;
668 /* When the box layout changes, contexts need to
669 * be reset and sizes for the box get requested again
671 for (l = priv->contexts; l; l = l->next)
673 GtkCellAreaContext *context = l->data;
675 gtk_cell_area_context_reset (context);
679 /* Fall back on a completely unaligned dynamic allocation of cells
680 * when not allocated for the said orientation, alignment of cells
681 * is not done when each area gets a different size in the orientation
685 allocate_cells_manually (GtkCellAreaBox *box,
690 GtkCellAreaBoxPrivate *priv = box->priv;
692 GSList *allocated_cells = NULL;
693 GtkRequestedSize *sizes;
695 gint nvisible = 0, nexpand = 0, group_expand;
696 gint avail_size, extra_size, extra_extra, full_size;
697 gint position = 0, for_size;
703 /* For vertical oriented boxes, we just let the cell renderers
704 * realign themselves for rtl
706 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
707 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
709 cells = list_consecutive_cells (box);
711 /* Count the visible and expand cells */
712 for (i = 0; i < priv->groups->len; i++)
714 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
716 nvisible += count_visible_cells (group, &group_expand);
717 nexpand += group_expand;
726 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
728 full_size = avail_size = width;
733 full_size = avail_size = height;
737 /* Go ahead and collect the requests on the fly */
738 sizes = g_new0 (GtkRequestedSize, nvisible);
739 for (l = cells, i = 0; l; l = l->next)
741 CellInfo *info = l->data;
743 if (!gtk_cell_renderer_get_visible (info->renderer))
746 gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
749 &sizes[i].minimum_size,
750 &sizes[i].natural_size);
752 avail_size -= sizes[i].minimum_size;
754 sizes[i].data = info;
759 /* Naturally distribute the allocation */
760 avail_size -= (nvisible - 1) * priv->spacing;
762 avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
766 /* Calculate/distribute expand for cells */
769 extra_size = avail_size / nexpand;
770 extra_extra = avail_size % nexpand;
773 extra_size = extra_extra = 0;
775 /* Create the allocated cells */
776 for (i = 0; i < nvisible; i++)
778 CellInfo *info = sizes[i].data;
783 sizes[i].minimum_size += extra_size;
786 sizes[i].minimum_size++;
792 cell = allocated_cell_new (info->renderer,
793 full_size - (position + sizes[i].minimum_size),
794 sizes[i].minimum_size);
796 cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
798 allocated_cells = g_slist_prepend (allocated_cells, cell);
800 position += sizes[i].minimum_size;
801 position += priv->spacing;
807 /* Note it might not be important to reverse the list here at all,
808 * we have the correct positions, no need to allocate from left to right
810 return g_slist_reverse (allocated_cells);
813 /* Returns an allocation for each cell in the orientation of the box,
814 * used in ->render()/->event() implementations to get a straight-forward
815 * list of allocated cells to operate on.
818 get_allocated_cells (GtkCellAreaBox *box,
819 GtkCellAreaBoxContext *context,
824 GtkCellAreaBoxAllocation *group_allocs;
825 GtkCellArea *area = GTK_CELL_AREA (box);
826 GtkCellAreaBoxPrivate *priv = box->priv;
828 GSList *allocated_cells = NULL;
829 gint i, j, n_allocs, position;
830 gint for_size, full_size;
833 group_allocs = gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
835 return allocate_cells_manually (box, widget, width, height);
837 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
848 /* For vertical oriented boxes, we just let the cell renderers
849 * realign themselves for rtl
851 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
852 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
854 for (position = 0, i = 0; i < n_allocs; i++)
856 /* We dont always allocate all groups, sometimes the requested
857 * group has only invisible cells for every row, hence the usage
858 * of group_allocs[i].group_idx here
860 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
862 /* Exception for single cell groups */
863 if (group->n_cells == 1)
865 CellInfo *info = group->cells->data;
867 gint cell_position, cell_size;
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);
1066 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075 gtk_cell_area_box_get_property (GObject *object,
1080 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
1084 case PROP_ORIENTATION:
1085 g_value_set_enum (value, box->priv->orientation);
1088 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
1091 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1096 /*************************************************************
1097 * GtkCellAreaClass *
1098 *************************************************************/
1100 gtk_cell_area_box_add (GtkCellArea *area,
1101 GtkCellRenderer *renderer)
1103 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
1104 renderer, FALSE, FALSE, TRUE);
1108 gtk_cell_area_box_remove (GtkCellArea *area,
1109 GtkCellRenderer *renderer)
1111 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1112 GtkCellAreaBoxPrivate *priv = box->priv;
1115 if (priv->last_focus_cell == renderer)
1116 priv->last_focus_cell = NULL;
1118 node = g_list_find_custom (priv->cells, renderer,
1119 (GCompareFunc)cell_info_find);
1123 CellInfo *info = node->data;
1125 cell_info_free (info);
1127 priv->cells = g_list_delete_link (priv->cells, node);
1129 /* Reconstruct cell groups */
1130 cell_groups_rebuild (box);
1133 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1137 gtk_cell_area_box_foreach (GtkCellArea *area,
1138 GtkCellCallback callback,
1139 gpointer callback_data)
1141 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1142 GtkCellAreaBoxPrivate *priv = box->priv;
1145 for (list = priv->cells; list; list = list->next)
1147 CellInfo *info = list->data;
1149 if (callback (info->renderer, callback_data))
1155 gtk_cell_area_box_foreach_alloc (GtkCellArea *area,
1156 GtkCellAreaContext *context,
1158 const GdkRectangle *cell_area,
1159 const GdkRectangle *background_area,
1160 GtkCellAllocCallback callback,
1161 gpointer callback_data)
1163 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1164 GtkCellAreaBoxPrivate *priv = box->priv;
1165 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1166 GSList *allocated_cells, *l;
1167 GdkRectangle cell_alloc, cell_background;
1170 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1171 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1173 cell_alloc = *cell_area;
1175 /* Get a list of cells with allocation sizes decided regardless
1176 * of alignments and pack order etc.
1178 allocated_cells = get_allocated_cells (box, box_context, widget,
1179 cell_area->width, cell_area->height);
1181 for (l = allocated_cells; l; l = l->next)
1183 AllocatedCell *cell = l->data;
1185 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1187 cell_alloc.x = cell_area->x + cell->position;
1188 cell_alloc.width = cell->size;
1192 cell_alloc.y = cell_area->y + cell->position;
1193 cell_alloc.height = cell->size;
1196 /* Stop iterating over cells if they flow out of the render
1197 * area, this can happen because the render area can actually
1198 * be smaller than the requested area (treeview columns can
1199 * be user resizable and can be resized to be smaller than
1200 * the actual requested area).
1202 if (cell_alloc.x > cell_area->x + cell_area->width ||
1203 cell_alloc.x + cell_alloc.width < cell_area->x ||
1204 cell_alloc.y > cell_area->y + cell_area->height)
1207 /* Special case for the last cell (or first cell in rtl)...
1208 * let the last cell consume the remaining space in the area
1209 * (the last cell is allowed to consume the remaining space if
1210 * the space given for rendering is actually larger than allocation,
1211 * this can happen in the expander GtkTreeViewColumn where only the
1212 * deepest depth column receives the allocation... shallow columns
1213 * receive more width). */
1218 /* Fill the leading space for the first cell in the area
1219 * (still last in the list)
1221 cell_alloc.width = (cell_alloc.x - cell_area->x) + cell_alloc.width;
1222 cell_alloc.x = cell_area->x;
1226 cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1227 cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1232 /* If the cell we are rendering doesnt fit into the remaining space,
1233 * clip it so that the underlying renderer has a chance to deal with
1234 * it (for instance text renderers get a chance to ellipsize).
1236 if (cell_alloc.x + cell_alloc.width > cell_area->x + cell_area->width)
1237 cell_alloc.width = cell_area->x + cell_area->width - cell_alloc.x;
1239 if (cell_alloc.y + cell_alloc.height > cell_area->y + cell_area->height)
1240 cell_alloc.height = cell_area->y + cell_area->height - cell_alloc.y;
1243 /* Add portions of the background_area to the cell_alloc
1244 * to create the cell_background
1246 cell_background = cell_alloc;
1248 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1250 if (l == allocated_cells)
1252 /* Add the depth to the first cell */
1255 cell_background.width += background_area->width - cell_area->width;
1256 cell_background.x = background_area->x + background_area->width - cell_background.width;
1260 cell_background.width += cell_area->x - background_area->x;
1261 cell_background.x = background_area->x;
1265 if (l->next == NULL)
1267 /* Grant this cell the remaining space */
1268 int remain = cell_background.x - background_area->x;
1271 cell_background.x -= remain;
1273 cell_background.width = background_area->width - remain;
1276 cell_background.y = background_area->y;
1277 cell_background.height = background_area->height;
1281 if (l == allocated_cells)
1283 cell_background.height += cell_background.y - background_area->y;
1284 cell_background.y = background_area->y;
1287 if (l->next == NULL)
1288 cell_background.height =
1289 background_area->height - (cell_background.y - background_area->y);
1291 cell_background.x = background_area->x;
1292 cell_background.width = background_area->width;
1295 if (callback (cell->renderer, &cell_alloc, &cell_background, callback_data))
1299 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1300 g_slist_free (allocated_cells);
1304 gtk_cell_area_box_apply_attributes (GtkCellArea *area,
1305 GtkTreeModel *tree_model,
1307 gboolean is_expander,
1308 gboolean is_expanded)
1310 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1311 GtkCellAreaBoxPrivate *priv = box->priv;
1314 /* Call the parent class to apply the attributes */
1316 (gtk_cell_area_box_parent_class)->apply_attributes (area, tree_model, iter,
1317 is_expander, is_expanded);
1319 /* Update visible state for cell groups */
1320 for (i = 0; i < priv->groups->len; i++)
1322 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1325 group->visible = FALSE;
1327 for (list = group->cells; list && group->visible == FALSE; list = list->next)
1329 CellInfo *info = list->data;
1331 if (gtk_cell_renderer_get_visible (info->renderer))
1332 group->visible = TRUE;
1338 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
1339 GtkCellRenderer *renderer,
1341 const GValue *value,
1344 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1345 GtkCellAreaBoxPrivate *priv = box->priv;
1348 gboolean rebuild = FALSE;
1350 GtkPackType pack_type;
1352 node = g_list_find_custom (priv->cells, renderer,
1353 (GCompareFunc)cell_info_find);
1361 case CELL_PROP_EXPAND:
1362 val = g_value_get_boolean (value);
1364 if (info->expand != val)
1371 case CELL_PROP_ALIGN:
1372 val = g_value_get_boolean (value);
1374 if (info->align != val)
1381 case CELL_PROP_FIXED_SIZE:
1382 val = g_value_get_boolean (value);
1384 if (info->fixed != val)
1391 case CELL_PROP_PACK_TYPE:
1392 pack_type = g_value_get_enum (value);
1394 if (info->pack != pack_type)
1396 info->pack = pack_type;
1401 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1405 /* Groups need to be rebuilt */
1407 cell_groups_rebuild (box);
1411 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
1412 GtkCellRenderer *renderer,
1417 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1418 GtkCellAreaBoxPrivate *priv = box->priv;
1422 node = g_list_find_custom (priv->cells, renderer,
1423 (GCompareFunc)cell_info_find);
1431 case CELL_PROP_EXPAND:
1432 g_value_set_boolean (value, info->expand);
1435 case CELL_PROP_ALIGN:
1436 g_value_set_boolean (value, info->align);
1439 case CELL_PROP_FIXED_SIZE:
1440 g_value_set_boolean (value, info->fixed);
1443 case CELL_PROP_PACK_TYPE:
1444 g_value_set_enum (value, info->pack);
1447 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1453 static GtkCellAreaContext *
1454 gtk_cell_area_box_create_context (GtkCellArea *area)
1456 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1457 GtkCellAreaBoxPrivate *priv = box->priv;
1458 GtkCellAreaContext *context =
1459 (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
1460 "area", area, NULL);
1462 priv->contexts = g_slist_prepend (priv->contexts, context);
1464 g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1466 /* Tell the new group about our cell layout */
1467 init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1472 static GtkCellAreaContext *
1473 gtk_cell_area_box_copy_context (GtkCellArea *area,
1474 GtkCellAreaContext *context)
1476 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1477 GtkCellAreaBoxPrivate *priv = box->priv;
1478 GtkCellAreaContext *copy =
1479 (GtkCellAreaContext *)gtk_cell_area_box_context_copy (GTK_CELL_AREA_BOX (area),
1480 GTK_CELL_AREA_BOX_CONTEXT (context));
1482 priv->contexts = g_slist_prepend (priv->contexts, copy);
1484 g_object_weak_ref (G_OBJECT (copy), (GWeakNotify)context_weak_notify, box);
1489 static GtkSizeRequestMode
1490 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1492 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1493 GtkCellAreaBoxPrivate *priv = box->priv;
1495 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1496 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1497 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1501 compute_size (GtkCellAreaBox *box,
1502 GtkOrientation orientation,
1503 GtkCellAreaBoxContext *context,
1509 GtkCellAreaBoxPrivate *priv = box->priv;
1510 GtkCellArea *area = GTK_CELL_AREA (box);
1516 for (i = 0; i < priv->groups->len; i++)
1518 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1519 gint group_min_size = 0;
1520 gint group_nat_size = 0;
1522 for (list = group->cells; list; list = list->next)
1524 CellInfo *info = list->data;
1525 gint renderer_min_size, renderer_nat_size;
1527 if (!gtk_cell_renderer_get_visible (info->renderer))
1530 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1531 &renderer_min_size, &renderer_nat_size);
1533 if (orientation == priv->orientation)
1537 min_size += priv->spacing;
1538 nat_size += priv->spacing;
1541 if (group_min_size > 0)
1543 group_min_size += priv->spacing;
1544 group_nat_size += priv->spacing;
1547 min_size += renderer_min_size;
1548 nat_size += renderer_nat_size;
1549 group_min_size += renderer_min_size;
1550 group_nat_size += renderer_nat_size;
1554 min_size = MAX (min_size, renderer_min_size);
1555 nat_size = MAX (nat_size, renderer_nat_size);
1556 group_min_size = MAX (group_min_size, renderer_min_size);
1557 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1561 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1564 gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1566 gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1567 group_min_size, group_nat_size);
1572 gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1574 gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1575 group_min_size, group_nat_size);
1579 *minimum_size = min_size;
1580 *natural_size = nat_size;
1582 /* Update rtl state for focus navigation to work */
1583 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1584 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1587 static GtkRequestedSize *
1588 get_group_sizes (GtkCellArea *area,
1590 GtkOrientation orientation,
1594 GtkRequestedSize *sizes;
1598 *n_sizes = count_visible_cells (group, NULL);
1599 sizes = g_new (GtkRequestedSize, *n_sizes);
1601 for (l = group->cells, i = 0; l; l = l->next)
1603 CellInfo *info = l->data;
1605 if (!gtk_cell_renderer_get_visible (info->renderer))
1608 sizes[i].data = info;
1610 gtk_cell_area_request_renderer (area, info->renderer,
1611 orientation, widget, -1,
1612 &sizes[i].minimum_size,
1613 &sizes[i].natural_size);
1622 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1629 GtkCellAreaBoxPrivate *priv = box->priv;
1630 GtkCellArea *area = GTK_CELL_AREA (box);
1632 /* Exception for single cell groups */
1633 if (group->n_cells == 1)
1635 CellInfo *info = group->cells->data;
1637 gtk_cell_area_request_renderer (area, info->renderer,
1638 OPPOSITE_ORIENTATION (priv->orientation),
1639 widget, for_size, minimum_size, natural_size);
1643 GtkRequestedSize *orientation_sizes;
1646 gint avail_size = for_size;
1647 gint extra_size, extra_extra;
1648 gint min_size = 0, nat_size = 0;
1650 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1652 /* First naturally allocate the cells in the group into the for_size */
1653 avail_size -= (n_sizes - 1) * priv->spacing;
1654 for (i = 0; i < n_sizes; i++)
1655 avail_size -= orientation_sizes[i].minimum_size;
1658 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1662 /* Calculate/distribute expand for cells */
1663 if (group->expand_cells > 0)
1665 extra_size = avail_size / group->expand_cells;
1666 extra_extra = avail_size % group->expand_cells;
1669 extra_size = extra_extra = 0;
1671 for (i = 0; i < n_sizes; i++)
1673 gint cell_min, cell_nat;
1675 info = orientation_sizes[i].data;
1679 orientation_sizes[i].minimum_size += extra_size;
1682 orientation_sizes[i].minimum_size++;
1687 gtk_cell_area_request_renderer (area, info->renderer,
1688 OPPOSITE_ORIENTATION (priv->orientation),
1690 orientation_sizes[i].minimum_size,
1691 &cell_min, &cell_nat);
1693 min_size = MAX (min_size, cell_min);
1694 nat_size = MAX (nat_size, cell_nat);
1697 *minimum_size = min_size;
1698 *natural_size = nat_size;
1700 g_free (orientation_sizes);
1705 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1706 GtkCellAreaBoxContext *context,
1712 GtkCellAreaBoxPrivate *priv = box->priv;
1714 GtkRequestedSize *orientation_sizes;
1715 gint n_groups, n_expand_groups, i;
1716 gint avail_size = for_size;
1717 gint extra_size, extra_extra;
1718 gint min_size = 0, nat_size = 0;
1720 n_expand_groups = count_expand_groups (box);
1722 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1723 orientation_sizes = gtk_cell_area_box_context_get_widths (context, &n_groups);
1725 orientation_sizes = gtk_cell_area_box_context_get_heights (context, &n_groups);
1727 /* First start by naturally allocating space among groups of cells */
1728 avail_size -= (n_groups - 1) * priv->spacing;
1729 for (i = 0; i < n_groups; i++)
1730 avail_size -= orientation_sizes[i].minimum_size;
1733 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1737 /* Calculate/distribute expand for groups */
1738 if (n_expand_groups > 0)
1740 extra_size = avail_size / n_expand_groups;
1741 extra_extra = avail_size % n_expand_groups;
1744 extra_size = extra_extra = 0;
1746 /* Now we need to naturally allocate sizes for cells in each group
1747 * and push the height-for-width for each group accordingly while
1748 * accumulating the overall height-for-width for this row.
1750 for (i = 0; i < n_groups; i++)
1752 gint group_min, group_nat;
1753 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1755 group = &g_array_index (priv->groups, CellGroup, group_idx);
1757 if (group->expand_cells > 0)
1759 orientation_sizes[i].minimum_size += extra_size;
1762 orientation_sizes[i].minimum_size++;
1767 /* Now we have the allocation for the group,
1768 * request it's height-for-width
1770 compute_group_size_for_opposing_orientation (box, group, widget,
1771 orientation_sizes[i].minimum_size,
1772 &group_min, &group_nat);
1774 min_size = MAX (min_size, group_min);
1775 nat_size = MAX (nat_size, group_nat);
1777 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1779 gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1780 group_min, group_nat);
1784 gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1785 group_min, group_nat);
1789 *minimum_size = min_size;
1790 *natural_size = nat_size;
1792 g_free (orientation_sizes);
1794 /* Update rtl state for focus navigation to work */
1795 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1796 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1802 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1803 GtkCellAreaContext *context,
1805 gint *minimum_width,
1806 gint *natural_width)
1808 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1809 GtkCellAreaBoxContext *box_context;
1810 gint min_width, nat_width;
1812 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1814 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1816 /* Compute the size of all renderers for current row data,
1817 * bumping cell alignments in the context along the way
1819 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1820 box_context, widget, -1, &min_width, &nat_width);
1823 *minimum_width = min_width;
1826 *natural_width = nat_width;
1830 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1831 GtkCellAreaContext *context,
1833 gint *minimum_height,
1834 gint *natural_height)
1836 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1837 GtkCellAreaBoxContext *box_context;
1838 gint min_height, nat_height;
1840 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1842 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1844 /* Compute the size of all renderers for current row data,
1845 * bumping cell alignments in the context along the way
1847 compute_size (box, GTK_ORIENTATION_VERTICAL,
1848 box_context, widget, -1, &min_height, &nat_height);
1851 *minimum_height = min_height;
1854 *natural_height = nat_height;
1858 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1859 GtkCellAreaContext *context,
1862 gint *minimum_height,
1863 gint *natural_height)
1865 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1866 GtkCellAreaBoxContext *box_context;
1867 GtkCellAreaBoxPrivate *priv;
1868 gint min_height, nat_height;
1870 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1872 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1875 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1877 /* Add up vertical requests of height for width and push
1878 * the overall cached sizes for alignments
1880 compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1884 /* Juice: virtually allocate cells into the for_width using the
1885 * alignments and then return the overall height for that width,
1888 compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1892 *minimum_height = min_height;
1895 *natural_height = nat_height;
1899 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1900 GtkCellAreaContext *context,
1903 gint *minimum_width,
1904 gint *natural_width)
1906 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1907 GtkCellAreaBoxContext *box_context;
1908 GtkCellAreaBoxPrivate *priv;
1909 gint min_width, nat_width;
1911 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1913 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1916 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1918 /* Add up horizontal requests of width for height and push
1919 * the overall cached sizes for alignments
1921 compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1925 /* Juice: horizontally allocate cells into the for_height using the
1926 * alignments and then return the overall width for that height,
1929 compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1933 *minimum_width = min_width;
1936 *natural_width = nat_width;
1947 gtk_cell_area_box_focus (GtkCellArea *area,
1948 GtkDirectionType direction)
1950 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1951 GtkCellAreaBoxPrivate *priv = box->priv;
1952 gint cycle = FOCUS_NONE;
1953 gboolean cycled_focus = FALSE;
1954 GtkCellRenderer *focus_cell;
1956 focus_cell = gtk_cell_area_get_focus_cell (area);
1958 /* Special case, when there is no activatable cell, focus
1959 * is painted around the entire area... in this case we
1960 * let focus leave the area directly.
1962 if (focus_cell && !gtk_cell_area_is_activatable (area))
1964 gtk_cell_area_set_focus_cell (area, NULL);
1970 case GTK_DIR_TAB_FORWARD:
1971 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1973 case GTK_DIR_TAB_BACKWARD:
1974 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1977 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1979 else if (!focus_cell)
1980 cycle = FOCUS_LAST_CELL;
1983 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !priv->last_focus_cell)
1985 else if (!focus_cell)
1986 cycle = FOCUS_LAST_CELL;
1989 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1990 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1991 else if (!focus_cell)
1992 cycle = FOCUS_LAST_CELL;
1995 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !priv->last_focus_cell)
1996 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1997 else if (!focus_cell)
1998 cycle = FOCUS_LAST_CELL;
2004 if (cycle == FOCUS_LAST_CELL)
2006 gtk_cell_area_set_focus_cell (area, priv->last_focus_cell);
2007 cycled_focus = TRUE;
2009 else if (cycle != FOCUS_NONE)
2011 gboolean found_cell = FALSE;
2015 /* If there is no focused cell, focus on the first (or last) one */
2019 for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
2020 cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
2021 i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
2023 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
2025 for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
2026 cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
2028 CellInfo *info = list->data;
2030 if (info->renderer == focus_cell)
2032 else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
2033 gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
2035 gtk_cell_area_set_focus_cell (area, info->renderer);
2036 cycled_focus = TRUE;
2043 gtk_cell_area_set_focus_cell (area, NULL);
2045 return cycled_focus;
2049 /*************************************************************
2050 * GtkCellLayoutIface *
2051 *************************************************************/
2053 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
2055 iface->pack_start = gtk_cell_area_box_layout_pack_start;
2056 iface->pack_end = gtk_cell_area_box_layout_pack_end;
2057 iface->reorder = gtk_cell_area_box_layout_reorder;
2061 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
2062 GtkCellRenderer *renderer,
2065 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2069 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
2070 GtkCellRenderer *renderer,
2073 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, FALSE, TRUE);
2077 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
2078 GtkCellRenderer *renderer,
2081 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
2082 GtkCellAreaBoxPrivate *priv = box->priv;
2086 node = g_list_find_custom (priv->cells, renderer,
2087 (GCompareFunc)cell_info_find);
2093 priv->cells = g_list_delete_link (priv->cells, node);
2094 priv->cells = g_list_insert (priv->cells, info, position);
2096 cell_groups_rebuild (box);
2100 /*************************************************************
2101 * Private interaction with GtkCellAreaBoxContext *
2102 *************************************************************/
2104 _gtk_cell_area_box_group_visible (GtkCellAreaBox *box,
2107 GtkCellAreaBoxPrivate *priv = box->priv;
2110 g_assert (group_idx >= 0 && group_idx < priv->groups->len);
2112 group = &g_array_index (priv->groups, CellGroup, group_idx);
2114 return group->visible;
2118 /*************************************************************
2120 *************************************************************/
2122 * gtk_cell_area_box_new:
2124 * Creates a new #GtkCellAreaBox.
2126 * Return value: a newly created #GtkCellAreaBox
2131 gtk_cell_area_box_new (void)
2133 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
2137 * gtk_cell_area_box_pack_start:
2138 * @box: a #GtkCellAreaBox
2139 * @renderer: the #GtkCellRenderer to add
2140 * @expand: whether @renderer should receive extra space when the area receives
2141 * more than its natural size
2142 * @align: whether @renderer should be aligned in adjacent rows
2143 * @fixed: whether @renderer should have the same size in all rows
2145 * Adds @renderer to @box, packed with reference to the start of @box.
2147 * The @renderer is packed after any other #GtkCellRenderer packed
2148 * with reference to the start of @box.
2153 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
2154 GtkCellRenderer *renderer,
2159 GtkCellAreaBoxPrivate *priv;
2162 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2163 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2167 if (g_list_find_custom (priv->cells, renderer,
2168 (GCompareFunc)cell_info_find))
2170 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
2174 info = cell_info_new (renderer, GTK_PACK_START, expand, align, fixed);
2176 priv->cells = g_list_append (priv->cells, info);
2178 cell_groups_rebuild (box);
2182 * gtk_cell_area_box_pack_end:
2183 * @box: a #GtkCellAreaBox
2184 * @renderer: the #GtkCellRenderer to add
2185 * @expand: whether @renderer should receive extra space when the area receives
2186 * more than its natural size
2187 * @align: whether @renderer should be aligned in adjacent rows
2188 * @fixed: whether @renderer should have the same size in all rows
2190 * Adds @renderer to @box, packed with reference to the end of @box.
2192 * The @renderer is packed after (away from end of) any other
2193 * #GtkCellRenderer packed with reference to the end of @box.
2198 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
2199 GtkCellRenderer *renderer,
2204 GtkCellAreaBoxPrivate *priv;
2207 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2208 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2212 if (g_list_find_custom (priv->cells, renderer,
2213 (GCompareFunc)cell_info_find))
2215 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
2219 info = cell_info_new (renderer, GTK_PACK_END, expand, align, fixed);
2221 priv->cells = g_list_append (priv->cells, info);
2223 cell_groups_rebuild (box);
2227 * gtk_cell_area_box_get_spacing:
2228 * @box: a #GtkCellAreaBox
2230 * Gets the spacing added between cell renderers.
2232 * Return value: the space added between cell renderers in @box.
2237 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
2239 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2241 return box->priv->spacing;
2245 * gtk_cell_area_box_set_spacing:
2246 * @box: a #GtkCellAreaBox
2247 * @spacing: the space to add between #GtkCellRenderers
2249 * Sets the spacing to add between cell renderers in @box.
2254 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
2257 GtkCellAreaBoxPrivate *priv;
2259 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2263 if (priv->spacing != spacing)
2265 priv->spacing = spacing;
2267 g_object_notify (G_OBJECT (box), "spacing");
2269 /* Notify that size needs to be requested again */
2270 reset_contexts (box);