2 * Copyright (C) 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:gtkcelllayout
22 * @Short_Description: An interface for packing cells
23 * @Title: GtkCellLayout
25 * #GtkCellLayout is an interface to be implemented by all objects which
26 * want to provide a #GtkTreeViewColumn-like API for packing cells, setting
27 * attributes and data funcs.
29 * One of the notable features provided by implementations of GtkCellLayout
30 * are <emphasis>attributes</emphasis>. Attributes let you set the properties
31 * in flexible ways. They can just be set to constant values like regular
32 * properties. But they can also be mapped to a column of the underlying
33 * tree model with gtk_cell_layout_set_attributes(), which means that the value
34 * of the attribute can change from cell to cell as they are rendered by the
35 * cell renderer. Finally, it is possible to specify a function with
36 * gtk_cell_layout_set_cell_data_func() that is called to determine the value
37 * of the attribute for each cell that is rendered.
39 * <refsect2 id="GtkCellLayout-BUILDER-UI">
40 * <title>GtkCellLayouts as GtkBuildable</title>
42 * Implementations of GtkCellLayout which also implement the GtkBuildable
43 * interface (#GtkCellView, #GtkIconView, #GtkComboBox, #GtkComboBoxEntry,
44 * #GtkEntryCompletion, #GtkTreeViewColumn) accept GtkCellRenderer objects
45 * as <child> elements in UI definitions. They support a custom
46 * <attributes> element for their children, which can contain
47 * multiple <attribute> elements. Each <attribute> element has
48 * a name attribute which specifies a property of the cell renderer; the
49 * content of the element is the attribute value.
52 * <title>A UI definition fragment specifying attributes</title>
53 * <programlisting><![CDATA[
54 * <object class="GtkCellView">
56 * <object class="GtkCellRendererText"/>
58 * <attribute name="text">0</attribute>
62 * ]]></programlisting>
65 * Furthermore for implementations of GtkCellLayout that use a #GtkCellArea
66 * to lay out cells (all GtkCellLayouts in GTK+ use a GtkCellArea)
67 * <link linkend="cell-properties">cell properties</link> can also be defined
68 * in the format by specifying the custom <cell-packing> attribute which
69 * can contain multiple <property> elements defined in the normal way.
71 * <title>A UI definition fragment specifying cell properties</title>
72 * <programlisting><![CDATA[
73 * <object class="GtkTreeViewColumn">
75 * <object class="GtkCellRendererText"/>
77 * <property name="align">True</property>
78 * <property name="expand">False</property>
82 * ]]></programlisting>
92 #include "gtkcelllayout.h"
93 #include "gtkbuilderprivate.h"
97 typedef GtkCellLayoutIface GtkCellLayoutInterface;
98 G_DEFINE_INTERFACE (GtkCellLayout, gtk_cell_layout, G_TYPE_OBJECT);
100 static void gtk_cell_layout_default_pack_start (GtkCellLayout *cell_layout,
101 GtkCellRenderer *cell,
103 static void gtk_cell_layout_default_pack_end (GtkCellLayout *cell_layout,
104 GtkCellRenderer *cell,
106 static void gtk_cell_layout_default_clear (GtkCellLayout *cell_layout);
107 static void gtk_cell_layout_default_add_attribute (GtkCellLayout *cell_layout,
108 GtkCellRenderer *cell,
109 const gchar *attribute,
111 static void gtk_cell_layout_default_set_cell_data_func (GtkCellLayout *cell_layout,
112 GtkCellRenderer *cell,
113 GtkCellLayoutDataFunc func,
115 GDestroyNotify destroy);
116 static void gtk_cell_layout_default_clear_attributes (GtkCellLayout *cell_layout,
117 GtkCellRenderer *cell);
118 static void gtk_cell_layout_default_reorder (GtkCellLayout *cell_layout,
119 GtkCellRenderer *cell,
121 static GList *gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout);
125 gtk_cell_layout_default_init (GtkCellLayoutIface *iface)
127 iface->pack_start = gtk_cell_layout_default_pack_start;
128 iface->pack_end = gtk_cell_layout_default_pack_end;
129 iface->clear = gtk_cell_layout_default_clear;
130 iface->add_attribute = gtk_cell_layout_default_add_attribute;
131 iface->set_cell_data_func = gtk_cell_layout_default_set_cell_data_func;
132 iface->clear_attributes = gtk_cell_layout_default_clear_attributes;
133 iface->reorder = gtk_cell_layout_default_reorder;
134 iface->get_cells = gtk_cell_layout_default_get_cells;
138 /* Default implementation is to fall back on an underlying cell area */
140 gtk_cell_layout_default_pack_start (GtkCellLayout *cell_layout,
141 GtkCellRenderer *cell,
144 GtkCellLayoutIface *iface;
147 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
151 area = iface->get_area (cell_layout);
153 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
158 gtk_cell_layout_default_pack_end (GtkCellLayout *cell_layout,
159 GtkCellRenderer *cell,
162 GtkCellLayoutIface *iface;
165 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
169 area = iface->get_area (cell_layout);
171 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
176 gtk_cell_layout_default_clear (GtkCellLayout *cell_layout)
178 GtkCellLayoutIface *iface;
181 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
185 area = iface->get_area (cell_layout);
187 gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
192 gtk_cell_layout_default_add_attribute (GtkCellLayout *cell_layout,
193 GtkCellRenderer *cell,
194 const gchar *attribute,
197 GtkCellLayoutIface *iface;
200 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
204 area = iface->get_area (cell_layout);
206 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
211 gtk_cell_layout_default_set_cell_data_func (GtkCellLayout *cell_layout,
212 GtkCellRenderer *cell,
213 GtkCellLayoutDataFunc func,
215 GDestroyNotify destroy)
217 GtkCellLayoutIface *iface;
220 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
224 area = iface->get_area (cell_layout);
226 _gtk_cell_area_set_cell_data_func_with_proxy (area, cell,
227 (GFunc)func, func_data, destroy,
233 gtk_cell_layout_default_clear_attributes (GtkCellLayout *cell_layout,
234 GtkCellRenderer *cell)
236 GtkCellLayoutIface *iface;
239 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
243 area = iface->get_area (cell_layout);
245 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
250 gtk_cell_layout_default_reorder (GtkCellLayout *cell_layout,
251 GtkCellRenderer *cell,
254 GtkCellLayoutIface *iface;
257 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
261 area = iface->get_area (cell_layout);
263 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
268 gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout)
270 GtkCellLayoutIface *iface;
273 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
277 area = iface->get_area (cell_layout);
279 return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
286 * gtk_cell_layout_pack_start:
287 * @cell_layout: a #GtkCellLayout
288 * @cell: a #GtkCellRenderer
289 * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
291 * Packs the @cell into the beginning of @cell_layout. If @expand is %FALSE,
292 * then the @cell is allocated no more space than it needs. Any unused space
293 * is divided evenly between cells for which @expand is %TRUE.
295 * Note that reusing the same cell renderer is not supported.
300 gtk_cell_layout_pack_start (GtkCellLayout *cell_layout,
301 GtkCellRenderer *cell,
304 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
305 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
307 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_start (cell_layout, cell, expand);
311 * gtk_cell_layout_pack_end:
312 * @cell_layout: a #GtkCellLayout
313 * @cell: a #GtkCellRenderer
314 * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
316 * Adds the @cell to the end of @cell_layout. If @expand is %FALSE, then the
317 * @cell is allocated no more space than it needs. Any unused space is
318 * divided evenly between cells for which @expand is %TRUE.
320 * Note that reusing the same cell renderer is not supported.
325 gtk_cell_layout_pack_end (GtkCellLayout *cell_layout,
326 GtkCellRenderer *cell,
329 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
330 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
332 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_end (cell_layout, cell, expand);
336 * gtk_cell_layout_clear:
337 * @cell_layout: a #GtkCellLayout
339 * Unsets all the mappings on all renderers on @cell_layout and
340 * removes all renderers from @cell_layout.
345 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
347 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
349 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear (cell_layout);
353 gtk_cell_layout_set_attributesv (GtkCellLayout *cell_layout,
354 GtkCellRenderer *cell,
360 attribute = va_arg (args, gchar *);
362 gtk_cell_layout_clear_attributes (cell_layout, cell);
364 while (attribute != NULL)
366 column = va_arg (args, gint);
368 gtk_cell_layout_add_attribute (cell_layout, cell, attribute, column);
370 attribute = va_arg (args, gchar *);
375 * gtk_cell_layout_set_attributes:
376 * @cell_layout: a #GtkCellLayout
377 * @cell: a #GtkCellRenderer
378 * @Varargs: a %NULL-terminated list of attributes
380 * Sets the attributes in list as the attributes of @cell_layout.
382 * The attributes should be in attribute/column order, as in
383 * gtk_cell_layout_add_attribute(). All existing attributes are
384 * removed, and replaced with the new attributes.
389 gtk_cell_layout_set_attributes (GtkCellLayout *cell_layout,
390 GtkCellRenderer *cell,
395 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
396 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
398 va_start (args, cell);
399 gtk_cell_layout_set_attributesv (cell_layout, cell, args);
404 * gtk_cell_layout_add_attribute:
405 * @cell_layout: a #GtkCellLayout
406 * @cell: a #GtkCellRenderer
407 * @attribute: an attribute on the renderer
408 * @column: the column position on the model to get the attribute from
410 * Adds an attribute mapping to the list in @cell_layout.
412 * The @column is the column of the model to get a value from, and the
413 * @attribute is the parameter on @cell to be set from the value. So for
414 * example if column 2 of the model contains strings, you could have the
415 * "text" attribute of a #GtkCellRendererText get its values from column 2.
420 gtk_cell_layout_add_attribute (GtkCellLayout *cell_layout,
421 GtkCellRenderer *cell,
422 const gchar *attribute,
425 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
426 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
427 g_return_if_fail (attribute != NULL);
428 g_return_if_fail (column >= 0);
430 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->add_attribute (cell_layout, cell, attribute, column);
434 * gtk_cell_layout_set_cell_data_func:
435 * @cell_layout: a #GtkCellLayout
436 * @cell: a #GtkCellRenderer
437 * @func: (allow-none): the #GtkCellLayoutDataFunc to use, or %NULL
438 * @func_data: user data for @func
439 * @destroy: destroy notify for @func_data
441 * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
443 * This function is used instead of the standard attributes mapping
444 * for setting the column value, and should set the value of @cell_layout's
445 * cell renderer(s) as appropriate.
447 * @func may be %NULL to remove a previously set function.
452 gtk_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
453 GtkCellRenderer *cell,
454 GtkCellLayoutDataFunc func,
456 GDestroyNotify destroy)
458 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
459 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
461 GTK_CELL_LAYOUT_GET_IFACE
462 (cell_layout)->set_cell_data_func (cell_layout, cell, func, func_data, destroy);
466 * gtk_cell_layout_clear_attributes:
467 * @cell_layout: a #GtkCellLayout
468 * @cell: a #GtkCellRenderer to clear the attribute mapping on
470 * Clears all existing attributes previously set with
471 * gtk_cell_layout_set_attributes().
476 gtk_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
477 GtkCellRenderer *cell)
479 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
480 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
482 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear_attributes (cell_layout, cell);
486 * gtk_cell_layout_reorder:
487 * @cell_layout: a #GtkCellLayout
488 * @cell: a #GtkCellRenderer to reorder
489 * @position: new position to insert @cell at
491 * Re-inserts @cell at @position.
493 * Note that @cell has already to be packed into @cell_layout
494 * for this to function properly.
499 gtk_cell_layout_reorder (GtkCellLayout *cell_layout,
500 GtkCellRenderer *cell,
503 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
504 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
506 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->reorder (cell_layout, cell, position);
510 * gtk_cell_layout_get_cells:
511 * @cell_layout: a #GtkCellLayout
513 * Returns the cell renderers which have been added to @cell_layout.
515 * Return value: (element-type GtkCellRenderer) (transfer container):
516 * a list of cell renderers. The list, but not the renderers has
517 * been newly allocated and should be freed with g_list_free()
518 * when no longer needed.
523 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
525 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
527 return GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->get_cells (cell_layout);
531 * gtk_cell_layout_get_area:
532 * @cell_layout: a #GtkCellLayout
534 * Returns the underlying #GtkCellArea which might be @cell_layout
535 * if called on a #GtkCellArea or might be %NULL if no #GtkCellArea
536 * is used by @cell_layout.
538 * Return value: (transfer none): the cell area used by @cell_layout.
543 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
545 GtkCellLayoutIface *iface;
547 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
549 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
551 return iface->get_area (cell_layout);
556 /* Attribute parsing */
558 GtkCellLayout *cell_layout;
559 GtkCellRenderer *renderer;
561 } AttributesSubParserData;
564 attributes_start_element (GMarkupParseContext *context,
565 const gchar *element_name,
567 const gchar **values,
571 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
574 if (strcmp (element_name, "attribute") == 0)
576 for (i = 0; names[i]; i++)
577 if (strcmp (names[i], "name") == 0)
578 parser_data->attr_name = g_strdup (values[i]);
580 else if (strcmp (element_name, "attributes") == 0)
583 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
587 attributes_text_element (GMarkupParseContext *context,
593 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
598 if (!parser_data->attr_name)
601 string = g_strndup (text, text_len);
603 l = g_ascii_strtoll (string, &endptr, 0);
604 if (errno || endptr == string)
608 GTK_BUILDER_ERROR_INVALID_VALUE,
609 "Could not parse integer `%s'",
616 gtk_cell_layout_add_attribute (parser_data->cell_layout,
617 parser_data->renderer,
618 parser_data->attr_name, l);
619 g_free (parser_data->attr_name);
620 parser_data->attr_name = NULL;
623 static const GMarkupParser attributes_parser =
625 attributes_start_element,
627 attributes_text_element,
631 /* Cell packing parsing */
633 gtk_cell_layout_buildable_set_cell_property (GtkCellArea *area,
635 GtkCellRenderer *cell,
640 GValue gvalue = { 0, };
641 GError *error = NULL;
643 pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
646 g_warning ("%s does not have a property called %s",
647 g_type_name (G_OBJECT_TYPE (area)), name);
651 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
653 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
654 g_type_name (G_OBJECT_TYPE (area)),
657 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
659 g_error_free (error);
663 gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
664 g_value_unset (&gvalue);
669 GtkCellLayout *cell_layout;
670 GtkCellRenderer *renderer;
671 gchar *cell_prop_name;
673 gboolean translatable;
674 } CellPackingSubParserData;
677 cell_packing_start_element (GMarkupParseContext *context,
678 const gchar *element_name,
680 const gchar **values,
684 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
687 if (strcmp (element_name, "property") == 0)
689 for (i = 0; names[i]; i++)
690 if (strcmp (names[i], "name") == 0)
691 parser_data->cell_prop_name = g_strdup (values[i]);
692 else if (strcmp (names[i], "translatable") == 0)
694 if (!_gtk_builder_boolean_from_string (values[i],
695 &parser_data->translatable,
699 else if (strcmp (names[i], "comments") == 0)
700 ; /* for translators */
701 else if (strcmp (names[i], "context") == 0)
702 parser_data->context = g_strdup (values[i]);
704 g_warning ("Unsupported attribute for GtkCellLayout Cell "
705 "property: %s\n", names[i]);
707 else if (strcmp (element_name, "cell-packing") == 0)
710 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
714 cell_packing_text_element (GMarkupParseContext *context,
720 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
724 if (!parser_data->cell_prop_name)
727 if (parser_data->translatable && text_len)
730 domain = gtk_builder_get_translation_domain (parser_data->builder);
732 value = _gtk_builder_parser_translate (domain,
733 parser_data->context,
738 value = g_strdup (text);
741 area = gtk_cell_layout_get_area (parser_data->cell_layout);
745 g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
746 g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
750 gtk_cell_layout_buildable_set_cell_property (area,
751 parser_data->builder,
752 parser_data->renderer,
753 parser_data->cell_prop_name,
756 g_free (parser_data->cell_prop_name);
757 g_free (parser_data->context);
759 parser_data->cell_prop_name = NULL;
760 parser_data->context = NULL;
761 parser_data->translatable = FALSE;
764 static const GMarkupParser cell_packing_parser =
766 cell_packing_start_element,
768 cell_packing_text_element,
772 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable *buildable,
775 const gchar *tagname,
776 GMarkupParser *parser,
779 AttributesSubParserData *attr_data;
780 CellPackingSubParserData *packing_data;
785 if (strcmp (tagname, "attributes") == 0)
787 attr_data = g_slice_new0 (AttributesSubParserData);
788 attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
789 attr_data->renderer = GTK_CELL_RENDERER (child);
790 attr_data->attr_name = NULL;
792 *parser = attributes_parser;
796 else if (strcmp (tagname, "cell-packing") == 0)
798 packing_data = g_slice_new0 (CellPackingSubParserData);
799 packing_data->builder = builder;
800 packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
801 packing_data->renderer = GTK_CELL_RENDERER (child);
803 *parser = cell_packing_parser;
804 *data = packing_data;
812 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
815 const gchar *tagname,
818 AttributesSubParserData *attr_data;
820 if (strcmp (tagname, "attributes") == 0)
822 attr_data = (AttributesSubParserData*)data;
823 g_assert (!attr_data->attr_name);
824 g_slice_free (AttributesSubParserData, attr_data);
827 else if (strcmp (tagname, "cell-packing") == 0)
829 g_slice_free (CellPackingSubParserData, (gpointer)data);
836 _gtk_cell_layout_buildable_add_child (GtkBuildable *buildable,
841 g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
842 g_return_if_fail (GTK_IS_CELL_RENDERER (child));
844 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);