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 into a row or a column
28 * @Title: GtkCellAreaBox
30 * The #GtkCellAreaBox renders cell renderers into a row or a column depending on
31 * its #GtkOrientation.
33 * GtkCellAreaBox uses a notion of <emphasis>packing</emphasis>. Packing
34 * refers to adding cell renderers with reference to a particular position
35 * in a #GtkCellAreaBox. There are two reference positions: the
36 * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
37 * When the #GtkCellAreaBox is oriented in the %GTK_ORIENTATION_VERTICAL orientation,
38 * the start is defined as the top of the box and the end is defined as the bottom.
39 * In the %GTK_ORIENTATION_HORIZONTAL orientation start is defined as the
40 * left side and the end is defined as the right side.
42 * Alignments of #GtkCellRenderers rendered in adjacent rows can be configured
43 * by configuring the #GtkCellAreaBox:align child cell property with
44 * gtk_cell_area_cell_set_property() or by specifying the "align" argument
45 * 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 "gtkcellareaboxcontext.h"
54 #include "gtkprivate.h"
58 static void gtk_cell_area_box_finalize (GObject *object);
59 static void gtk_cell_area_box_dispose (GObject *object);
60 static void gtk_cell_area_box_set_property (GObject *object,
64 static void gtk_cell_area_box_get_property (GObject *object,
69 /* GtkCellAreaClass */
70 static void gtk_cell_area_box_add (GtkCellArea *area,
71 GtkCellRenderer *renderer);
72 static void gtk_cell_area_box_remove (GtkCellArea *area,
73 GtkCellRenderer *renderer);
74 static void gtk_cell_area_box_foreach (GtkCellArea *area,
75 GtkCellCallback callback,
76 gpointer callback_data);
77 static void gtk_cell_area_box_foreach_alloc (GtkCellArea *area,
78 GtkCellAreaContext *context,
80 const GdkRectangle *cell_area,
81 GtkCellAllocCallback callback,
82 gpointer callback_data);
83 static void gtk_cell_area_box_render (GtkCellArea *area,
84 GtkCellAreaContext *context,
87 const GdkRectangle *background_area,
88 const GdkRectangle *cell_area,
89 GtkCellRendererState flags,
90 gboolean paint_focus);
91 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
92 GtkCellRenderer *renderer,
96 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
97 GtkCellRenderer *renderer,
101 static GtkCellAreaContext *gtk_cell_area_box_create_context (GtkCellArea *area);
102 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
103 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
104 GtkCellAreaContext *context,
107 gint *natural_width);
108 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
109 GtkCellAreaContext *context,
111 gint *minimum_height,
112 gint *natural_height);
113 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
114 GtkCellAreaContext *context,
117 gint *minimum_height,
118 gint *natural_height);
119 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
120 GtkCellAreaContext *context,
124 gint *natural_width);
125 static gboolean gtk_cell_area_box_focus (GtkCellArea *area,
126 GtkDirectionType direction);
128 /* GtkCellLayoutIface */
129 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
130 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
131 GtkCellRenderer *renderer,
133 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
134 GtkCellRenderer *renderer,
136 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
137 GtkCellRenderer *renderer,
141 /* CellInfo/CellGroup metadata handling and convenience functions */
143 GtkCellRenderer *renderer;
145 guint expand : 1; /* Whether the cell expands */
146 guint pack : 1; /* Whether the cell is packed from the start or end */
147 guint align : 1; /* Whether to align this cell's position with adjacent rows */
155 guint expand_cells : 8;
159 GtkCellRenderer *renderer;
165 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
169 static void cell_info_free (CellInfo *info);
170 static gint cell_info_find (CellInfo *info,
171 GtkCellRenderer *renderer);
173 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
176 static void allocated_cell_free (AllocatedCell *cell);
177 static GList *list_consecutive_cells (GtkCellAreaBox *box);
178 static gint count_expand_groups (GtkCellAreaBox *box);
179 static void context_weak_notify (GtkCellAreaBox *box,
180 GtkCellAreaBoxContext *dead_context);
181 static void reset_contexts (GtkCellAreaBox *box);
182 static void init_context_groups (GtkCellAreaBox *box);
183 static void init_context_group (GtkCellAreaBox *box,
184 GtkCellAreaBoxContext *context);
185 static GSList *get_allocated_cells (GtkCellAreaBox *box,
186 GtkCellAreaBoxContext *context,
192 struct _GtkCellAreaBoxPrivate
194 GtkOrientation orientation;
203 /* We hold on to the rtl state from a widget we are requested for
204 * so that we can navigate focus correctly */
221 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
222 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
223 gtk_cell_area_box_cell_layout_init)
224 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
226 #define OPPOSITE_ORIENTATION(orientation) \
227 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
228 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
231 gtk_cell_area_box_init (GtkCellAreaBox *box)
233 GtkCellAreaBoxPrivate *priv;
235 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
236 GTK_TYPE_CELL_AREA_BOX,
237 GtkCellAreaBoxPrivate);
240 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
241 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
243 priv->contexts = NULL;
249 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
251 GObjectClass *object_class = G_OBJECT_CLASS (class);
252 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
255 object_class->finalize = gtk_cell_area_box_finalize;
256 object_class->dispose = gtk_cell_area_box_dispose;
257 object_class->set_property = gtk_cell_area_box_set_property;
258 object_class->get_property = gtk_cell_area_box_get_property;
260 /* GtkCellAreaClass */
261 area_class->add = gtk_cell_area_box_add;
262 area_class->remove = gtk_cell_area_box_remove;
263 area_class->foreach = gtk_cell_area_box_foreach;
264 area_class->foreach_alloc = gtk_cell_area_box_foreach_alloc;
265 area_class->render = gtk_cell_area_box_render;
266 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
267 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
269 area_class->create_context = gtk_cell_area_box_create_context;
270 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
271 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
272 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
273 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
274 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
276 area_class->focus = gtk_cell_area_box_focus;
279 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
282 * GtkCellAreaBox:spacing:
284 * The amount of space to reserve between cells.
288 g_object_class_install_property (object_class,
290 g_param_spec_int ("spacing",
292 P_("Space which is inserted between cells"),
296 GTK_PARAM_READWRITE));
298 /* Cell Properties */
300 * GtkCellAreaBox:expand:
302 * Whether the cell renderer should receive extra space when the area receives
303 * more than its natural size.
307 gtk_cell_area_class_install_cell_property (area_class,
312 P_("Whether the cell expands"),
314 GTK_PARAM_READWRITE));
317 * GtkCellAreaBox:align:
319 * Whether the cell renderer should be aligned in adjacent rows.
323 gtk_cell_area_class_install_cell_property (area_class,
328 P_("Whether cell should align with adjacent rows"),
330 GTK_PARAM_READWRITE));
333 * GtkCellAreaBox:pack-type:
335 * A GtkPackType indicating whether the cell renderer is packed with reference to the
336 * start or end of the area.
340 gtk_cell_area_class_install_cell_property (area_class,
345 P_("A GtkPackType indicating whether the cell is packed with "
346 "reference to the start or end of the cell area"),
347 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
348 GTK_PARAM_READWRITE));
350 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
354 /*************************************************************
355 * CellInfo/CellGroup basics and convenience functions *
356 *************************************************************/
358 cell_info_new (GtkCellRenderer *renderer,
363 CellInfo *info = g_slice_new (CellInfo);
365 info->renderer = g_object_ref_sink (renderer);
367 info->expand = expand;
374 cell_info_free (CellInfo *info)
376 g_object_unref (info->renderer);
378 g_slice_free (CellInfo, info);
382 cell_info_find (CellInfo *info,
383 GtkCellRenderer *renderer)
385 return (info->renderer == renderer) ? 0 : -1;
388 static AllocatedCell *
389 allocated_cell_new (GtkCellRenderer *renderer,
393 AllocatedCell *cell = g_slice_new (AllocatedCell);
395 cell->renderer = renderer;
396 cell->position = position;
403 allocated_cell_free (AllocatedCell *cell)
405 g_slice_free (AllocatedCell, cell);
409 list_consecutive_cells (GtkCellAreaBox *box)
411 GtkCellAreaBoxPrivate *priv = box->priv;
412 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
415 /* List cells in consecutive order taking their
416 * PACK_START/PACK_END options into account
418 for (l = priv->cells; l; l = l->next)
422 if (info->pack == GTK_PACK_START)
423 consecutive_cells = g_list_prepend (consecutive_cells, info);
426 for (l = priv->cells; l; l = l->next)
430 if (info->pack == GTK_PACK_END)
431 pack_end_cells = g_list_prepend (pack_end_cells, info);
434 consecutive_cells = g_list_reverse (consecutive_cells);
435 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
437 return consecutive_cells;
441 cell_groups_clear (GtkCellAreaBox *box)
443 GtkCellAreaBoxPrivate *priv = box->priv;
446 for (i = 0; i < priv->groups->len; i++)
448 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
450 g_list_free (group->cells);
453 g_array_set_size (priv->groups, 0);
457 cell_groups_rebuild (GtkCellAreaBox *box)
459 GtkCellAreaBoxPrivate *priv = box->priv;
460 CellGroup group = { 0, };
461 CellGroup *group_ptr;
465 cell_groups_clear (box);
470 cells = list_consecutive_cells (box);
472 /* First group is implied */
473 g_array_append_val (priv->groups, group);
474 group_ptr = &g_array_index (priv->groups, CellGroup, id);
476 for (l = cells; l; l = l->next)
478 CellInfo *info = l->data;
480 /* A new group starts with any aligned cell, the first group is implied */
481 if (info->align && l != cells)
483 memset (&group, 0x0, sizeof (CellGroup));
486 g_array_append_val (priv->groups, group);
487 group_ptr = &g_array_index (priv->groups, CellGroup, id);
490 group_ptr->cells = g_list_prepend (group_ptr->cells, info);
491 group_ptr->n_cells++;
493 /* A group expands if it contains any expand cells */
495 group_ptr->expand_cells++;
500 for (id = 0; id < priv->groups->len; id++)
502 group_ptr = &g_array_index (priv->groups, CellGroup, id);
504 group_ptr->cells = g_list_reverse (group_ptr->cells);
507 /* Contexts need to be updated with the new grouping information */
508 init_context_groups (box);
512 count_visible_cells (CellGroup *group,
516 gint visible_cells = 0;
517 gint n_expand_cells = 0;
519 for (l = group->cells; l; l = l->next)
521 CellInfo *info = l->data;
523 if (gtk_cell_renderer_get_visible (info->renderer))
533 *expand_cells = n_expand_cells;
535 return visible_cells;
539 count_expand_groups (GtkCellAreaBox *box)
541 GtkCellAreaBoxPrivate *priv = box->priv;
543 gint expand_groups = 0;
545 for (i = 0; i < priv->groups->len; i++)
547 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
549 if (group->expand_cells > 0)
553 return expand_groups;
557 context_weak_notify (GtkCellAreaBox *box,
558 GtkCellAreaBoxContext *dead_context)
560 GtkCellAreaBoxPrivate *priv = box->priv;
562 priv->contexts = g_slist_remove (priv->contexts, dead_context);
566 init_context_group (GtkCellAreaBox *box,
567 GtkCellAreaBoxContext *context)
569 GtkCellAreaBoxPrivate *priv = box->priv;
570 gint *expand_groups, i;
572 expand_groups = g_new (gboolean, priv->groups->len);
574 for (i = 0; i < priv->groups->len; i++)
576 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
578 expand_groups[i] = (group->expand_cells > 0);
581 /* This call implies reseting the request info */
582 gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups);
583 g_free (expand_groups);
587 init_context_groups (GtkCellAreaBox *box)
589 GtkCellAreaBoxPrivate *priv = box->priv;
592 /* When the box's groups are reconstructed, contexts need to
595 for (l = priv->contexts; l; l = l->next)
597 GtkCellAreaBoxContext *context = l->data;
599 init_context_group (box, context);
604 reset_contexts (GtkCellAreaBox *box)
606 GtkCellAreaBoxPrivate *priv = box->priv;
609 /* When the box layout changes, contexts need to
610 * be reset and sizes for the box get requested again
612 for (l = priv->contexts; l; l = l->next)
614 GtkCellAreaContext *context = l->data;
616 gtk_cell_area_context_reset (context);
620 /* Fall back on a completely unaligned dynamic allocation of cells
621 * when not allocated for the said orientation, alignment of cells
622 * is not done when each area gets a different size in the orientation
626 allocate_cells_manually (GtkCellAreaBox *box,
631 GtkCellAreaBoxPrivate *priv = box->priv;
633 GSList *allocated_cells = NULL;
634 GtkRequestedSize *sizes;
636 gint nvisible = 0, nexpand = 0, group_expand;
637 gint avail_size, extra_size, extra_extra, full_size;
638 gint position = 0, for_size;
644 /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
645 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
646 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
648 cells = list_consecutive_cells (box);
650 /* Count the visible and expand cells */
651 for (i = 0; i < priv->groups->len; i++)
653 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
655 nvisible += count_visible_cells (group, &group_expand);
656 nexpand += group_expand;
665 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
667 full_size = avail_size = width;
672 full_size = avail_size = height;
676 /* Go ahead and collect the requests on the fly */
677 sizes = g_new0 (GtkRequestedSize, nvisible);
678 for (l = cells, i = 0; l; l = l->next)
680 CellInfo *info = l->data;
682 if (!gtk_cell_renderer_get_visible (info->renderer))
685 gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
688 &sizes[i].minimum_size,
689 &sizes[i].natural_size);
691 avail_size -= sizes[i].minimum_size;
693 sizes[i].data = info;
698 /* Naturally distribute the allocation */
699 avail_size -= (nvisible - 1) * priv->spacing;
701 avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
705 /* Calculate/distribute expand for cells */
708 extra_size = avail_size / nexpand;
709 extra_extra = avail_size % nexpand;
712 extra_size = extra_extra = 0;
714 /* Create the allocated cells */
715 for (i = 0; i < nvisible; i++)
717 CellInfo *info = sizes[i].data;
722 sizes[i].minimum_size += extra_size;
725 sizes[i].minimum_size++;
731 cell = allocated_cell_new (info->renderer,
732 full_size - (position + sizes[i].minimum_size),
733 sizes[i].minimum_size);
735 cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
737 allocated_cells = g_slist_prepend (allocated_cells, cell);
739 position += sizes[i].minimum_size;
740 position += priv->spacing;
746 /* Note it might not be important to reverse the list here at all,
747 * we have the correct positions, no need to allocate from left to right */
748 return g_slist_reverse (allocated_cells);
751 /* Returns an allocation for each cell in the orientation of the box,
752 * used in ->render()/->event() implementations to get a straight-forward
753 * list of allocated cells to operate on.
756 get_allocated_cells (GtkCellAreaBox *box,
757 GtkCellAreaBoxContext *context,
762 GtkCellAreaBoxAllocation *group_allocs;
763 GtkCellArea *area = GTK_CELL_AREA (box);
764 GtkCellAreaBoxPrivate *priv = box->priv;
766 GSList *allocated_cells = NULL;
768 gint for_size, full_size;
771 group_allocs = gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
773 return allocate_cells_manually (box, widget, width, height);
775 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
786 /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
787 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
788 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
790 for (i = 0; i < n_allocs; i++)
792 /* We dont always allocate all groups, sometimes the requested group has only invisible
793 * cells for every row, hence the usage of group_allocs[i].group_idx here
795 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
797 /* Exception for single cell groups */
798 if (group->n_cells == 1)
800 CellInfo *info = group->cells->data;
804 cell = allocated_cell_new (info->renderer,
805 full_size - (group_allocs[i].position + group_allocs[i].size),
806 group_allocs[i].size);
808 cell = allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
810 allocated_cells = g_slist_prepend (allocated_cells, cell);
814 GtkRequestedSize *sizes;
815 gint avail_size, position;
816 gint visible_cells, expand_cells;
817 gint extra_size, extra_extra;
819 visible_cells = count_visible_cells (group, &expand_cells);
821 /* If this row has no visible cells in this group, just
822 * skip the allocation */
823 if (visible_cells == 0)
826 /* Offset the allocation to the group position and allocate into
827 * the group's available size */
828 position = group_allocs[i].position;
829 avail_size = group_allocs[i].size;
831 sizes = g_new (GtkRequestedSize, visible_cells);
833 for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
835 CellInfo *info = cell_list->data;
837 if (!gtk_cell_renderer_get_visible (info->renderer))
840 gtk_cell_area_request_renderer (area, info->renderer,
843 &sizes[j].minimum_size,
844 &sizes[j].natural_size);
846 sizes[j].data = info;
847 avail_size -= sizes[j].minimum_size;
852 /* Distribute cells naturally within the group */
853 avail_size -= (visible_cells - 1) * priv->spacing;
855 avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
859 /* Calculate/distribute expand for cells */
860 if (expand_cells > 0)
862 extra_size = avail_size / expand_cells;
863 extra_extra = avail_size % expand_cells;
866 extra_size = extra_extra = 0;
868 /* Create the allocated cells (loop only over visible cells here) */
869 for (j = 0; j < visible_cells; j++)
871 CellInfo *info = sizes[j].data;
876 sizes[j].minimum_size += extra_size;
879 sizes[j].minimum_size++;
885 cell = allocated_cell_new (info->renderer,
886 full_size - (position + sizes[j].minimum_size),
887 sizes[j].minimum_size);
889 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
891 allocated_cells = g_slist_prepend (allocated_cells, cell);
893 position += sizes[j].minimum_size;
894 position += priv->spacing;
901 /* Note it might not be important to reverse the list here at all,
902 * we have the correct positions, no need to allocate from left to right */
903 return g_slist_reverse (allocated_cells);
906 /*************************************************************
908 *************************************************************/
910 gtk_cell_area_box_finalize (GObject *object)
912 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
913 GtkCellAreaBoxPrivate *priv = box->priv;
916 /* Unref/free the context list */
917 for (l = priv->contexts; l; l = l->next)
918 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
920 g_slist_free (priv->contexts);
921 priv->contexts = NULL;
923 /* Free the cell grouping info */
924 cell_groups_clear (box);
925 g_array_free (priv->groups, TRUE);
927 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
931 gtk_cell_area_box_dispose (GObject *object)
933 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
937 gtk_cell_area_box_set_property (GObject *object,
942 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
946 case PROP_ORIENTATION:
947 box->priv->orientation = g_value_get_enum (value);
949 /* Notify that size needs to be requested again */
950 reset_contexts (box);
953 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
956 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
962 gtk_cell_area_box_get_property (GObject *object,
967 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
971 case PROP_ORIENTATION:
972 g_value_set_enum (value, box->priv->orientation);
975 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
978 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
983 /*************************************************************
985 *************************************************************/
987 gtk_cell_area_box_add (GtkCellArea *area,
988 GtkCellRenderer *renderer)
990 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
991 renderer, FALSE, TRUE);
995 gtk_cell_area_box_remove (GtkCellArea *area,
996 GtkCellRenderer *renderer)
998 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
999 GtkCellAreaBoxPrivate *priv = box->priv;
1002 node = g_list_find_custom (priv->cells, renderer,
1003 (GCompareFunc)cell_info_find);
1007 CellInfo *info = node->data;
1009 cell_info_free (info);
1011 priv->cells = g_list_delete_link (priv->cells, node);
1013 /* Reconstruct cell groups */
1014 cell_groups_rebuild (box);
1017 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1021 gtk_cell_area_box_foreach (GtkCellArea *area,
1022 GtkCellCallback callback,
1023 gpointer callback_data)
1025 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1026 GtkCellAreaBoxPrivate *priv = box->priv;
1029 for (list = priv->cells; list; list = list->next)
1031 CellInfo *info = list->data;
1033 if (callback (info->renderer, callback_data))
1039 gtk_cell_area_box_foreach_alloc (GtkCellArea *area,
1040 GtkCellAreaContext *context,
1042 const GdkRectangle *cell_area,
1043 GtkCellAllocCallback callback,
1044 gpointer callback_data)
1046 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1047 GtkCellAreaBoxPrivate *priv = box->priv;
1048 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1049 GSList *allocated_cells, *l;
1050 GdkRectangle allocation;
1052 allocation = *cell_area;
1054 /* Get a list of cells with allocation sizes decided regardless
1055 * of alignments and pack order etc. */
1056 allocated_cells = get_allocated_cells (box, box_context, widget,
1057 cell_area->width, cell_area->height);
1059 for (l = allocated_cells; l; l = l->next)
1061 AllocatedCell *cell = l->data;
1063 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1065 allocation.x = cell_area->x + cell->position;
1066 allocation.width = cell->size;
1070 allocation.y = cell_area->y + cell->position;
1071 allocation.height = cell->size;
1074 if (callback (cell->renderer, &allocation, callback_data))
1078 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1079 g_slist_free (allocated_cells);
1083 gtk_cell_area_box_render (GtkCellArea *area,
1084 GtkCellAreaContext *context,
1087 const GdkRectangle *background_area,
1088 const GdkRectangle *cell_area,
1089 GtkCellRendererState flags,
1090 gboolean paint_focus)
1092 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1093 GtkCellAreaBoxPrivate *priv = box->priv;
1094 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1095 GSList *allocated_cells, *l;
1096 GdkRectangle cell_background, inner_area;
1097 GtkCellRenderer *focus_cell = NULL;
1098 GdkRectangle focus_rect = { 0, };
1099 gboolean first_focus_cell = TRUE;
1100 gboolean focus_all = FALSE;
1103 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1104 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1106 /* Make sure we dont paint a focus rectangle while there
1107 * is an editable widget in play
1109 if (gtk_cell_area_get_edited_cell (area))
1110 paint_focus = FALSE;
1112 if (flags & GTK_CELL_RENDERER_FOCUSED)
1114 focus_cell = gtk_cell_area_get_focus_cell (area);
1115 flags &= ~GTK_CELL_RENDERER_FOCUSED;
1117 /* If no cell can activate but the caller wants focus painted,
1118 * then we paint focus around all cells */
1119 if (paint_focus && !gtk_cell_area_is_activatable (area))
1123 cell_background = *cell_area;
1125 /* Get a list of cells with allocation sizes decided regardless
1126 * of alignments and pack order etc. */
1127 allocated_cells = get_allocated_cells (box, box_context, widget,
1128 cell_area->width, cell_area->height);
1130 for (l = allocated_cells; l; l = l->next)
1132 AllocatedCell *cell = l->data;
1133 GtkCellRendererState cell_fields = 0;
1134 GdkRectangle render_background;
1136 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1138 cell_background.x = cell_area->x + cell->position;
1139 cell_background.width = cell->size;
1143 cell_background.y = cell_area->y + cell->position;
1144 cell_background.height = cell->size;
1147 /* Stop rendering cells if they flow out of the render area,
1148 * this can happen because the render area can actually be
1149 * smaller than the requested area (treeview columns can
1150 * be user resizable and can be resized to be smaller than
1151 * the actual requested area). */
1152 if (cell_background.x > cell_area->x + cell_area->width ||
1153 cell_background.x + cell_background.width < cell_area->x ||
1154 cell_background.y > cell_area->y + cell_area->height)
1157 /* Special case for the last cell (or first cell in rtl)... let the last cell consume
1158 * the remaining space in the area (the last cell is allowed to consume the remaining
1159 * space if the space given for rendering is actually larger than allocation, this can
1160 * happen in the expander GtkTreeViewColumn where only the deepest depth column
1161 * receives the allocation... shallow columns recieve more width). */
1166 /* Fill the leading space for the first cell in the area (still last in the list) */
1167 cell_background.width = (cell_background.x - cell_area->x) + cell_background.width;
1168 cell_background.x = cell_area->x;
1172 cell_background.width = cell_area->x + cell_area->width - cell_background.x;
1173 cell_background.height = cell_area->y + cell_area->height - cell_background.y;
1178 /* If the cell we are rendering doesnt fit into the remaining space, clip it
1179 * so that the underlying renderer has a chance to deal with it (for instance
1180 * text renderers get a chance to ellipsize).
1182 if (cell_background.x + cell_background.width > cell_area->x + cell_area->width)
1183 cell_background.width = cell_area->x + cell_area->width - cell_background.x;
1185 if (cell_background.y + cell_background.height > cell_area->y + cell_area->height)
1186 cell_background.height = cell_area->y + cell_area->height - cell_background.y;
1189 /* Remove margins from the background area to produce the cell area
1191 gtk_cell_area_inner_cell_area (area, widget, &cell_background, &inner_area);
1193 /* Add portions of the background_area to the cell_background
1194 * to create the render_background */
1195 render_background = cell_background;
1197 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1199 if (l == allocated_cells)
1201 render_background.width += render_background.x - background_area->x;
1202 render_background.x = background_area->x;
1205 if (l->next == NULL)
1206 render_background.width =
1207 background_area->width - (render_background.x - background_area->x);
1209 render_background.y = background_area->y;
1210 render_background.height = background_area->height;
1214 if (l == allocated_cells)
1216 render_background.height += render_background.y - background_area->y;
1217 render_background.y = background_area->y;
1220 if (l->next == NULL)
1221 render_background.height =
1222 background_area->height - (render_background.y - background_area->y);
1224 render_background.x = background_area->x;
1225 render_background.width = background_area->width;
1230 (cell->renderer == focus_cell ||
1231 gtk_cell_area_is_focus_sibling (area, focus_cell, cell->renderer))))
1233 cell_fields |= GTK_CELL_RENDERER_FOCUSED;
1237 GdkRectangle cell_focus;
1239 gtk_cell_renderer_get_aligned_area (cell->renderer, widget, flags, &inner_area, &cell_focus);
1241 /* Accumulate the focus rectangle for all focus siblings */
1242 if (first_focus_cell)
1244 focus_rect = cell_focus;
1245 first_focus_cell = FALSE;
1248 gdk_rectangle_union (&focus_rect, &cell_focus, &focus_rect);
1252 /* We have to do some per-cell considerations for the 'flags'
1253 * for focus handling */
1254 gtk_cell_renderer_render (cell->renderer, cr, widget,
1255 &render_background, &inner_area,
1256 flags | cell_fields);
1259 if (paint_focus && focus_rect.width != 0 && focus_rect.height != 0)
1261 GtkStateType renderer_state =
1262 flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
1263 (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
1264 (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL));
1266 gtk_paint_focus (gtk_widget_get_style (widget), cr,
1267 renderer_state, widget,
1268 gtk_cell_area_get_style_detail (area),
1269 focus_rect.x, focus_rect.y,
1270 focus_rect.width, focus_rect.height);
1274 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1275 g_slist_free (allocated_cells);
1279 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
1280 GtkCellRenderer *renderer,
1282 const GValue *value,
1285 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1286 GtkCellAreaBoxPrivate *priv = box->priv;
1289 gboolean rebuild = FALSE;
1291 GtkPackType pack_type;
1293 node = g_list_find_custom (priv->cells, renderer,
1294 (GCompareFunc)cell_info_find);
1302 case CELL_PROP_EXPAND:
1303 val = g_value_get_boolean (value);
1305 if (info->expand != val)
1312 case CELL_PROP_ALIGN:
1313 val = g_value_get_boolean (value);
1315 if (info->align != val)
1322 case CELL_PROP_PACK_TYPE:
1323 pack_type = g_value_get_enum (value);
1325 if (info->pack != pack_type)
1327 info->pack = pack_type;
1332 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1336 /* Groups need to be rebuilt */
1338 cell_groups_rebuild (box);
1342 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
1343 GtkCellRenderer *renderer,
1348 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1349 GtkCellAreaBoxPrivate *priv = box->priv;
1353 node = g_list_find_custom (priv->cells, renderer,
1354 (GCompareFunc)cell_info_find);
1362 case CELL_PROP_EXPAND:
1363 g_value_set_boolean (value, info->expand);
1366 case CELL_PROP_ALIGN:
1367 g_value_set_boolean (value, info->align);
1370 case CELL_PROP_PACK_TYPE:
1371 g_value_set_enum (value, info->pack);
1374 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1380 static GtkCellAreaContext *
1381 gtk_cell_area_box_create_context (GtkCellArea *area)
1383 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1384 GtkCellAreaBoxPrivate *priv = box->priv;
1385 GtkCellAreaContext *context =
1386 (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
1387 "area", area, NULL);
1389 priv->contexts = g_slist_prepend (priv->contexts, context);
1391 g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1393 /* Tell the new group about our cell layout */
1394 init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1399 static GtkSizeRequestMode
1400 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1402 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1403 GtkCellAreaBoxPrivate *priv = box->priv;
1405 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1406 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1407 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1411 compute_size (GtkCellAreaBox *box,
1412 GtkOrientation orientation,
1413 GtkCellAreaBoxContext *context,
1419 GtkCellAreaBoxPrivate *priv = box->priv;
1420 GtkCellArea *area = GTK_CELL_AREA (box);
1426 for (i = 0; i < priv->groups->len; i++)
1428 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1429 gint group_min_size = 0;
1430 gint group_nat_size = 0;
1432 for (list = group->cells; list; list = list->next)
1434 CellInfo *info = list->data;
1435 gint renderer_min_size, renderer_nat_size;
1437 if (!gtk_cell_renderer_get_visible (info->renderer))
1440 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1441 &renderer_min_size, &renderer_nat_size);
1443 if (orientation == priv->orientation)
1447 min_size += priv->spacing;
1448 nat_size += priv->spacing;
1451 if (group_min_size > 0)
1453 group_min_size += priv->spacing;
1454 group_nat_size += priv->spacing;
1457 min_size += renderer_min_size;
1458 nat_size += renderer_nat_size;
1459 group_min_size += renderer_min_size;
1460 group_nat_size += renderer_nat_size;
1464 min_size = MAX (min_size, renderer_min_size);
1465 nat_size = MAX (nat_size, renderer_nat_size);
1466 group_min_size = MAX (group_min_size, renderer_min_size);
1467 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1471 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1474 gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1476 gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1477 group_min_size, group_nat_size);
1482 gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1484 gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1485 group_min_size, group_nat_size);
1489 *minimum_size = min_size;
1490 *natural_size = nat_size;
1492 /* Update rtl state for focus navigation to work */
1493 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1494 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1498 get_group_sizes (GtkCellArea *area,
1500 GtkOrientation orientation,
1504 GtkRequestedSize *sizes;
1508 *n_sizes = count_visible_cells (group, NULL);
1509 sizes = g_new (GtkRequestedSize, *n_sizes);
1511 for (l = group->cells, i = 0; l; l = l->next)
1513 CellInfo *info = l->data;
1515 if (!gtk_cell_renderer_get_visible (info->renderer))
1518 sizes[i].data = info;
1520 gtk_cell_area_request_renderer (area, info->renderer,
1521 orientation, widget, -1,
1522 &sizes[i].minimum_size,
1523 &sizes[i].natural_size);
1532 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1539 GtkCellAreaBoxPrivate *priv = box->priv;
1540 GtkCellArea *area = GTK_CELL_AREA (box);
1542 /* Exception for single cell groups */
1543 if (group->n_cells == 1)
1545 CellInfo *info = group->cells->data;
1547 gtk_cell_area_request_renderer (area, info->renderer,
1548 OPPOSITE_ORIENTATION (priv->orientation),
1549 widget, for_size, minimum_size, natural_size);
1553 GtkRequestedSize *orientation_sizes;
1556 gint avail_size = for_size;
1557 gint extra_size, extra_extra;
1558 gint min_size = 0, nat_size = 0;
1560 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1562 /* First naturally allocate the cells in the group into the for_size */
1563 avail_size -= (n_sizes - 1) * priv->spacing;
1564 for (i = 0; i < n_sizes; i++)
1565 avail_size -= orientation_sizes[i].minimum_size;
1568 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1572 /* Calculate/distribute expand for cells */
1573 if (group->expand_cells > 0)
1575 extra_size = avail_size / group->expand_cells;
1576 extra_extra = avail_size % group->expand_cells;
1579 extra_size = extra_extra = 0;
1581 for (i = 0; i < n_sizes; i++)
1583 gint cell_min, cell_nat;
1585 info = orientation_sizes[i].data;
1589 orientation_sizes[i].minimum_size += extra_size;
1592 orientation_sizes[i].minimum_size++;
1597 gtk_cell_area_request_renderer (area, info->renderer,
1598 OPPOSITE_ORIENTATION (priv->orientation),
1600 orientation_sizes[i].minimum_size,
1601 &cell_min, &cell_nat);
1603 min_size = MAX (min_size, cell_min);
1604 nat_size = MAX (nat_size, cell_nat);
1607 *minimum_size = min_size;
1608 *natural_size = nat_size;
1610 g_free (orientation_sizes);
1615 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1616 GtkCellAreaBoxContext *context,
1622 GtkCellAreaBoxPrivate *priv = box->priv;
1624 GtkRequestedSize *orientation_sizes;
1625 gint n_groups, n_expand_groups, i;
1626 gint avail_size = for_size;
1627 gint extra_size, extra_extra;
1628 gint min_size = 0, nat_size = 0;
1630 n_expand_groups = count_expand_groups (box);
1632 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1633 orientation_sizes = gtk_cell_area_box_context_get_widths (context, &n_groups);
1635 orientation_sizes = gtk_cell_area_box_context_get_heights (context, &n_groups);
1637 /* First start by naturally allocating space among groups of cells */
1638 avail_size -= (n_groups - 1) * priv->spacing;
1639 for (i = 0; i < n_groups; i++)
1640 avail_size -= orientation_sizes[i].minimum_size;
1643 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1647 /* Calculate/distribute expand for groups */
1648 if (n_expand_groups > 0)
1650 extra_size = avail_size / n_expand_groups;
1651 extra_extra = avail_size % n_expand_groups;
1654 extra_size = extra_extra = 0;
1656 /* Now we need to naturally allocate sizes for cells in each group
1657 * and push the height-for-width for each group accordingly while accumulating
1658 * the overall height-for-width for this row.
1660 for (i = 0; i < n_groups; i++)
1662 gint group_min, group_nat;
1663 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1665 group = &g_array_index (priv->groups, CellGroup, group_idx);
1667 if (group->expand_cells > 0)
1669 orientation_sizes[i].minimum_size += extra_size;
1672 orientation_sizes[i].minimum_size++;
1677 /* Now we have the allocation for the group, request it's height-for-width */
1678 compute_group_size_for_opposing_orientation (box, group, widget,
1679 orientation_sizes[i].minimum_size,
1680 &group_min, &group_nat);
1682 min_size = MAX (min_size, group_min);
1683 nat_size = MAX (nat_size, group_nat);
1685 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1687 gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1688 group_min, group_nat);
1692 gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1693 group_min, group_nat);
1697 *minimum_size = min_size;
1698 *natural_size = nat_size;
1700 g_free (orientation_sizes);
1702 /* Update rtl state for focus navigation to work */
1703 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1704 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1710 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1711 GtkCellAreaContext *context,
1713 gint *minimum_width,
1714 gint *natural_width)
1716 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1717 GtkCellAreaBoxContext *box_context;
1718 gint min_width, nat_width;
1720 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1722 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1724 /* Compute the size of all renderers for current row data,
1725 * bumping cell alignments in the context along the way */
1726 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1727 box_context, widget, -1, &min_width, &nat_width);
1730 *minimum_width = min_width;
1733 *natural_width = nat_width;
1737 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1738 GtkCellAreaContext *context,
1740 gint *minimum_height,
1741 gint *natural_height)
1743 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1744 GtkCellAreaBoxContext *box_context;
1745 gint min_height, nat_height;
1747 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1749 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1751 /* Compute the size of all renderers for current row data,
1752 * bumping cell alignments in the context along the way */
1753 compute_size (box, GTK_ORIENTATION_VERTICAL,
1754 box_context, widget, -1, &min_height, &nat_height);
1757 *minimum_height = min_height;
1760 *natural_height = nat_height;
1764 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1765 GtkCellAreaContext *context,
1768 gint *minimum_height,
1769 gint *natural_height)
1771 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1772 GtkCellAreaBoxContext *box_context;
1773 GtkCellAreaBoxPrivate *priv;
1774 gint min_height, nat_height;
1776 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1778 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1781 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1783 /* Add up vertical requests of height for width and push the overall
1784 * cached sizes for alignments */
1785 compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1789 /* Juice: virtually allocate cells into the for_width using the
1790 * alignments and then return the overall height for that width, and cache it */
1791 compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1795 *minimum_height = min_height;
1798 *natural_height = nat_height;
1802 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1803 GtkCellAreaContext *context,
1806 gint *minimum_width,
1807 gint *natural_width)
1809 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1810 GtkCellAreaBoxContext *box_context;
1811 GtkCellAreaBoxPrivate *priv;
1812 gint min_width, nat_width;
1814 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1816 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1819 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1821 /* Add up horizontal requests of width for height and push the overall
1822 * cached sizes for alignments */
1823 compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1827 /* Juice: horizontally allocate cells into the for_height using the
1828 * alignments and then return the overall width for that height, and cache it */
1829 compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1833 *minimum_width = min_width;
1836 *natural_width = nat_width;
1846 gtk_cell_area_box_focus (GtkCellArea *area,
1847 GtkDirectionType direction)
1849 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1850 GtkCellAreaBoxPrivate *priv = box->priv;
1851 gint cycle = FOCUS_NONE;
1852 gboolean cycled_focus = FALSE;
1853 GtkCellRenderer *focus_cell;
1855 focus_cell = gtk_cell_area_get_focus_cell (area);
1857 /* Special case, when there is no activatable cell, focus
1858 * is painted around the entire area... in this case we
1859 * let focus leave the area directly.
1861 if (focus_cell && !gtk_cell_area_is_activatable (area))
1863 gtk_cell_area_set_focus_cell (area, NULL);
1869 case GTK_DIR_TAB_FORWARD:
1870 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1872 case GTK_DIR_TAB_BACKWARD:
1873 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1876 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
1880 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
1884 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
1885 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1888 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
1889 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1895 if (cycle != FOCUS_NONE)
1897 gboolean found_cell = FALSE;
1901 /* If there is no focused cell, focus on the first (or last) one in the list */
1905 for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
1906 cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
1907 i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
1909 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1911 for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
1912 cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
1914 CellInfo *info = list->data;
1916 if (info->renderer == focus_cell)
1918 else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
1919 gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
1921 gtk_cell_area_set_focus_cell (area, info->renderer);
1922 cycled_focus = TRUE;
1929 gtk_cell_area_set_focus_cell (area, NULL);
1931 return cycled_focus;
1935 /*************************************************************
1936 * GtkCellLayoutIface *
1937 *************************************************************/
1939 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1941 iface->pack_start = gtk_cell_area_box_layout_pack_start;
1942 iface->pack_end = gtk_cell_area_box_layout_pack_end;
1943 iface->reorder = gtk_cell_area_box_layout_reorder;
1947 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
1948 GtkCellRenderer *renderer,
1951 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1955 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
1956 GtkCellRenderer *renderer,
1959 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1963 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
1964 GtkCellRenderer *renderer,
1967 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
1968 GtkCellAreaBoxPrivate *priv = box->priv;
1972 node = g_list_find_custom (priv->cells, renderer,
1973 (GCompareFunc)cell_info_find);
1979 priv->cells = g_list_delete_link (priv->cells, node);
1980 priv->cells = g_list_insert (priv->cells, info, position);
1982 cell_groups_rebuild (box);
1986 /*************************************************************
1988 *************************************************************/
1990 * gtk_cell_area_box_new:
1992 * Creates a new #GtkCellAreaBox.
1994 * Return value: a newly created #GtkCellAreaBox
1997 gtk_cell_area_box_new (void)
1999 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
2003 * gtk_cell_area_box_pack_start:
2004 * @box: a #GtkCellAreaBox
2005 * @renderer: the #GtkCellRenderer to add
2006 * @expand: whether @renderer should receive extra space when the area receives
2007 * more than its natural size
2008 * @align: whether @renderer should be aligned in adjacent rows.
2010 * Adds @renderer to @box, packed with reference to the start of @box.
2012 * The @renderer is packed after any other #GtkCellRenderer packed with reference
2013 * to the start of @box.
2018 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
2019 GtkCellRenderer *renderer,
2023 GtkCellAreaBoxPrivate *priv;
2026 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2027 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2031 if (g_list_find_custom (priv->cells, renderer,
2032 (GCompareFunc)cell_info_find))
2034 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
2038 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
2040 priv->cells = g_list_append (priv->cells, info);
2042 cell_groups_rebuild (box);
2046 * gtk_cell_area_box_pack_end:
2047 * @box: a #GtkCellAreaBox
2048 * @renderer: the #GtkCellRenderer to add
2049 * @expand: whether @renderer should receive extra space when the area receives
2050 * more than its natural size
2051 * @align: whether @renderer should be aligned in adjacent rows.
2053 * Adds @renderer to @box, packed with reference to the end of @box.
2055 * The @renderer is packed after (away from end of) any other #GtkCellRenderer
2056 * packed with reference to the end of @box.
2061 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
2062 GtkCellRenderer *renderer,
2066 GtkCellAreaBoxPrivate *priv;
2069 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2070 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2074 if (g_list_find_custom (priv->cells, renderer,
2075 (GCompareFunc)cell_info_find))
2077 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
2081 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
2083 priv->cells = g_list_append (priv->cells, info);
2085 cell_groups_rebuild (box);
2089 * gtk_cell_area_box_get_spacing:
2090 * @box: a #GtkCellAreaBox
2092 * Gets the spacing added between cell renderers.
2094 * Return value: the space added between cell renderers in @box.
2099 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
2101 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2103 return box->priv->spacing;
2107 * gtk_cell_area_box_set_spacing:
2108 * @box: a #GtkCellAreaBox
2109 * @spacing: the space to add between #GtkCellRenderers
2111 * Sets the spacing to add between cell renderers in @box.
2116 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
2119 GtkCellAreaBoxPrivate *priv;
2121 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2125 if (priv->spacing != spacing)
2127 priv->spacing = spacing;
2129 g_object_notify (G_OBJECT (box), "spacing");
2131 /* Notify that size needs to be requested again */
2132 reset_contexts (box);