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_forall (GtkCellArea *area,
75 GtkCellCallback callback,
76 gpointer callback_data);
77 static void gtk_cell_area_box_get_cell_allocation (GtkCellArea *area,
78 GtkCellAreaContext *context,
80 GtkCellRenderer *renderer,
81 const GdkRectangle *cell_area,
82 GdkRectangle *allocation);
83 static gint gtk_cell_area_box_event (GtkCellArea *area,
84 GtkCellAreaContext *context,
87 const GdkRectangle *cell_area,
88 GtkCellRendererState flags);
89 static void gtk_cell_area_box_render (GtkCellArea *area,
90 GtkCellAreaContext *context,
93 const GdkRectangle *background_area,
94 const GdkRectangle *cell_area,
95 GtkCellRendererState flags,
96 gboolean paint_focus);
97 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
98 GtkCellRenderer *renderer,
102 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
103 GtkCellRenderer *renderer,
107 static GtkCellAreaContext *gtk_cell_area_box_create_context (GtkCellArea *area);
108 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
109 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
110 GtkCellAreaContext *context,
113 gint *natural_width);
114 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
115 GtkCellAreaContext *context,
117 gint *minimum_height,
118 gint *natural_height);
119 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
120 GtkCellAreaContext *context,
123 gint *minimum_height,
124 gint *natural_height);
125 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
126 GtkCellAreaContext *context,
130 gint *natural_width);
131 static gboolean gtk_cell_area_box_focus (GtkCellArea *area,
132 GtkDirectionType direction);
134 /* GtkCellLayoutIface */
135 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
136 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
137 GtkCellRenderer *renderer,
139 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
140 GtkCellRenderer *renderer,
142 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
143 GtkCellRenderer *renderer,
147 /* CellInfo/CellGroup metadata handling and convenience functions */
149 GtkCellRenderer *renderer;
151 guint expand : 1; /* Whether the cell expands */
152 guint pack : 1; /* Whether the cell is packed from the start or end */
153 guint align : 1; /* Whether to align this cell's position with adjacent rows */
161 guint expand_cells : 8;
165 GtkCellRenderer *renderer;
171 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
175 static void cell_info_free (CellInfo *info);
176 static gint cell_info_find (CellInfo *info,
177 GtkCellRenderer *renderer);
179 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
182 static void allocated_cell_free (AllocatedCell *cell);
183 static GList *list_consecutive_cells (GtkCellAreaBox *box);
184 static gint count_expand_groups (GtkCellAreaBox *box);
185 static void context_weak_notify (GtkCellAreaBox *box,
186 GtkCellAreaBoxContext *dead_context);
187 static void reset_contexts (GtkCellAreaBox *box);
188 static void init_context_groups (GtkCellAreaBox *box);
189 static void init_context_group (GtkCellAreaBox *box,
190 GtkCellAreaBoxContext *context);
191 static GSList *get_allocated_cells (GtkCellAreaBox *box,
192 GtkCellAreaBoxContext *context,
198 struct _GtkCellAreaBoxPrivate
200 GtkOrientation orientation;
209 /* We hold on to the rtl state from a widget we are requested for
210 * so that we can navigate focus correctly */
227 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
228 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
229 gtk_cell_area_box_cell_layout_init)
230 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
232 #define OPPOSITE_ORIENTATION(orientation) \
233 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
234 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
237 gtk_cell_area_box_init (GtkCellAreaBox *box)
239 GtkCellAreaBoxPrivate *priv;
241 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
242 GTK_TYPE_CELL_AREA_BOX,
243 GtkCellAreaBoxPrivate);
246 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
247 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
249 priv->contexts = NULL;
255 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
257 GObjectClass *object_class = G_OBJECT_CLASS (class);
258 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
261 object_class->finalize = gtk_cell_area_box_finalize;
262 object_class->dispose = gtk_cell_area_box_dispose;
263 object_class->set_property = gtk_cell_area_box_set_property;
264 object_class->get_property = gtk_cell_area_box_get_property;
266 /* GtkCellAreaClass */
267 area_class->add = gtk_cell_area_box_add;
268 area_class->remove = gtk_cell_area_box_remove;
269 area_class->forall = gtk_cell_area_box_forall;
270 area_class->get_cell_allocation = gtk_cell_area_box_get_cell_allocation;
271 area_class->event = gtk_cell_area_box_event;
272 area_class->render = gtk_cell_area_box_render;
273 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
274 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
276 area_class->create_context = gtk_cell_area_box_create_context;
277 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
278 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
279 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
280 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
281 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
283 area_class->focus = gtk_cell_area_box_focus;
286 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
289 * GtkCellAreaBox:spacing:
291 * The amount of space to reserve between cells.
295 g_object_class_install_property (object_class,
297 g_param_spec_int ("spacing",
299 P_("Space which is inserted between cells"),
303 GTK_PARAM_READWRITE));
305 /* Cell Properties */
307 * GtkCellAreaBox:expand:
309 * Whether the cell renderer should receive extra space when the area receives
310 * more than its natural size.
314 gtk_cell_area_class_install_cell_property (area_class,
319 P_("Whether the cell expands"),
321 GTK_PARAM_READWRITE));
324 * GtkCellAreaBox:align:
326 * Whether the cell renderer should be aligned in adjacent rows.
330 gtk_cell_area_class_install_cell_property (area_class,
335 P_("Whether cell should align with adjacent rows"),
337 GTK_PARAM_READWRITE));
340 * GtkCellAreaBox:pack-type:
342 * A GtkPackType indicating whether the cell renderer is packed with reference to the
343 * start or end of the area.
347 gtk_cell_area_class_install_cell_property (area_class,
352 P_("A GtkPackType indicating whether the cell is packed with "
353 "reference to the start or end of the cell area"),
354 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
355 GTK_PARAM_READWRITE));
357 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
361 /*************************************************************
362 * CellInfo/CellGroup basics and convenience functions *
363 *************************************************************/
365 cell_info_new (GtkCellRenderer *renderer,
370 CellInfo *info = g_slice_new (CellInfo);
372 info->renderer = g_object_ref_sink (renderer);
374 info->expand = expand;
381 cell_info_free (CellInfo *info)
383 g_object_unref (info->renderer);
385 g_slice_free (CellInfo, info);
389 cell_info_find (CellInfo *info,
390 GtkCellRenderer *renderer)
392 return (info->renderer == renderer) ? 0 : -1;
395 static AllocatedCell *
396 allocated_cell_new (GtkCellRenderer *renderer,
400 AllocatedCell *cell = g_slice_new (AllocatedCell);
402 cell->renderer = renderer;
403 cell->position = position;
410 allocated_cell_free (AllocatedCell *cell)
412 g_slice_free (AllocatedCell, cell);
416 list_consecutive_cells (GtkCellAreaBox *box)
418 GtkCellAreaBoxPrivate *priv = box->priv;
419 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
422 /* List cells in consecutive order taking their
423 * PACK_START/PACK_END options into account
425 for (l = priv->cells; l; l = l->next)
429 if (info->pack == GTK_PACK_START)
430 consecutive_cells = g_list_prepend (consecutive_cells, info);
433 for (l = priv->cells; l; l = l->next)
437 if (info->pack == GTK_PACK_END)
438 pack_end_cells = g_list_prepend (pack_end_cells, info);
441 consecutive_cells = g_list_reverse (consecutive_cells);
442 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
444 return consecutive_cells;
448 cell_groups_clear (GtkCellAreaBox *box)
450 GtkCellAreaBoxPrivate *priv = box->priv;
453 for (i = 0; i < priv->groups->len; i++)
455 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
457 g_list_free (group->cells);
460 g_array_set_size (priv->groups, 0);
464 cell_groups_rebuild (GtkCellAreaBox *box)
466 GtkCellAreaBoxPrivate *priv = box->priv;
467 CellGroup group = { 0, };
468 CellGroup *group_ptr;
472 cell_groups_clear (box);
477 cells = list_consecutive_cells (box);
479 /* First group is implied */
480 g_array_append_val (priv->groups, group);
481 group_ptr = &g_array_index (priv->groups, CellGroup, id);
483 for (l = cells; l; l = l->next)
485 CellInfo *info = l->data;
487 /* A new group starts with any aligned cell, the first group is implied */
488 if (info->align && l != cells)
490 memset (&group, 0x0, sizeof (CellGroup));
493 g_array_append_val (priv->groups, group);
494 group_ptr = &g_array_index (priv->groups, CellGroup, id);
497 group_ptr->cells = g_list_prepend (group_ptr->cells, info);
498 group_ptr->n_cells++;
500 /* A group expands if it contains any expand cells */
502 group_ptr->expand_cells++;
507 for (id = 0; id < priv->groups->len; id++)
509 group_ptr = &g_array_index (priv->groups, CellGroup, id);
511 group_ptr->cells = g_list_reverse (group_ptr->cells);
514 /* Contexts need to be updated with the new grouping information */
515 init_context_groups (box);
519 count_visible_cells (CellGroup *group,
523 gint visible_cells = 0;
524 gint n_expand_cells = 0;
526 for (l = group->cells; l; l = l->next)
528 CellInfo *info = l->data;
530 if (gtk_cell_renderer_get_visible (info->renderer))
540 *expand_cells = n_expand_cells;
542 return visible_cells;
546 count_expand_groups (GtkCellAreaBox *box)
548 GtkCellAreaBoxPrivate *priv = box->priv;
550 gint expand_groups = 0;
552 for (i = 0; i < priv->groups->len; i++)
554 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
556 if (group->expand_cells > 0)
560 return expand_groups;
564 context_weak_notify (GtkCellAreaBox *box,
565 GtkCellAreaBoxContext *dead_context)
567 GtkCellAreaBoxPrivate *priv = box->priv;
569 priv->contexts = g_slist_remove (priv->contexts, dead_context);
573 init_context_group (GtkCellAreaBox *box,
574 GtkCellAreaBoxContext *context)
576 GtkCellAreaBoxPrivate *priv = box->priv;
577 gint *expand_groups, i;
579 expand_groups = g_new (gboolean, priv->groups->len);
581 for (i = 0; i < priv->groups->len; i++)
583 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
585 expand_groups[i] = (group->expand_cells > 0);
588 /* This call implies reseting the request info */
589 gtk_cell_area_box_init_groups (context, priv->groups->len, expand_groups);
590 g_free (expand_groups);
594 init_context_groups (GtkCellAreaBox *box)
596 GtkCellAreaBoxPrivate *priv = box->priv;
599 /* When the box's groups are reconstructed, contexts need to
602 for (l = priv->contexts; l; l = l->next)
604 GtkCellAreaBoxContext *context = l->data;
606 init_context_group (box, context);
611 reset_contexts (GtkCellAreaBox *box)
613 GtkCellAreaBoxPrivate *priv = box->priv;
616 /* When the box layout changes, contexts need to
617 * be reset and sizes for the box get requested again
619 for (l = priv->contexts; l; l = l->next)
621 GtkCellAreaContext *context = l->data;
623 gtk_cell_area_context_reset (context);
627 /* Fall back on a completely unaligned dynamic allocation of cells
628 * when not allocated for the said orientation, alignment of cells
629 * is not done when each area gets a different size in the orientation
633 allocate_cells_manually (GtkCellAreaBox *box,
638 GtkCellAreaBoxPrivate *priv = box->priv;
640 GSList *allocated_cells = NULL;
641 GtkRequestedSize *sizes;
643 gint nvisible = 0, nexpand = 0, group_expand;
644 gint avail_size, extra_size, extra_extra, full_size;
645 gint position = 0, for_size;
651 /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
652 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
653 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
655 cells = list_consecutive_cells (box);
657 /* Count the visible and expand cells */
658 for (i = 0; i < priv->groups->len; i++)
660 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
662 nvisible += count_visible_cells (group, &group_expand);
663 nexpand += group_expand;
672 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
674 full_size = avail_size = width;
679 full_size = avail_size = height;
683 /* Go ahead and collect the requests on the fly */
684 sizes = g_new0 (GtkRequestedSize, nvisible);
685 for (l = cells, i = 0; l; l = l->next)
687 CellInfo *info = l->data;
689 if (!gtk_cell_renderer_get_visible (info->renderer))
692 gtk_cell_area_request_renderer (GTK_CELL_AREA (box), info->renderer,
695 &sizes[i].minimum_size,
696 &sizes[i].natural_size);
698 avail_size -= sizes[i].minimum_size;
700 sizes[i].data = info;
705 /* Naturally distribute the allocation */
706 avail_size -= (nvisible - 1) * priv->spacing;
707 avail_size = gtk_distribute_natural_allocation (avail_size, nvisible, sizes);
709 /* Calculate/distribute expand for cells */
712 extra_size = avail_size / nexpand;
713 extra_extra = avail_size % nexpand;
716 extra_size = extra_extra = 0;
718 /* Create the allocated cells */
719 for (i = 0; i < nvisible; i++)
721 CellInfo *info = sizes[i].data;
726 sizes[i].minimum_size += extra_size;
729 sizes[i].minimum_size++;
735 cell = allocated_cell_new (info->renderer,
736 full_size - (position + sizes[i].minimum_size),
737 sizes[i].minimum_size);
739 cell = allocated_cell_new (info->renderer, position, sizes[i].minimum_size);
741 allocated_cells = g_slist_prepend (allocated_cells, cell);
743 position += sizes[i].minimum_size;
744 position += priv->spacing;
750 /* Note it might not be important to reverse the list here at all,
751 * we have the correct positions, no need to allocate from left to right */
752 return g_slist_reverse (allocated_cells);
755 /* Returns an allocation for each cell in the orientation of the box,
756 * used in ->render()/->event() implementations to get a straight-forward
757 * list of allocated cells to operate on.
760 get_allocated_cells (GtkCellAreaBox *box,
761 GtkCellAreaBoxContext *context,
766 GtkCellAreaBoxAllocation *group_allocs;
767 GtkCellArea *area = GTK_CELL_AREA (box);
768 GtkCellAreaBoxPrivate *priv = box->priv;
770 GSList *allocated_cells = NULL;
772 gint for_size, full_size;
775 group_allocs = gtk_cell_area_box_context_get_orientation_allocs (context, &n_allocs);
777 return allocate_cells_manually (box, widget, width, height);
779 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
790 /* For vertical oriented boxes, we just let the cell renderers realign themselves for rtl */
791 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
792 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
794 for (i = 0; i < n_allocs; i++)
796 /* We dont always allocate all groups, sometimes the requested group has only invisible
797 * cells for every row, hence the usage of group_allocs[i].group_idx here
799 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
801 /* Exception for single cell groups */
802 if (group->n_cells == 1)
804 CellInfo *info = group->cells->data;
808 cell = allocated_cell_new (info->renderer,
809 full_size - (group_allocs[i].position + group_allocs[i].size),
810 group_allocs[i].size);
812 cell = allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
814 allocated_cells = g_slist_prepend (allocated_cells, cell);
818 GtkRequestedSize *sizes;
819 gint avail_size, position;
820 gint visible_cells, expand_cells;
821 gint extra_size, extra_extra;
823 visible_cells = count_visible_cells (group, &expand_cells);
825 /* If this row has no visible cells in this group, just
826 * skip the allocation */
827 if (visible_cells == 0)
830 /* Offset the allocation to the group position and allocate into
831 * the group's available size */
832 position = group_allocs[i].position;
833 avail_size = group_allocs[i].size;
835 sizes = g_new (GtkRequestedSize, visible_cells);
837 for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
839 CellInfo *info = cell_list->data;
841 if (!gtk_cell_renderer_get_visible (info->renderer))
844 gtk_cell_area_request_renderer (area, info->renderer,
847 &sizes[j].minimum_size,
848 &sizes[j].natural_size);
850 sizes[j].data = info;
851 avail_size -= sizes[j].minimum_size;
856 /* Distribute cells naturally within the group */
857 avail_size -= (visible_cells - 1) * priv->spacing;
859 avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
863 /* Calculate/distribute expand for cells */
864 if (expand_cells > 0)
866 extra_size = avail_size / expand_cells;
867 extra_extra = avail_size % expand_cells;
870 extra_size = extra_extra = 0;
872 /* Create the allocated cells (loop only over visible cells here) */
873 for (j = 0; j < visible_cells; j++)
875 CellInfo *info = sizes[j].data;
880 sizes[j].minimum_size += extra_size;
883 sizes[j].minimum_size++;
889 cell = allocated_cell_new (info->renderer,
890 full_size - (position + sizes[j].minimum_size),
891 sizes[j].minimum_size);
893 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
895 allocated_cells = g_slist_prepend (allocated_cells, cell);
897 position += sizes[j].minimum_size;
898 position += priv->spacing;
905 /* Note it might not be important to reverse the list here at all,
906 * we have the correct positions, no need to allocate from left to right */
907 return g_slist_reverse (allocated_cells);
910 /*************************************************************
912 *************************************************************/
914 gtk_cell_area_box_finalize (GObject *object)
916 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
917 GtkCellAreaBoxPrivate *priv = box->priv;
920 /* Unref/free the context list */
921 for (l = priv->contexts; l; l = l->next)
922 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)context_weak_notify, box);
924 g_slist_free (priv->contexts);
925 priv->contexts = NULL;
927 /* Free the cell grouping info */
928 cell_groups_clear (box);
929 g_array_free (priv->groups, TRUE);
931 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
935 gtk_cell_area_box_dispose (GObject *object)
937 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
941 gtk_cell_area_box_set_property (GObject *object,
946 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
950 case PROP_ORIENTATION:
951 box->priv->orientation = g_value_get_enum (value);
953 /* Notify that size needs to be requested again */
954 reset_contexts (box);
957 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
966 gtk_cell_area_box_get_property (GObject *object,
971 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
975 case PROP_ORIENTATION:
976 g_value_set_enum (value, box->priv->orientation);
979 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
982 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
987 /*************************************************************
989 *************************************************************/
991 gtk_cell_area_box_add (GtkCellArea *area,
992 GtkCellRenderer *renderer)
994 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
995 renderer, FALSE, TRUE);
999 gtk_cell_area_box_remove (GtkCellArea *area,
1000 GtkCellRenderer *renderer)
1002 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1003 GtkCellAreaBoxPrivate *priv = box->priv;
1006 node = g_list_find_custom (priv->cells, renderer,
1007 (GCompareFunc)cell_info_find);
1011 CellInfo *info = node->data;
1013 cell_info_free (info);
1015 priv->cells = g_list_delete_link (priv->cells, node);
1017 /* Reconstruct cell groups */
1018 cell_groups_rebuild (box);
1021 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
1025 gtk_cell_area_box_forall (GtkCellArea *area,
1026 GtkCellCallback callback,
1027 gpointer callback_data)
1029 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1030 GtkCellAreaBoxPrivate *priv = box->priv;
1033 for (list = priv->cells; list; list = list->next)
1035 CellInfo *info = list->data;
1037 callback (info->renderer, callback_data);
1042 gtk_cell_area_box_get_cell_allocation (GtkCellArea *area,
1043 GtkCellAreaContext *context,
1045 GtkCellRenderer *renderer,
1046 const GdkRectangle *cell_area,
1047 GdkRectangle *allocation)
1049 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1050 GtkCellAreaBoxPrivate *priv = box->priv;
1051 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1052 GSList *allocated_cells, *l;
1054 *allocation = *cell_area;
1056 /* Get a list of cells with allocation sizes decided regardless
1057 * of alignments and pack order etc. */
1058 allocated_cells = get_allocated_cells (box, box_context, widget,
1059 cell_area->width, cell_area->height);
1061 for (l = allocated_cells; l; l = l->next)
1063 AllocatedCell *cell = l->data;
1065 if (cell->renderer == renderer)
1067 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1069 allocation->x = cell_area->x + cell->position;
1070 allocation->width = cell->size;
1074 allocation->y = cell_area->y + cell->position;
1075 allocation->height = cell->size;
1082 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1083 g_slist_free (allocated_cells);
1093 gtk_cell_area_box_event (GtkCellArea *area,
1094 GtkCellAreaContext *context,
1097 const GdkRectangle *cell_area,
1098 GtkCellRendererState flags)
1102 /* First let the parent class handle activation of cells via keystrokes */
1104 GTK_CELL_AREA_CLASS (gtk_cell_area_box_parent_class)->event (area, context, widget,
1105 event, cell_area, flags);
1110 /* Also detect mouse events, for mouse events we need to allocate the renderers
1111 * and find which renderer needs to be activated.
1113 if (event->type == GDK_BUTTON_PRESS)
1115 GdkEventButton *button_event = (GdkEventButton *)event;
1117 if (button_event->button == 1)
1119 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1120 GtkCellAreaBoxPrivate *priv = box->priv;
1121 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1122 GSList *allocated_cells, *l;
1123 GdkRectangle cell_background, inner_area;
1124 gint event_x, event_y;
1126 /* We may need some semantics to tell us the offset of the event
1127 * window we are handling events for (i.e. GtkTreeView has a bin_window) */
1128 event_x = button_event->x;
1129 event_y = button_event->y;
1131 cell_background = *cell_area;
1133 /* Get a list of cells with allocation sizes decided regardless
1134 * of alignments and pack order etc. */
1135 allocated_cells = get_allocated_cells (box, box_context, widget,
1136 cell_area->width, cell_area->height);
1138 for (l = allocated_cells; l; l = l->next)
1140 AllocatedCell *cell = l->data;
1142 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1144 cell_background.x = cell_area->x + cell->position;
1145 cell_background.width = cell->size;
1149 cell_background.y = cell_area->y + cell->position;
1150 cell_background.height = cell->size;
1153 /* Remove margins from the background area to produce the cell area
1155 gtk_cell_area_inner_cell_area (area, widget, &cell_background, &inner_area);
1157 if (event_x >= inner_area.x && event_x <= inner_area.x + inner_area.width &&
1158 event_y >= inner_area.y && event_y <= inner_area.y + inner_area.height)
1160 GtkCellRenderer *event_renderer = NULL;
1161 GtkCellRenderer *focus_renderer;
1163 focus_renderer = gtk_cell_area_get_focus_from_sibling (area, cell->renderer);
1165 event_renderer = focus_renderer;
1167 event_renderer = cell->renderer;
1169 event_renderer = cell->renderer;
1173 if (gtk_cell_area_get_edited_cell (area))
1175 /* XXX Was it really canceled in this case ? */
1176 gtk_cell_area_stop_editing (area, TRUE);
1177 gtk_cell_area_set_focus_cell (area, event_renderer);
1182 /* If we are activating via a focus sibling, we need to fix the
1184 if (event_renderer != cell->renderer)
1185 gtk_cell_area_inner_cell_area (area, widget, cell_area, &cell_background);
1187 gtk_cell_area_set_focus_cell (area, event_renderer);
1189 retval = gtk_cell_area_activate_cell (area, widget, event_renderer,
1190 event, &cell_background, flags);
1196 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1197 g_slist_free (allocated_cells);
1205 gtk_cell_area_box_render (GtkCellArea *area,
1206 GtkCellAreaContext *context,
1209 const GdkRectangle *background_area,
1210 const GdkRectangle *cell_area,
1211 GtkCellRendererState flags,
1212 gboolean paint_focus)
1214 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1215 GtkCellAreaBoxPrivate *priv = box->priv;
1216 GtkCellAreaBoxContext *box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1217 GSList *allocated_cells, *l;
1218 GdkRectangle cell_background, inner_area;
1219 GtkCellRenderer *focus_cell = NULL;
1220 GdkRectangle focus_rect = { 0, };
1221 gboolean first_focus_cell = TRUE;
1222 gboolean focus_all = FALSE;
1225 rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1226 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1228 /* Make sure we dont paint a focus rectangle while there
1229 * is an editable widget in play
1231 if (gtk_cell_area_get_edited_cell (area))
1232 paint_focus = FALSE;
1234 if (flags & GTK_CELL_RENDERER_FOCUSED)
1236 focus_cell = gtk_cell_area_get_focus_cell (area);
1237 flags &= ~GTK_CELL_RENDERER_FOCUSED;
1239 /* If no cell can activate but the caller wants focus painted,
1240 * then we paint focus around all cells */
1241 if (paint_focus && !gtk_cell_area_is_activatable (area))
1245 cell_background = *cell_area;
1247 /* Get a list of cells with allocation sizes decided regardless
1248 * of alignments and pack order etc. */
1249 allocated_cells = get_allocated_cells (box, box_context, widget,
1250 cell_area->width, cell_area->height);
1252 for (l = allocated_cells; l; l = l->next)
1254 AllocatedCell *cell = l->data;
1255 GtkCellRendererState cell_fields = 0;
1256 GdkRectangle render_background;
1258 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1260 cell_background.x = cell_area->x + cell->position;
1261 cell_background.width = cell->size;
1265 cell_background.y = cell_area->y + cell->position;
1266 cell_background.height = cell->size;
1269 /* Stop rendering cells if they flow out of the render area,
1270 * this can happen because the render area can actually be
1271 * smaller than the requested area (treeview columns can
1272 * be user resizable and can be resized to be smaller than
1273 * the actual requested area). */
1274 if (cell_background.x > cell_area->x + cell_area->width ||
1275 cell_background.x + cell_background.width < cell_area->x ||
1276 cell_background.y > cell_area->y + cell_area->height)
1279 /* Special case for the last cell (or first cell in rtl)... let the last cell consume
1280 * the remaining space in the area (the last cell is allowed to consume the remaining
1281 * space if the space given for rendering is actually larger than allocation, this can
1282 * happen in the expander GtkTreeViewColumn where only the deepest depth column
1283 * receives the allocation... shallow columns recieve more width). */
1288 /* Fill the leading space for the first cell in the area (still last in the list) */
1289 cell_background.width = (cell_background.x - cell_area->x) + cell_background.width;
1290 cell_background.x = cell_area->x;
1294 cell_background.width = cell_area->x + cell_area->width - cell_background.x;
1295 cell_background.height = cell_area->y + cell_area->height - cell_background.y;
1300 /* If the cell we are rendering doesnt fit into the remaining space, clip it
1301 * so that the underlying renderer has a chance to deal with it (for instance
1302 * text renderers get a chance to ellipsize).
1304 if (cell_background.x + cell_background.width > cell_area->x + cell_area->width)
1305 cell_background.width = cell_area->x + cell_area->width - cell_background.x;
1307 if (cell_background.y + cell_background.height > cell_area->y + cell_area->height)
1308 cell_background.height = cell_area->y + cell_area->height - cell_background.y;
1311 /* Remove margins from the background area to produce the cell area
1313 gtk_cell_area_inner_cell_area (area, widget, &cell_background, &inner_area);
1315 /* Add portions of the background_area to the cell_background
1316 * to create the render_background */
1317 render_background = cell_background;
1319 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1321 if (l == allocated_cells)
1323 render_background.width += render_background.x - background_area->x;
1324 render_background.x = background_area->x;
1327 if (l->next == NULL)
1328 render_background.width =
1329 background_area->width - (render_background.x - background_area->x);
1331 render_background.y = background_area->y;
1332 render_background.height = background_area->height;
1336 if (l == allocated_cells)
1338 render_background.height += render_background.y - background_area->y;
1339 render_background.y = background_area->y;
1342 if (l->next == NULL)
1343 render_background.height =
1344 background_area->height - (render_background.y - background_area->y);
1346 render_background.x = background_area->x;
1347 render_background.width = background_area->width;
1352 (cell->renderer == focus_cell ||
1353 gtk_cell_area_is_focus_sibling (area, focus_cell, cell->renderer))))
1355 cell_fields |= GTK_CELL_RENDERER_FOCUSED;
1359 GdkRectangle cell_focus;
1361 gtk_cell_renderer_get_aligned_area (cell->renderer, widget, flags, &inner_area, &cell_focus);
1363 /* Accumulate the focus rectangle for all focus siblings */
1364 if (first_focus_cell)
1366 focus_rect = cell_focus;
1367 first_focus_cell = FALSE;
1370 gdk_rectangle_union (&focus_rect, &cell_focus, &focus_rect);
1374 /* We have to do some per-cell considerations for the 'flags'
1375 * for focus handling */
1376 gtk_cell_renderer_render (cell->renderer, cr, widget,
1377 &render_background, &inner_area,
1378 flags | cell_fields);
1381 if (paint_focus && focus_rect.width != 0 && focus_rect.height != 0)
1383 GtkStateType renderer_state =
1384 flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
1385 (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
1386 (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL));
1388 gtk_paint_focus (gtk_widget_get_style (widget), cr,
1389 renderer_state, widget,
1390 gtk_cell_area_get_style_detail (area),
1391 focus_rect.x, focus_rect.y,
1392 focus_rect.width, focus_rect.height);
1396 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
1397 g_slist_free (allocated_cells);
1401 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
1402 GtkCellRenderer *renderer,
1404 const GValue *value,
1407 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1408 GtkCellAreaBoxPrivate *priv = box->priv;
1411 gboolean rebuild = FALSE;
1413 GtkPackType pack_type;
1415 node = g_list_find_custom (priv->cells, renderer,
1416 (GCompareFunc)cell_info_find);
1424 case CELL_PROP_EXPAND:
1425 val = g_value_get_boolean (value);
1427 if (info->expand != val)
1434 case CELL_PROP_ALIGN:
1435 val = g_value_get_boolean (value);
1437 if (info->align != val)
1444 case CELL_PROP_PACK_TYPE:
1445 pack_type = g_value_get_enum (value);
1447 if (info->pack != pack_type)
1449 info->pack = pack_type;
1454 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1458 /* Groups need to be rebuilt */
1460 cell_groups_rebuild (box);
1464 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
1465 GtkCellRenderer *renderer,
1470 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1471 GtkCellAreaBoxPrivate *priv = box->priv;
1475 node = g_list_find_custom (priv->cells, renderer,
1476 (GCompareFunc)cell_info_find);
1484 case CELL_PROP_EXPAND:
1485 g_value_set_boolean (value, info->expand);
1488 case CELL_PROP_ALIGN:
1489 g_value_set_boolean (value, info->align);
1492 case CELL_PROP_PACK_TYPE:
1493 g_value_set_enum (value, info->pack);
1496 GTK_CELL_AREA_WARN_INVALID_CELL_PROPERTY_ID (area, prop_id, pspec);
1502 static GtkCellAreaContext *
1503 gtk_cell_area_box_create_context (GtkCellArea *area)
1505 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1506 GtkCellAreaBoxPrivate *priv = box->priv;
1507 GtkCellAreaContext *context =
1508 (GtkCellAreaContext *)g_object_new (GTK_TYPE_CELL_AREA_BOX_CONTEXT,
1509 "area", area, NULL);
1511 priv->contexts = g_slist_prepend (priv->contexts, context);
1513 g_object_weak_ref (G_OBJECT (context), (GWeakNotify)context_weak_notify, box);
1515 /* Tell the new group about our cell layout */
1516 init_context_group (box, GTK_CELL_AREA_BOX_CONTEXT (context));
1521 static GtkSizeRequestMode
1522 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1524 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1525 GtkCellAreaBoxPrivate *priv = box->priv;
1527 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1528 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1529 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1533 compute_size (GtkCellAreaBox *box,
1534 GtkOrientation orientation,
1535 GtkCellAreaBoxContext *context,
1541 GtkCellAreaBoxPrivate *priv = box->priv;
1542 GtkCellArea *area = GTK_CELL_AREA (box);
1548 for (i = 0; i < priv->groups->len; i++)
1550 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1551 gint group_min_size = 0;
1552 gint group_nat_size = 0;
1554 for (list = group->cells; list; list = list->next)
1556 CellInfo *info = list->data;
1557 gint renderer_min_size, renderer_nat_size;
1559 if (!gtk_cell_renderer_get_visible (info->renderer))
1562 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1563 &renderer_min_size, &renderer_nat_size);
1565 if (orientation == priv->orientation)
1569 min_size += priv->spacing;
1570 nat_size += priv->spacing;
1573 if (group_min_size > 0)
1575 group_min_size += priv->spacing;
1576 group_nat_size += priv->spacing;
1579 min_size += renderer_min_size;
1580 nat_size += renderer_nat_size;
1581 group_min_size += renderer_min_size;
1582 group_nat_size += renderer_nat_size;
1586 min_size = MAX (min_size, renderer_min_size);
1587 nat_size = MAX (nat_size, renderer_nat_size);
1588 group_min_size = MAX (group_min_size, renderer_min_size);
1589 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1593 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1596 gtk_cell_area_box_context_push_group_width (context, group->id, group_min_size, group_nat_size);
1598 gtk_cell_area_box_context_push_group_width_for_height (context, group->id, for_size,
1599 group_min_size, group_nat_size);
1604 gtk_cell_area_box_context_push_group_height (context, group->id, group_min_size, group_nat_size);
1606 gtk_cell_area_box_context_push_group_height_for_width (context, group->id, for_size,
1607 group_min_size, group_nat_size);
1611 *minimum_size = min_size;
1612 *natural_size = nat_size;
1614 /* Update rtl state for focus navigation to work */
1615 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1616 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1620 get_group_sizes (GtkCellArea *area,
1622 GtkOrientation orientation,
1626 GtkRequestedSize *sizes;
1630 *n_sizes = count_visible_cells (group, NULL);
1631 sizes = g_new (GtkRequestedSize, *n_sizes);
1633 for (l = group->cells, i = 0; l; l = l->next)
1635 CellInfo *info = l->data;
1637 if (!gtk_cell_renderer_get_visible (info->renderer))
1640 sizes[i].data = info;
1642 gtk_cell_area_request_renderer (area, info->renderer,
1643 orientation, widget, -1,
1644 &sizes[i].minimum_size,
1645 &sizes[i].natural_size);
1654 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1661 GtkCellAreaBoxPrivate *priv = box->priv;
1662 GtkCellArea *area = GTK_CELL_AREA (box);
1664 /* Exception for single cell groups */
1665 if (group->n_cells == 1)
1667 CellInfo *info = group->cells->data;
1669 gtk_cell_area_request_renderer (area, info->renderer,
1670 OPPOSITE_ORIENTATION (priv->orientation),
1671 widget, for_size, minimum_size, natural_size);
1675 GtkRequestedSize *orientation_sizes;
1678 gint avail_size = for_size;
1679 gint extra_size, extra_extra;
1680 gint min_size = 0, nat_size = 0;
1682 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1684 /* First naturally allocate the cells in the group into the for_size */
1685 avail_size -= (n_sizes - 1) * priv->spacing;
1686 for (i = 0; i < n_sizes; i++)
1687 avail_size -= orientation_sizes[i].minimum_size;
1690 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1694 /* Calculate/distribute expand for cells */
1695 if (group->expand_cells > 0)
1697 extra_size = avail_size / group->expand_cells;
1698 extra_extra = avail_size % group->expand_cells;
1701 extra_size = extra_extra = 0;
1703 for (i = 0; i < n_sizes; i++)
1705 gint cell_min, cell_nat;
1707 info = orientation_sizes[i].data;
1711 orientation_sizes[i].minimum_size += extra_size;
1714 orientation_sizes[i].minimum_size++;
1719 gtk_cell_area_request_renderer (area, info->renderer,
1720 OPPOSITE_ORIENTATION (priv->orientation),
1722 orientation_sizes[i].minimum_size,
1723 &cell_min, &cell_nat);
1725 min_size = MAX (min_size, cell_min);
1726 nat_size = MAX (nat_size, cell_nat);
1729 *minimum_size = min_size;
1730 *natural_size = nat_size;
1732 g_free (orientation_sizes);
1737 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1738 GtkCellAreaBoxContext *context,
1744 GtkCellAreaBoxPrivate *priv = box->priv;
1746 GtkRequestedSize *orientation_sizes;
1747 gint n_groups, n_expand_groups, i;
1748 gint avail_size = for_size;
1749 gint extra_size, extra_extra;
1750 gint min_size = 0, nat_size = 0;
1752 n_expand_groups = count_expand_groups (box);
1754 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1755 orientation_sizes = gtk_cell_area_box_context_get_widths (context, &n_groups);
1757 orientation_sizes = gtk_cell_area_box_context_get_heights (context, &n_groups);
1759 /* First start by naturally allocating space among groups of cells */
1760 avail_size -= (n_groups - 1) * priv->spacing;
1761 for (i = 0; i < n_groups; i++)
1762 avail_size -= orientation_sizes[i].minimum_size;
1765 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1769 /* Calculate/distribute expand for groups */
1770 if (n_expand_groups > 0)
1772 extra_size = avail_size / n_expand_groups;
1773 extra_extra = avail_size % n_expand_groups;
1776 extra_size = extra_extra = 0;
1778 /* Now we need to naturally allocate sizes for cells in each group
1779 * and push the height-for-width for each group accordingly while accumulating
1780 * the overall height-for-width for this row.
1782 for (i = 0; i < n_groups; i++)
1784 gint group_min, group_nat;
1785 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1787 group = &g_array_index (priv->groups, CellGroup, group_idx);
1789 if (group->expand_cells > 0)
1791 orientation_sizes[i].minimum_size += extra_size;
1794 orientation_sizes[i].minimum_size++;
1799 /* Now we have the allocation for the group, request it's height-for-width */
1800 compute_group_size_for_opposing_orientation (box, group, widget,
1801 orientation_sizes[i].minimum_size,
1802 &group_min, &group_nat);
1804 min_size = MAX (min_size, group_min);
1805 nat_size = MAX (nat_size, group_nat);
1807 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1809 gtk_cell_area_box_context_push_group_height_for_width (context, group_idx, for_size,
1810 group_min, group_nat);
1814 gtk_cell_area_box_context_push_group_width_for_height (context, group_idx, for_size,
1815 group_min, group_nat);
1819 *minimum_size = min_size;
1820 *natural_size = nat_size;
1822 g_free (orientation_sizes);
1824 /* Update rtl state for focus navigation to work */
1825 priv->rtl = (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1826 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1832 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1833 GtkCellAreaContext *context,
1835 gint *minimum_width,
1836 gint *natural_width)
1838 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1839 GtkCellAreaBoxContext *box_context;
1840 gint min_width, nat_width;
1842 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1844 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1846 /* Compute the size of all renderers for current row data,
1847 * bumping cell alignments in the context along the way */
1848 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1849 box_context, widget, -1, &min_width, &nat_width);
1852 *minimum_width = min_width;
1855 *natural_width = nat_width;
1859 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1860 GtkCellAreaContext *context,
1862 gint *minimum_height,
1863 gint *natural_height)
1865 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1866 GtkCellAreaBoxContext *box_context;
1867 gint min_height, nat_height;
1869 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1871 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1873 /* Compute the size of all renderers for current row data,
1874 * bumping cell alignments in the context along the way */
1875 compute_size (box, GTK_ORIENTATION_VERTICAL,
1876 box_context, widget, -1, &min_height, &nat_height);
1879 *minimum_height = min_height;
1882 *natural_height = nat_height;
1886 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1887 GtkCellAreaContext *context,
1890 gint *minimum_height,
1891 gint *natural_height)
1893 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1894 GtkCellAreaBoxContext *box_context;
1895 GtkCellAreaBoxPrivate *priv;
1896 gint min_height, nat_height;
1898 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1900 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1903 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1905 /* Add up vertical requests of height for width and push the overall
1906 * cached sizes for alignments */
1907 compute_size (box, priv->orientation, box_context, widget, width, &min_height, &nat_height);
1911 /* Juice: virtually allocate cells into the for_width using the
1912 * alignments and then return the overall height for that width, and cache it */
1913 compute_size_for_opposing_orientation (box, box_context, widget, width, &min_height, &nat_height);
1917 *minimum_height = min_height;
1920 *natural_height = nat_height;
1924 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1925 GtkCellAreaContext *context,
1928 gint *minimum_width,
1929 gint *natural_width)
1931 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1932 GtkCellAreaBoxContext *box_context;
1933 GtkCellAreaBoxPrivate *priv;
1934 gint min_width, nat_width;
1936 g_return_if_fail (GTK_IS_CELL_AREA_BOX_CONTEXT (context));
1938 box_context = GTK_CELL_AREA_BOX_CONTEXT (context);
1941 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1943 /* Add up horizontal requests of width for height and push the overall
1944 * cached sizes for alignments */
1945 compute_size (box, priv->orientation, box_context, widget, height, &min_width, &nat_width);
1949 /* Juice: horizontally allocate cells into the for_height using the
1950 * alignments and then return the overall width for that height, and cache it */
1951 compute_size_for_opposing_orientation (box, box_context, widget, height, &min_width, &nat_width);
1955 *minimum_width = min_width;
1958 *natural_width = nat_width;
1962 gtk_cell_area_box_focus (GtkCellArea *area,
1963 GtkDirectionType direction)
1965 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1966 GtkCellAreaBoxPrivate *priv = box->priv;
1967 gint cycle = FOCUS_NONE;
1968 gboolean cycled_focus = FALSE;
1969 GtkCellRenderer *focus_cell;
1971 focus_cell = gtk_cell_area_get_focus_cell (area);
1973 /* Special case, when there is no activatable cell, focus
1974 * is painted around the entire area... in this case we
1975 * let focus leave the area directly.
1977 if (focus_cell && !gtk_cell_area_is_activatable (area))
1979 gtk_cell_area_set_focus_cell (area, NULL);
1985 case GTK_DIR_TAB_FORWARD:
1986 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
1988 case GTK_DIR_TAB_BACKWARD:
1989 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
1992 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
1996 if (priv->orientation == GTK_ORIENTATION_VERTICAL || !focus_cell)
2000 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
2001 cycle = priv->rtl ? FOCUS_NEXT : FOCUS_PREV;
2004 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL || !focus_cell)
2005 cycle = priv->rtl ? FOCUS_PREV : FOCUS_NEXT;
2011 if (cycle != FOCUS_NONE)
2013 gboolean found_cell = FALSE;
2017 /* If there is no focused cell, focus on the first (or last) one in the list */
2021 for (i = (cycle == FOCUS_NEXT) ? 0 : priv->groups->len -1;
2022 cycled_focus == FALSE && i >= 0 && i < priv->groups->len;
2023 i = (cycle == FOCUS_NEXT) ? i + 1 : i - 1)
2025 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
2027 for (list = (cycle == FOCUS_NEXT) ? g_list_first (group->cells) : g_list_last (group->cells);
2028 cycled_focus == FALSE && list; list = (cycle == FOCUS_NEXT) ? list->next : list->prev)
2030 CellInfo *info = list->data;
2032 if (info->renderer == focus_cell)
2034 else if (found_cell && /* Dont give focus to cells that are siblings to a focus cell */
2035 gtk_cell_area_get_focus_from_sibling (area, info->renderer) == NULL)
2037 gtk_cell_area_set_focus_cell (area, info->renderer);
2038 cycled_focus = TRUE;
2045 gtk_cell_area_set_focus_cell (area, NULL);
2047 return cycled_focus;
2051 /*************************************************************
2052 * GtkCellLayoutIface *
2053 *************************************************************/
2055 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
2057 iface->pack_start = gtk_cell_area_box_layout_pack_start;
2058 iface->pack_end = gtk_cell_area_box_layout_pack_end;
2059 iface->reorder = gtk_cell_area_box_layout_reorder;
2063 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
2064 GtkCellRenderer *renderer,
2067 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
2071 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
2072 GtkCellRenderer *renderer,
2075 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
2079 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
2080 GtkCellRenderer *renderer,
2083 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
2084 GtkCellAreaBoxPrivate *priv = box->priv;
2088 node = g_list_find_custom (priv->cells, renderer,
2089 (GCompareFunc)cell_info_find);
2095 priv->cells = g_list_delete_link (priv->cells, node);
2096 priv->cells = g_list_insert (priv->cells, info, position);
2098 cell_groups_rebuild (box);
2102 /*************************************************************
2104 *************************************************************/
2106 * gtk_cell_area_box_new:
2108 * Creates a new #GtkCellAreaBox.
2110 * Return value: a newly created #GtkCellAreaBox
2113 gtk_cell_area_box_new (void)
2115 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
2119 * gtk_cell_area_box_pack_start:
2120 * @box: a #GtkCellAreaBox
2121 * @renderer: the #GtkCellRenderer to add
2122 * @expand: whether @renderer should receive extra space when the area receives
2123 * more than its natural size
2124 * @align: whether @renderer should be aligned in adjacent rows.
2126 * Adds @renderer to @box, packed with reference to the start of @box.
2128 * The @renderer is packed after any other #GtkCellRenderer packed with reference
2129 * to the start of @box.
2134 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
2135 GtkCellRenderer *renderer,
2139 GtkCellAreaBoxPrivate *priv;
2142 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2143 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2147 if (g_list_find_custom (priv->cells, renderer,
2148 (GCompareFunc)cell_info_find))
2150 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
2154 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
2156 priv->cells = g_list_append (priv->cells, info);
2158 cell_groups_rebuild (box);
2162 * gtk_cell_area_box_pack_end:
2163 * @box: a #GtkCellAreaBox
2164 * @renderer: the #GtkCellRenderer to add
2165 * @expand: whether @renderer should receive extra space when the area receives
2166 * more than its natural size
2167 * @align: whether @renderer should be aligned in adjacent rows.
2169 * Adds @renderer to @box, packed with reference to the end of @box.
2171 * The @renderer is packed after (away from end of) any other #GtkCellRenderer
2172 * packed with reference to the end of @box.
2177 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
2178 GtkCellRenderer *renderer,
2182 GtkCellAreaBoxPrivate *priv;
2185 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2186 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2190 if (g_list_find_custom (priv->cells, renderer,
2191 (GCompareFunc)cell_info_find))
2193 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
2197 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
2199 priv->cells = g_list_append (priv->cells, info);
2201 cell_groups_rebuild (box);
2205 * gtk_cell_area_box_get_spacing:
2206 * @box: a #GtkCellAreaBox
2208 * Gets the spacing added between cell renderers.
2210 * Return value: the space added between cell renderers in @box.
2215 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
2217 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
2219 return box->priv->spacing;
2223 * gtk_cell_area_box_set_spacing:
2224 * @box: a #GtkCellAreaBox
2225 * @spacing: the space to add between #GtkCellRenderers
2227 * Sets the spacing to add between cell renderers in @box.
2232 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
2235 GtkCellAreaBoxPrivate *priv;
2237 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
2241 if (priv->spacing != spacing)
2243 priv->spacing = spacing;
2245 g_object_notify (G_OBJECT (box), "spacing");
2247 /* Notify that size needs to be requested again */
2248 reset_contexts (box);