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 #include "gtkorientable.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellareabox.h"
29 #include "gtkcellareaboxiter.h"
30 #include "gtkprivate.h"
34 static void gtk_cell_area_box_finalize (GObject *object);
35 static void gtk_cell_area_box_dispose (GObject *object);
36 static void gtk_cell_area_box_set_property (GObject *object,
40 static void gtk_cell_area_box_get_property (GObject *object,
45 /* GtkCellAreaClass */
46 static void gtk_cell_area_box_add (GtkCellArea *area,
47 GtkCellRenderer *renderer);
48 static void gtk_cell_area_box_remove (GtkCellArea *area,
49 GtkCellRenderer *renderer);
50 static void gtk_cell_area_box_forall (GtkCellArea *area,
51 GtkCellCallback callback,
52 gpointer callback_data);
53 static gint gtk_cell_area_box_event (GtkCellArea *area,
54 GtkCellAreaIter *iter,
57 const GdkRectangle *cell_area,
58 GtkCellRendererState flags);
59 static void gtk_cell_area_box_render (GtkCellArea *area,
60 GtkCellAreaIter *iter,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
65 static void gtk_cell_area_box_set_cell_property (GtkCellArea *area,
66 GtkCellRenderer *renderer,
70 static void gtk_cell_area_box_get_cell_property (GtkCellArea *area,
71 GtkCellRenderer *renderer,
75 static GtkCellAreaIter *gtk_cell_area_box_create_iter (GtkCellArea *area);
76 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
77 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
78 GtkCellAreaIter *iter,
82 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
83 GtkCellAreaIter *iter,
86 gint *natural_height);
87 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
88 GtkCellAreaIter *iter,
92 gint *natural_height);
93 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
94 GtkCellAreaIter *iter,
99 static void gtk_cell_area_box_grab_focus (GtkCellArea *area,
100 GtkDirectionType direction);
102 /* GtkCellLayoutIface */
103 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
104 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
105 GtkCellRenderer *renderer,
107 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
108 GtkCellRenderer *renderer,
110 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
111 GtkCellRenderer *renderer,
115 /* CellInfo/CellGroup metadata handling and convenience functions */
117 GtkCellRenderer *renderer;
119 guint expand : 1; /* Whether the cell expands */
120 guint pack : 1; /* Whether the cell is packed from the start or end */
121 guint align : 1; /* Whether to align this cell's position with adjacent rows */
129 guint expand_cells : 8;
133 GtkCellRenderer *renderer;
139 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
143 static void cell_info_free (CellInfo *info);
144 static gint cell_info_find (CellInfo *info,
145 GtkCellRenderer *renderer);
147 static AllocatedCell *allocated_cell_new (GtkCellRenderer *renderer,
150 static void allocated_cell_free (AllocatedCell *cell);
151 static GList *list_consecutive_cells (GtkCellAreaBox *box);
152 static gint count_expand_groups (GtkCellAreaBox *box);
153 static void iter_weak_notify (GtkCellAreaBox *box,
154 GtkCellAreaBoxIter *dead_iter);
155 static void flush_iters (GtkCellAreaBox *box);
156 static void init_iter_groups (GtkCellAreaBox *box);
157 static void init_iter_group (GtkCellAreaBox *box,
158 GtkCellAreaBoxIter *iter);
159 static GSList *get_allocated_cells (GtkCellAreaBox *box,
160 GtkCellAreaBoxIter *iter,
164 struct _GtkCellAreaBoxPrivate
166 GtkOrientation orientation;
189 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
190 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
191 gtk_cell_area_box_cell_layout_init)
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
194 #define OPPOSITE_ORIENTATION(orientation) \
195 ((orientation) == GTK_ORIENTATION_HORIZONTAL ? \
196 GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL)
199 gtk_cell_area_box_init (GtkCellAreaBox *box)
201 GtkCellAreaBoxPrivate *priv;
203 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
204 GTK_TYPE_CELL_AREA_BOX,
205 GtkCellAreaBoxPrivate);
208 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
209 priv->groups = g_array_new (FALSE, TRUE, sizeof (CellGroup));
216 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
218 GObjectClass *object_class = G_OBJECT_CLASS (class);
219 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
222 object_class->finalize = gtk_cell_area_box_finalize;
223 object_class->dispose = gtk_cell_area_box_dispose;
224 object_class->set_property = gtk_cell_area_box_set_property;
225 object_class->get_property = gtk_cell_area_box_get_property;
227 /* GtkCellAreaClass */
228 area_class->add = gtk_cell_area_box_add;
229 area_class->remove = gtk_cell_area_box_remove;
230 area_class->forall = gtk_cell_area_box_forall;
231 area_class->event = gtk_cell_area_box_event;
232 area_class->render = gtk_cell_area_box_render;
233 area_class->set_cell_property = gtk_cell_area_box_set_cell_property;
234 area_class->get_cell_property = gtk_cell_area_box_get_cell_property;
236 area_class->create_iter = gtk_cell_area_box_create_iter;
237 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
238 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
239 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
240 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
241 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
243 area_class->grab_focus = gtk_cell_area_box_grab_focus;
246 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
248 g_object_class_install_property (object_class,
250 g_param_spec_int ("spacing",
252 P_("Space which is inserted between cells"),
256 GTK_PARAM_READWRITE));
258 /* Cell Properties */
259 gtk_cell_area_class_install_cell_property (area_class,
264 P_("Whether the cell expands"),
266 GTK_PARAM_READWRITE));
268 gtk_cell_area_class_install_cell_property (area_class,
273 P_("Whether cell should align with adjacent rows"),
275 GTK_PARAM_READWRITE));
277 gtk_cell_area_class_install_cell_property (area_class,
282 P_("A GtkPackType indicating whether the cell is packed with "
283 "reference to the start or end of the cell area"),
284 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
285 GTK_PARAM_READWRITE));
287 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
291 /*************************************************************
292 * CellInfo/CellGroup basics and convenience functions *
293 *************************************************************/
295 cell_info_new (GtkCellRenderer *renderer,
300 CellInfo *info = g_slice_new (CellInfo);
302 info->renderer = g_object_ref_sink (renderer);
304 info->expand = expand;
311 cell_info_free (CellInfo *info)
313 g_object_unref (info->renderer);
315 g_slice_free (CellInfo, info);
319 cell_info_find (CellInfo *info,
320 GtkCellRenderer *renderer)
322 return (info->renderer == renderer) ? 0 : -1;
325 static AllocatedCell *
326 allocated_cell_new (GtkCellRenderer *renderer,
330 AllocatedCell *cell = g_slice_new (AllocatedCell);
332 cell->renderer = renderer;
333 cell->position = position;
340 allocated_cell_free (AllocatedCell *cell)
342 g_slice_free (AllocatedCell, cell);
346 list_consecutive_cells (GtkCellAreaBox *box)
348 GtkCellAreaBoxPrivate *priv = box->priv;
349 GList *l, *consecutive_cells = NULL, *pack_end_cells = NULL;
352 /* List cells in consecutive order taking their
353 * PACK_START/PACK_END options into account
355 for (l = priv->cells; l; l = l->next)
359 if (info->pack == GTK_PACK_START)
360 consecutive_cells = g_list_prepend (consecutive_cells, info);
363 for (l = priv->cells; l; l = l->next)
367 if (info->pack == GTK_PACK_END)
368 pack_end_cells = g_list_prepend (pack_end_cells, info);
371 consecutive_cells = g_list_reverse (consecutive_cells);
372 consecutive_cells = g_list_concat (consecutive_cells, pack_end_cells);
374 return consecutive_cells;
378 cell_groups_clear (GtkCellAreaBox *box)
380 GtkCellAreaBoxPrivate *priv = box->priv;
383 for (i = 0; i < priv->groups->len; i++)
385 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
387 g_list_free (group->cells);
390 g_array_set_size (priv->groups, 0);
394 cell_groups_rebuild (GtkCellAreaBox *box)
396 GtkCellAreaBoxPrivate *priv = box->priv;
397 CellGroup group = { 0, };
401 cell_groups_clear (box);
406 cells = list_consecutive_cells (box);
408 /* First group is implied */
409 g_array_append_val (priv->groups, group);
411 for (l = cells; l; l = l->next)
413 CellInfo *info = l->data;
415 /* A new group starts with any aligned cell, the first group is implied */
416 if (info->align && l != cells)
418 memset (&group, 0x0, sizeof (CellGroup));
421 g_array_append_val (priv->groups, group);
424 group.cells = g_list_prepend (group.cells, info);
427 /* A group expands if it contains any expand cells */
429 group.expand_cells++;
434 for (id = 0; id < priv->groups->len; id++)
436 CellGroup *group_ptr = &g_array_index (priv->groups, CellGroup, id);
438 group_ptr->cells = g_list_reverse (group_ptr->cells);
441 /* Iters need to be updated with the new grouping information */
442 init_iter_groups (box);
446 count_visible_cells (CellGroup *group,
450 gint visible_cells = 0;
451 gint n_expand_cells = 0;
453 for (l = group->cells; l; l = l->next)
455 CellInfo *info = l->data;
457 if (gtk_cell_renderer_get_visible (info->renderer))
467 *expand_cells = n_expand_cells;
469 return visible_cells;
473 count_expand_groups (GtkCellAreaBox *box)
475 GtkCellAreaBoxPrivate *priv = box->priv;
477 gint expand_groups = 0;
479 for (i = 0; i < priv->groups->len; i++)
481 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
483 if (group->expand_cells > 0)
487 return expand_groups;
491 iter_weak_notify (GtkCellAreaBox *box,
492 GtkCellAreaBoxIter *dead_iter)
494 GtkCellAreaBoxPrivate *priv = box->priv;
496 priv->iters = g_slist_remove (priv->iters, dead_iter);
500 init_iter_group (GtkCellAreaBox *box,
501 GtkCellAreaBoxIter *iter)
503 GtkCellAreaBoxPrivate *priv = box->priv;
504 gint *expand_groups, i;
506 expand_groups = g_new (gboolean, priv->groups->len);
508 for (i = 0; i < priv->groups->len; i++)
510 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
512 expand_groups[i] = (group->expand_cells > 0);
515 /* This call implies flushing the request info */
516 gtk_cell_area_box_init_groups (iter, priv->groups->len, expand_groups);
517 g_free (expand_groups);
521 init_iter_groups (GtkCellAreaBox *box)
523 GtkCellAreaBoxPrivate *priv = box->priv;
526 /* When the box's groups are reconstructed, iters need to
529 for (l = priv->iters; l; l = l->next)
531 GtkCellAreaBoxIter *iter = l->data;
533 init_iter_group (box, iter);
538 flush_iters (GtkCellAreaBox *box)
540 GtkCellAreaBoxPrivate *priv = box->priv;
543 /* When the box layout changes, iters need to
544 * be flushed and sizes for the box get requested again
546 for (l = priv->iters; l; l = l->next)
548 GtkCellAreaIter *iter = l->data;
550 gtk_cell_area_iter_flush (iter);
554 /* Returns an allocation for each cell in the orientation of the box,
555 * used in ->render()/->event() implementations to get a straight-forward
556 * list of allocated cells to operate on.
559 get_allocated_cells (GtkCellAreaBox *box,
560 GtkCellAreaBoxIter *iter,
563 const GtkCellAreaBoxAllocation *group_allocs;
564 GtkCellArea *area = GTK_CELL_AREA (box);
565 GtkCellAreaBoxPrivate *priv = box->priv;
567 GSList *allocated_cells = NULL;
570 group_allocs = gtk_cell_area_box_iter_get_orientation_allocs (iter, &n_allocs);
573 g_warning ("Trying to operate on an unallocated GtkCellAreaIter, "
574 "GtkCellAreaBox requires that the iter be allocated at least "
575 "in the orientation of the box");
579 for (i = 0; i < n_allocs; i++)
581 /* We dont always allocate all groups, sometimes the requested group has only invisible
582 * cells for every row, hence the usage of group_allocs[i].group_idx here
584 CellGroup *group = &g_array_index (priv->groups, CellGroup, group_allocs[i].group_idx);
586 /* Exception for single cell groups */
587 if (group->n_cells == 1)
589 CellInfo *info = group->cells->data;
590 AllocatedCell *cell =
591 allocated_cell_new (info->renderer, group_allocs[i].position, group_allocs[i].size);
593 allocated_cells = g_slist_prepend (allocated_cells, cell);
597 GtkRequestedSize *sizes;
598 gint avail_size, position;
599 gint visible_cells, expand_cells;
600 gint extra_size, extra_extra;
602 visible_cells = count_visible_cells (group, &expand_cells);
604 /* If this row has no visible cells in this group, just
605 * skip the allocation */
606 if (visible_cells == 0)
609 /* Offset the allocation to the group position and allocate into
610 * the group's available size */
611 position = group_allocs[i].position;
612 avail_size = group_allocs[i].size;
614 sizes = g_new (GtkRequestedSize, visible_cells);
616 for (j = 0, cell_list = group->cells; cell_list; cell_list = cell_list->next)
618 CellInfo *info = cell_list->data;
620 if (!gtk_cell_renderer_get_visible (info->renderer))
623 gtk_cell_area_request_renderer (area, info->renderer,
626 &sizes[j].minimum_size,
627 &sizes[j].natural_size);
629 sizes[j].data = info;
630 avail_size -= sizes[j].minimum_size;
635 /* Distribute cells naturally within the group */
636 avail_size -= (visible_cells - 1) * priv->spacing;
637 avail_size = gtk_distribute_natural_allocation (avail_size, visible_cells, sizes);
639 /* Calculate/distribute expand for cells */
640 if (expand_cells > 0)
642 extra_size = avail_size / expand_cells;
643 extra_extra = avail_size % expand_cells;
646 extra_size = extra_extra = 0;
648 /* Create the allocated cells (loop only over visible cells here) */
649 for (j = 0; j < visible_cells; j++)
651 CellInfo *info = sizes[j].data;
656 sizes[j].minimum_size += extra_size;
659 sizes[j].minimum_size++;
664 cell = allocated_cell_new (info->renderer, position, sizes[j].minimum_size);
666 allocated_cells = g_slist_prepend (allocated_cells, cell);
668 position += sizes[j].minimum_size;
669 position += priv->spacing;
678 /* Note it might not be important to reverse the list here at all,
679 * we have the correct positions, no need to allocate from left to right */
680 return g_slist_reverse (allocated_cells);
683 /*************************************************************
685 *************************************************************/
687 gtk_cell_area_box_finalize (GObject *object)
689 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
690 GtkCellAreaBoxPrivate *priv = box->priv;
693 /* Unref/free the iter list */
694 for (l = priv->iters; l; l = l->next)
695 g_object_weak_unref (G_OBJECT (l->data), (GWeakNotify)iter_weak_notify, box);
697 g_slist_free (priv->iters);
700 /* Free the cell grouping info */
701 cell_groups_clear (box);
702 g_array_free (priv->groups, TRUE);
704 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
708 gtk_cell_area_box_dispose (GObject *object)
710 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
714 gtk_cell_area_box_set_property (GObject *object,
719 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
723 case PROP_ORIENTATION:
724 box->priv->orientation = g_value_get_enum (value);
726 /* Notify that size needs to be requested again */
730 gtk_cell_area_box_set_spacing (box, g_value_get_int (value));
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739 gtk_cell_area_box_get_property (GObject *object,
744 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (object);
748 case PROP_ORIENTATION:
749 g_value_set_enum (value, box->priv->orientation);
752 g_value_set_int (value, gtk_cell_area_box_get_spacing (box));
755 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
760 /*************************************************************
762 *************************************************************/
764 gtk_cell_area_box_add (GtkCellArea *area,
765 GtkCellRenderer *renderer)
767 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
768 renderer, FALSE, TRUE);
772 gtk_cell_area_box_remove (GtkCellArea *area,
773 GtkCellRenderer *renderer)
775 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
776 GtkCellAreaBoxPrivate *priv = box->priv;
779 node = g_list_find_custom (priv->cells, renderer,
780 (GCompareFunc)cell_info_find);
784 CellInfo *info = node->data;
786 cell_info_free (info);
788 priv->cells = g_list_delete_link (priv->cells, node);
790 /* Reconstruct cell groups */
791 cell_groups_rebuild (box);
794 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
798 gtk_cell_area_box_forall (GtkCellArea *area,
799 GtkCellCallback callback,
800 gpointer callback_data)
802 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
803 GtkCellAreaBoxPrivate *priv = box->priv;
806 for (list = priv->cells; list; list = list->next)
808 CellInfo *info = list->data;
810 callback (info->renderer, callback_data);
815 gtk_cell_area_box_event (GtkCellArea *area,
816 GtkCellAreaIter *iter,
819 const GdkRectangle *cell_area,
820 GtkCellRendererState flags)
828 gtk_cell_area_box_render (GtkCellArea *area,
829 GtkCellAreaIter *iter,
832 const GdkRectangle *cell_area,
833 GtkCellRendererState flags)
835 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
836 GtkCellAreaBoxPrivate *priv = box->priv;
837 GtkCellAreaBoxIter *box_iter = GTK_CELL_AREA_BOX_ITER (iter);
838 GSList *allocated_cells, *l;
839 GdkRectangle background_area, inner_area;
841 background_area = *cell_area;
843 /* Get a list of cells with allocation sizes decided regardless
844 * of alignments and pack order etc. */
845 allocated_cells = get_allocated_cells (box, box_iter, widget);
847 for (l = allocated_cells; l; l = l->next)
849 AllocatedCell *cell = l->data;
851 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
853 background_area.x = cell_area->x + cell->position;
854 background_area.width = cell->size;
858 background_area.y = cell_area->y + cell->position;
859 background_area.height = cell->size;
862 /* Remove margins from the background area to produce the cell area
864 gtk_cell_area_inner_cell_area (area, &background_area, &inner_area);
866 /* XXX We have to do some per-cell considerations for the 'flags'
867 * for focus handling */
868 gtk_cell_renderer_render (cell->renderer, cr, widget,
869 &background_area, &inner_area,
874 g_slist_foreach (allocated_cells, (GFunc)allocated_cell_free, NULL);
875 g_slist_free (allocated_cells);
879 gtk_cell_area_box_set_cell_property (GtkCellArea *area,
880 GtkCellRenderer *renderer,
885 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
886 GtkCellAreaBoxPrivate *priv = box->priv;
889 gboolean rebuild = FALSE;
890 gboolean flush = FALSE;
892 GtkPackType pack_type;
894 node = g_list_find_custom (priv->cells, renderer,
895 (GCompareFunc)cell_info_find);
903 case CELL_PROP_EXPAND:
904 val = g_value_get_boolean (value);
906 if (info->expand != val)
913 case CELL_PROP_ALIGN:
914 val = g_value_get_boolean (value);
916 if (info->align != val)
923 case CELL_PROP_PACK_TYPE:
924 pack_type = g_value_get_enum (value);
926 if (info->pack != pack_type)
928 info->pack = pack_type;
933 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
937 /* Groups need to be rebuilt */
940 cell_groups_rebuild (box);
949 gtk_cell_area_box_get_cell_property (GtkCellArea *area,
950 GtkCellRenderer *renderer,
955 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
956 GtkCellAreaBoxPrivate *priv = box->priv;
960 node = g_list_find_custom (priv->cells, renderer,
961 (GCompareFunc)cell_info_find);
969 case CELL_PROP_EXPAND:
970 g_value_set_boolean (value, info->expand);
973 case CELL_PROP_ALIGN:
974 g_value_set_boolean (value, info->align);
977 case CELL_PROP_PACK_TYPE:
978 g_value_set_enum (value, info->pack);
981 GTK_CELL_AREA_WARN_INVALID_CHILD_PROPERTY_ID (area, prop_id, pspec);
987 static GtkCellAreaIter *
988 gtk_cell_area_box_create_iter (GtkCellArea *area)
990 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
991 GtkCellAreaBoxPrivate *priv = box->priv;
992 GtkCellAreaIter *iter =
993 (GtkCellAreaIter *)g_object_new (GTK_TYPE_CELL_AREA_BOX_ITER, NULL);
995 priv->iters = g_slist_prepend (priv->iters, iter);
997 g_object_weak_ref (G_OBJECT (iter), (GWeakNotify)iter_weak_notify, box);
999 /* Tell the new group about our cell layout */
1000 init_iter_group (box, GTK_CELL_AREA_BOX_ITER (iter));
1005 static GtkSizeRequestMode
1006 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
1008 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1009 GtkCellAreaBoxPrivate *priv = box->priv;
1011 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
1012 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
1013 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
1017 compute_size (GtkCellAreaBox *box,
1018 GtkOrientation orientation,
1019 GtkCellAreaBoxIter *iter,
1025 GtkCellAreaBoxPrivate *priv = box->priv;
1026 GtkCellArea *area = GTK_CELL_AREA (box);
1032 for (i = 0; i < priv->groups->len; i++)
1034 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1035 gint group_min_size = 0;
1036 gint group_nat_size = 0;
1038 for (list = group->cells; list; list = list->next)
1040 CellInfo *info = list->data;
1041 gint renderer_min_size, renderer_nat_size;
1043 if (!gtk_cell_renderer_get_visible (info->renderer))
1046 gtk_cell_area_request_renderer (area, info->renderer, orientation, widget, for_size,
1047 &renderer_min_size, &renderer_nat_size);
1049 if (orientation == priv->orientation)
1053 min_size += priv->spacing;
1054 nat_size += priv->spacing;
1057 if (group_min_size > 0)
1059 group_min_size += priv->spacing;
1060 group_nat_size += priv->spacing;
1063 min_size += renderer_min_size;
1064 nat_size += renderer_nat_size;
1065 group_min_size += renderer_min_size;
1066 group_nat_size += renderer_nat_size;
1070 min_size = MAX (min_size, renderer_min_size);
1071 nat_size = MAX (nat_size, renderer_nat_size);
1072 group_min_size = MAX (group_min_size, renderer_min_size);
1073 group_nat_size = MAX (group_nat_size, renderer_nat_size);
1077 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1080 gtk_cell_area_box_iter_push_group_width (iter, group->id, group_min_size, group_nat_size);
1082 gtk_cell_area_box_iter_push_group_width_for_height (iter, group->id, for_size,
1083 group_min_size, group_nat_size);
1088 gtk_cell_area_box_iter_push_group_height (iter, group->id, group_min_size, group_nat_size);
1090 gtk_cell_area_box_iter_push_group_height_for_width (iter, group->id, for_size,
1091 group_min_size, group_nat_size);
1095 *minimum_size = min_size;
1096 *natural_size = nat_size;
1100 get_group_sizes (GtkCellArea *area,
1102 GtkOrientation orientation,
1106 GtkRequestedSize *sizes;
1110 *n_sizes = count_visible_cells (group, NULL);
1111 sizes = g_new (GtkRequestedSize, *n_sizes);
1113 for (l = group->cells, i = 0; l; l = l->next)
1115 CellInfo *info = l->data;
1117 if (!gtk_cell_renderer_get_visible (info->renderer))
1120 sizes[i].data = info;
1122 gtk_cell_area_request_renderer (area, info->renderer,
1123 orientation, widget, -1,
1124 &sizes[i].minimum_size,
1125 &sizes[i].natural_size);
1134 compute_group_size_for_opposing_orientation (GtkCellAreaBox *box,
1141 GtkCellAreaBoxPrivate *priv = box->priv;
1142 GtkCellArea *area = GTK_CELL_AREA (box);
1144 /* Exception for single cell groups */
1145 if (group->n_cells == 1)
1147 CellInfo *info = group->cells->data;
1149 gtk_cell_area_request_renderer (area, info->renderer,
1150 OPPOSITE_ORIENTATION (priv->orientation),
1151 widget, for_size, minimum_size, natural_size);
1155 GtkRequestedSize *orientation_sizes;
1158 gint avail_size = for_size;
1159 gint extra_size, extra_extra;
1160 gint min_size = 0, nat_size = 0;
1162 orientation_sizes = get_group_sizes (area, group, priv->orientation, widget, &n_sizes);
1164 /* First naturally allocate the cells in the group into the for_size */
1165 avail_size -= (n_sizes - 1) * priv->spacing;
1166 for (i = 0; i < n_sizes; i++)
1167 avail_size -= orientation_sizes[i].minimum_size;
1169 avail_size = gtk_distribute_natural_allocation (avail_size, n_sizes, orientation_sizes);
1171 /* Calculate/distribute expand for cells */
1172 if (group->expand_cells > 0)
1174 extra_size = avail_size / group->expand_cells;
1175 extra_extra = avail_size % group->expand_cells;
1178 extra_size = extra_extra = 0;
1180 for (i = 0; i < n_sizes; i++)
1182 gint cell_min, cell_nat;
1184 info = orientation_sizes[i].data;
1188 orientation_sizes[i].minimum_size += extra_size;
1191 orientation_sizes[i].minimum_size++;
1196 gtk_cell_area_request_renderer (area, info->renderer,
1197 OPPOSITE_ORIENTATION (priv->orientation),
1199 orientation_sizes[i].minimum_size,
1200 &cell_min, &cell_nat);
1202 min_size = MAX (min_size, cell_min);
1203 nat_size = MAX (nat_size, cell_nat);
1206 *minimum_size = min_size;
1207 *natural_size = nat_size;
1209 g_free (orientation_sizes);
1214 compute_size_for_opposing_orientation (GtkCellAreaBox *box,
1215 GtkCellAreaBoxIter *iter,
1221 GtkCellAreaBoxPrivate *priv = box->priv;
1223 GtkRequestedSize *orientation_sizes;
1224 gint n_groups, n_expand_groups, i;
1225 gint avail_size = for_size;
1226 gint extra_size, extra_extra;
1227 gint min_size = 0, nat_size = 0;
1229 n_expand_groups = count_expand_groups (box);
1231 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1232 orientation_sizes = gtk_cell_area_box_iter_get_widths (iter, &n_groups);
1234 orientation_sizes = gtk_cell_area_box_iter_get_heights (iter, &n_groups);
1236 /* First start by naturally allocating space among groups of cells */
1237 avail_size -= (n_groups - 1) * priv->spacing;
1238 for (i = 0; i < n_groups; i++)
1239 avail_size -= orientation_sizes[i].minimum_size;
1241 avail_size = gtk_distribute_natural_allocation (avail_size, n_groups, orientation_sizes);
1243 /* Calculate/distribute expand for groups */
1244 if (n_expand_groups > 0)
1246 extra_size = avail_size / n_expand_groups;
1247 extra_extra = avail_size % n_expand_groups;
1250 extra_size = extra_extra = 0;
1252 /* Now we need to naturally allocate sizes for cells in each group
1253 * and push the height-for-width for each group accordingly while accumulating
1254 * the overall height-for-width for this row.
1256 for (i = 0; i < n_groups; i++)
1258 gint group_min, group_nat;
1259 gint group_idx = GPOINTER_TO_INT (orientation_sizes[i].data);
1261 group = &g_array_index (priv->groups, CellGroup, group_idx);
1263 if (group->expand_cells > 0)
1265 orientation_sizes[i].minimum_size += extra_size;
1268 orientation_sizes[i].minimum_size++;
1273 /* Now we have the allocation for the group, request it's height-for-width */
1274 compute_group_size_for_opposing_orientation (box, group, widget,
1275 orientation_sizes[i].minimum_size,
1276 &group_min, &group_nat);
1278 min_size = MAX (min_size, group_min);
1279 nat_size = MAX (nat_size, group_nat);
1281 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1283 gtk_cell_area_box_iter_push_group_height_for_width (iter, group_idx, for_size,
1284 group_min, group_nat);
1288 gtk_cell_area_box_iter_push_group_width_for_height (iter, group_idx, for_size,
1289 group_min, group_nat);
1293 *minimum_size = min_size;
1294 *natural_size = nat_size;
1296 g_free (orientation_sizes);
1302 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
1303 GtkCellAreaIter *iter,
1305 gint *minimum_width,
1306 gint *natural_width)
1308 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1309 GtkCellAreaBoxIter *box_iter;
1310 gint min_width, nat_width;
1312 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1314 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1316 /* Compute the size of all renderers for current row data,
1317 * bumping cell alignments in the iter along the way */
1318 compute_size (box, GTK_ORIENTATION_HORIZONTAL,
1319 box_iter, widget, -1, &min_width, &nat_width);
1322 *minimum_width = min_width;
1325 *natural_width = nat_width;
1329 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
1330 GtkCellAreaIter *iter,
1332 gint *minimum_height,
1333 gint *natural_height)
1335 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1336 GtkCellAreaBoxIter *box_iter;
1337 gint min_height, nat_height;
1339 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1341 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1343 /* Compute the size of all renderers for current row data,
1344 * bumping cell alignments in the iter along the way */
1345 compute_size (box, GTK_ORIENTATION_VERTICAL,
1346 box_iter, widget, -1, &min_height, &nat_height);
1349 *minimum_height = min_height;
1352 *natural_height = nat_height;
1356 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
1357 GtkCellAreaIter *iter,
1360 gint *minimum_height,
1361 gint *natural_height)
1363 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1364 GtkCellAreaBoxIter *box_iter;
1365 GtkCellAreaBoxPrivate *priv;
1366 gint min_height, nat_height;
1368 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1370 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1373 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1375 /* Add up vertical requests of height for width and push the overall
1376 * cached sizes for alignments */
1377 compute_size (box, priv->orientation, box_iter, widget, width, &min_height, &nat_height);
1381 /* Juice: virtually allocate cells into the for_width using the
1382 * alignments and then return the overall height for that width, and cache it */
1383 compute_size_for_opposing_orientation (box, box_iter, widget, width, &min_height, &nat_height);
1387 *minimum_height = min_height;
1390 *natural_height = nat_height;
1394 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
1395 GtkCellAreaIter *iter,
1398 gint *minimum_width,
1399 gint *natural_width)
1401 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1402 GtkCellAreaBoxIter *box_iter;
1403 GtkCellAreaBoxPrivate *priv;
1404 gint min_width, nat_width;
1406 g_return_if_fail (GTK_IS_CELL_AREA_BOX_ITER (iter));
1408 box_iter = GTK_CELL_AREA_BOX_ITER (iter);
1411 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1413 /* Add up horizontal requests of width for height and push the overall
1414 * cached sizes for alignments */
1415 compute_size (box, priv->orientation, box_iter, widget, height, &min_width, &nat_width);
1419 /* Juice: horizontally allocate cells into the for_height using the
1420 * alignments and then return the overall width for that height, and cache it */
1421 compute_size_for_opposing_orientation (box, box_iter, widget, height, &min_width, &nat_width);
1425 *minimum_width = min_width;
1428 *natural_width = nat_width;
1432 gtk_cell_area_box_grab_focus (GtkCellArea *area,
1433 GtkDirectionType direction)
1435 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
1436 GtkCellAreaBoxPrivate *priv;
1437 gboolean first_cell = FALSE;
1445 case GTK_DIR_TAB_FORWARD:
1451 case GTK_DIR_TAB_BACKWARD:
1459 for (i = first_cell ? 0 : priv->groups->len -1;
1460 i >= 0 && i < priv->groups->len;
1461 i = first_cell ? i + 1 : i - 1)
1463 CellGroup *group = &g_array_index (priv->groups, CellGroup, i);
1465 for (list = first_cell ? g_list_first (group->cells) : g_list_last (group->cells);
1466 list; list = first_cell ? list->next : list->prev)
1468 CellInfo *info = list->data;
1470 if (gtk_cell_renderer_can_focus (info->renderer))
1472 gtk_cell_area_set_focus_cell (area, info->renderer);
1480 /*************************************************************
1481 * GtkCellLayoutIface *
1482 *************************************************************/
1484 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
1486 iface->pack_start = gtk_cell_area_box_layout_pack_start;
1487 iface->pack_end = gtk_cell_area_box_layout_pack_end;
1488 iface->reorder = gtk_cell_area_box_layout_reorder;
1492 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
1493 GtkCellRenderer *renderer,
1496 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1500 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
1501 GtkCellRenderer *renderer,
1504 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand, TRUE);
1508 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
1509 GtkCellRenderer *renderer,
1512 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
1513 GtkCellAreaBoxPrivate *priv = box->priv;
1517 node = g_list_find_custom (priv->cells, renderer,
1518 (GCompareFunc)cell_info_find);
1524 priv->cells = g_list_delete_link (priv->cells, node);
1525 priv->cells = g_list_insert (priv->cells, info, position);
1527 cell_groups_rebuild (box);
1531 /*************************************************************
1533 *************************************************************/
1535 gtk_cell_area_box_new (void)
1537 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
1541 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
1542 GtkCellRenderer *renderer,
1546 GtkCellAreaBoxPrivate *priv;
1549 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1550 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1554 if (g_list_find_custom (priv->cells, renderer,
1555 (GCompareFunc)cell_info_find))
1557 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
1561 info = cell_info_new (renderer, GTK_PACK_START, expand, align);
1563 priv->cells = g_list_append (priv->cells, info);
1565 cell_groups_rebuild (box);
1569 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
1570 GtkCellRenderer *renderer,
1574 GtkCellAreaBoxPrivate *priv;
1577 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1578 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1582 if (g_list_find_custom (priv->cells, renderer,
1583 (GCompareFunc)cell_info_find))
1585 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
1589 info = cell_info_new (renderer, GTK_PACK_END, expand, align);
1591 priv->cells = g_list_append (priv->cells, info);
1593 cell_groups_rebuild (box);
1597 gtk_cell_area_box_get_spacing (GtkCellAreaBox *box)
1599 g_return_val_if_fail (GTK_IS_CELL_AREA_BOX (box), 0);
1601 return box->priv->spacing;
1605 gtk_cell_area_box_set_spacing (GtkCellAreaBox *box,
1608 GtkCellAreaBoxPrivate *priv;
1610 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
1614 if (priv->spacing != spacing)
1616 priv->spacing = spacing;
1618 g_object_notify (G_OBJECT (box), "spacing");
1620 /* Notify that size needs to be requested again */