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"
96 #define warn_no_cell_area(func) \
97 g_critical ("%s: Called but no GtkCellArea is available yet", func)
99 typedef GtkCellLayoutIface GtkCellLayoutInterface;
100 G_DEFINE_INTERFACE (GtkCellLayout, gtk_cell_layout, G_TYPE_OBJECT);
102 static void gtk_cell_layout_default_pack_start (GtkCellLayout *cell_layout,
103 GtkCellRenderer *cell,
105 static void gtk_cell_layout_default_pack_end (GtkCellLayout *cell_layout,
106 GtkCellRenderer *cell,
108 static void gtk_cell_layout_default_clear (GtkCellLayout *cell_layout);
109 static void gtk_cell_layout_default_add_attribute (GtkCellLayout *cell_layout,
110 GtkCellRenderer *cell,
111 const gchar *attribute,
113 static void gtk_cell_layout_default_set_cell_data_func (GtkCellLayout *cell_layout,
114 GtkCellRenderer *cell,
115 GtkCellLayoutDataFunc func,
117 GDestroyNotify destroy);
118 static void gtk_cell_layout_default_clear_attributes (GtkCellLayout *cell_layout,
119 GtkCellRenderer *cell);
120 static void gtk_cell_layout_default_reorder (GtkCellLayout *cell_layout,
121 GtkCellRenderer *cell,
123 static GList *gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout);
127 gtk_cell_layout_default_init (GtkCellLayoutIface *iface)
129 iface->pack_start = gtk_cell_layout_default_pack_start;
130 iface->pack_end = gtk_cell_layout_default_pack_end;
131 iface->clear = gtk_cell_layout_default_clear;
132 iface->add_attribute = gtk_cell_layout_default_add_attribute;
133 iface->set_cell_data_func = gtk_cell_layout_default_set_cell_data_func;
134 iface->clear_attributes = gtk_cell_layout_default_clear_attributes;
135 iface->reorder = gtk_cell_layout_default_reorder;
136 iface->get_cells = gtk_cell_layout_default_get_cells;
139 /* Default implementation is to fall back on an underlying cell area */
141 gtk_cell_layout_default_pack_start (GtkCellLayout *cell_layout,
142 GtkCellRenderer *cell,
145 GtkCellLayoutIface *iface;
148 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
152 area = iface->get_area (cell_layout);
155 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
157 warn_no_cell_area ("GtkCellLayoutIface->pack_start()");
162 gtk_cell_layout_default_pack_end (GtkCellLayout *cell_layout,
163 GtkCellRenderer *cell,
166 GtkCellLayoutIface *iface;
169 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
173 area = iface->get_area (cell_layout);
176 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
178 warn_no_cell_area ("GtkCellLayoutIface->pack_end()");
183 gtk_cell_layout_default_clear (GtkCellLayout *cell_layout)
185 GtkCellLayoutIface *iface;
188 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
192 area = iface->get_area (cell_layout);
195 gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
197 warn_no_cell_area ("GtkCellLayoutIface->clear()");
202 gtk_cell_layout_default_add_attribute (GtkCellLayout *cell_layout,
203 GtkCellRenderer *cell,
204 const gchar *attribute,
207 GtkCellLayoutIface *iface;
210 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
214 area = iface->get_area (cell_layout);
217 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
219 warn_no_cell_area ("GtkCellLayoutIface->add_attribute()");
224 gtk_cell_layout_default_set_cell_data_func (GtkCellLayout *cell_layout,
225 GtkCellRenderer *cell,
226 GtkCellLayoutDataFunc func,
228 GDestroyNotify destroy)
230 GtkCellLayoutIface *iface;
233 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
237 area = iface->get_area (cell_layout);
240 _gtk_cell_area_set_cell_data_func_with_proxy (area, cell,
241 (GFunc)func, func_data, destroy,
244 warn_no_cell_area ("GtkCellLayoutIface->set_cell_data_func()");
249 gtk_cell_layout_default_clear_attributes (GtkCellLayout *cell_layout,
250 GtkCellRenderer *cell)
252 GtkCellLayoutIface *iface;
255 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
259 area = iface->get_area (cell_layout);
262 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
264 warn_no_cell_area ("GtkCellLayoutIface->clear_attributes()");
269 gtk_cell_layout_default_reorder (GtkCellLayout *cell_layout,
270 GtkCellRenderer *cell,
273 GtkCellLayoutIface *iface;
276 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
280 area = iface->get_area (cell_layout);
283 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
285 warn_no_cell_area ("GtkCellLayoutIface->reorder()");
290 gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout)
292 GtkCellLayoutIface *iface;
295 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
299 area = iface->get_area (cell_layout);
302 return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
304 warn_no_cell_area ("GtkCellLayoutIface->get_cells()");
311 * gtk_cell_layout_pack_start:
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 * Packs the @cell into the beginning of @cell_layout. If @expand is %FALSE,
317 * then the @cell is allocated no more space than it needs. Any unused space
318 * is 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_start (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_start (cell_layout, cell, expand);
336 * gtk_cell_layout_pack_end:
337 * @cell_layout: a #GtkCellLayout
338 * @cell: a #GtkCellRenderer
339 * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
341 * Adds the @cell to the end of @cell_layout. If @expand is %FALSE, then the
342 * @cell is allocated no more space than it needs. Any unused space is
343 * divided evenly between cells for which @expand is %TRUE.
345 * Note that reusing the same cell renderer is not supported.
350 gtk_cell_layout_pack_end (GtkCellLayout *cell_layout,
351 GtkCellRenderer *cell,
354 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
355 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
357 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_end (cell_layout, cell, expand);
361 * gtk_cell_layout_clear:
362 * @cell_layout: a #GtkCellLayout
364 * Unsets all the mappings on all renderers on @cell_layout and
365 * removes all renderers from @cell_layout.
370 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
372 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
374 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear (cell_layout);
378 gtk_cell_layout_set_attributesv (GtkCellLayout *cell_layout,
379 GtkCellRenderer *cell,
385 attribute = va_arg (args, gchar *);
387 gtk_cell_layout_clear_attributes (cell_layout, cell);
389 while (attribute != NULL)
391 column = va_arg (args, gint);
393 gtk_cell_layout_add_attribute (cell_layout, cell, attribute, column);
395 attribute = va_arg (args, gchar *);
400 * gtk_cell_layout_set_attributes:
401 * @cell_layout: a #GtkCellLayout
402 * @cell: a #GtkCellRenderer
403 * @Varargs: a %NULL-terminated list of attributes
405 * Sets the attributes in list as the attributes of @cell_layout.
407 * The attributes should be in attribute/column order, as in
408 * gtk_cell_layout_add_attribute(). All existing attributes are
409 * removed, and replaced with the new attributes.
414 gtk_cell_layout_set_attributes (GtkCellLayout *cell_layout,
415 GtkCellRenderer *cell,
420 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
421 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
423 va_start (args, cell);
424 gtk_cell_layout_set_attributesv (cell_layout, cell, args);
429 * gtk_cell_layout_add_attribute:
430 * @cell_layout: a #GtkCellLayout
431 * @cell: a #GtkCellRenderer
432 * @attribute: an attribute on the renderer
433 * @column: the column position on the model to get the attribute from
435 * Adds an attribute mapping to the list in @cell_layout.
437 * The @column is the column of the model to get a value from, and the
438 * @attribute is the parameter on @cell to be set from the value. So for
439 * example if column 2 of the model contains strings, you could have the
440 * "text" attribute of a #GtkCellRendererText get its values from column 2.
445 gtk_cell_layout_add_attribute (GtkCellLayout *cell_layout,
446 GtkCellRenderer *cell,
447 const gchar *attribute,
450 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
451 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
452 g_return_if_fail (attribute != NULL);
453 g_return_if_fail (column >= 0);
455 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->add_attribute (cell_layout, cell, attribute, column);
459 * gtk_cell_layout_set_cell_data_func:
460 * @cell_layout: a #GtkCellLayout
461 * @cell: a #GtkCellRenderer
462 * @func: (allow-none): the #GtkCellLayoutDataFunc to use, or %NULL
463 * @func_data: user data for @func
464 * @destroy: destroy notify for @func_data
466 * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
468 * This function is used instead of the standard attributes mapping
469 * for setting the column value, and should set the value of @cell_layout's
470 * cell renderer(s) as appropriate.
472 * @func may be %NULL to remove a previously set function.
477 gtk_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
478 GtkCellRenderer *cell,
479 GtkCellLayoutDataFunc func,
481 GDestroyNotify destroy)
483 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
484 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
486 GTK_CELL_LAYOUT_GET_IFACE
487 (cell_layout)->set_cell_data_func (cell_layout, cell, func, func_data, destroy);
491 * gtk_cell_layout_clear_attributes:
492 * @cell_layout: a #GtkCellLayout
493 * @cell: a #GtkCellRenderer to clear the attribute mapping on
495 * Clears all existing attributes previously set with
496 * gtk_cell_layout_set_attributes().
501 gtk_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
502 GtkCellRenderer *cell)
504 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
505 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
507 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear_attributes (cell_layout, cell);
511 * gtk_cell_layout_reorder:
512 * @cell_layout: a #GtkCellLayout
513 * @cell: a #GtkCellRenderer to reorder
514 * @position: new position to insert @cell at
516 * Re-inserts @cell at @position.
518 * Note that @cell has already to be packed into @cell_layout
519 * for this to function properly.
524 gtk_cell_layout_reorder (GtkCellLayout *cell_layout,
525 GtkCellRenderer *cell,
528 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
529 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
531 GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->reorder (cell_layout, cell, position);
535 * gtk_cell_layout_get_cells:
536 * @cell_layout: a #GtkCellLayout
538 * Returns the cell renderers which have been added to @cell_layout.
540 * Return value: (element-type GtkCellRenderer) (transfer container):
541 * a list of cell renderers. The list, but not the renderers has
542 * been newly allocated and should be freed with g_list_free()
543 * when no longer needed.
548 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
550 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
552 return GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->get_cells (cell_layout);
556 * gtk_cell_layout_get_area:
557 * @cell_layout: a #GtkCellLayout
559 * Returns the underlying #GtkCellArea which might be @cell_layout
560 * if called on a #GtkCellArea or might be %NULL if no #GtkCellArea
561 * is used by @cell_layout.
563 * Return value: (transfer none): the cell area used by @cell_layout.
568 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
570 GtkCellLayoutIface *iface;
572 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
574 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
576 return iface->get_area (cell_layout);
581 /* Attribute parsing */
583 GtkCellLayout *cell_layout;
584 GtkCellRenderer *renderer;
586 } AttributesSubParserData;
589 attributes_start_element (GMarkupParseContext *context,
590 const gchar *element_name,
592 const gchar **values,
596 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
599 if (strcmp (element_name, "attribute") == 0)
601 for (i = 0; names[i]; i++)
602 if (strcmp (names[i], "name") == 0)
603 parser_data->attr_name = g_strdup (values[i]);
605 else if (strcmp (element_name, "attributes") == 0)
608 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
612 attributes_text_element (GMarkupParseContext *context,
618 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
623 if (!parser_data->attr_name)
626 string = g_strndup (text, text_len);
628 l = g_ascii_strtoll (string, &endptr, 0);
629 if (errno || endptr == string)
633 GTK_BUILDER_ERROR_INVALID_VALUE,
634 "Could not parse integer `%s'",
641 gtk_cell_layout_add_attribute (parser_data->cell_layout,
642 parser_data->renderer,
643 parser_data->attr_name, l);
644 g_free (parser_data->attr_name);
645 parser_data->attr_name = NULL;
648 static const GMarkupParser attributes_parser =
650 attributes_start_element,
652 attributes_text_element,
656 /* Cell packing parsing */
658 gtk_cell_layout_buildable_set_cell_property (GtkCellArea *area,
660 GtkCellRenderer *cell,
665 GValue gvalue = { 0, };
666 GError *error = NULL;
668 pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
671 g_warning ("%s does not have a property called %s",
672 g_type_name (G_OBJECT_TYPE (area)), name);
676 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
678 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
679 g_type_name (G_OBJECT_TYPE (area)),
682 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
684 g_error_free (error);
688 gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
689 g_value_unset (&gvalue);
694 GtkCellLayout *cell_layout;
695 GtkCellRenderer *renderer;
696 gchar *cell_prop_name;
698 gboolean translatable;
699 } CellPackingSubParserData;
702 cell_packing_start_element (GMarkupParseContext *context,
703 const gchar *element_name,
705 const gchar **values,
709 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
712 if (strcmp (element_name, "property") == 0)
714 for (i = 0; names[i]; i++)
715 if (strcmp (names[i], "name") == 0)
716 parser_data->cell_prop_name = g_strdup (values[i]);
717 else if (strcmp (names[i], "translatable") == 0)
719 if (!_gtk_builder_boolean_from_string (values[i],
720 &parser_data->translatable,
724 else if (strcmp (names[i], "comments") == 0)
725 ; /* for translators */
726 else if (strcmp (names[i], "context") == 0)
727 parser_data->context = g_strdup (values[i]);
729 g_warning ("Unsupported attribute for GtkCellLayout Cell "
730 "property: %s\n", names[i]);
732 else if (strcmp (element_name, "cell-packing") == 0)
735 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
739 cell_packing_text_element (GMarkupParseContext *context,
745 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
749 if (!parser_data->cell_prop_name)
752 if (parser_data->translatable && text_len)
755 domain = gtk_builder_get_translation_domain (parser_data->builder);
757 value = _gtk_builder_parser_translate (domain,
758 parser_data->context,
763 value = g_strdup (text);
766 area = gtk_cell_layout_get_area (parser_data->cell_layout);
770 g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
771 g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
775 gtk_cell_layout_buildable_set_cell_property (area,
776 parser_data->builder,
777 parser_data->renderer,
778 parser_data->cell_prop_name,
781 g_free (parser_data->cell_prop_name);
782 g_free (parser_data->context);
784 parser_data->cell_prop_name = NULL;
785 parser_data->context = NULL;
786 parser_data->translatable = FALSE;
789 static const GMarkupParser cell_packing_parser =
791 cell_packing_start_element,
793 cell_packing_text_element,
797 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable *buildable,
800 const gchar *tagname,
801 GMarkupParser *parser,
804 AttributesSubParserData *attr_data;
805 CellPackingSubParserData *packing_data;
810 if (strcmp (tagname, "attributes") == 0)
812 attr_data = g_slice_new0 (AttributesSubParserData);
813 attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
814 attr_data->renderer = GTK_CELL_RENDERER (child);
815 attr_data->attr_name = NULL;
817 *parser = attributes_parser;
821 else if (strcmp (tagname, "cell-packing") == 0)
823 packing_data = g_slice_new0 (CellPackingSubParserData);
824 packing_data->builder = builder;
825 packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
826 packing_data->renderer = GTK_CELL_RENDERER (child);
828 *parser = cell_packing_parser;
829 *data = packing_data;
837 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
840 const gchar *tagname,
843 AttributesSubParserData *attr_data;
845 if (strcmp (tagname, "attributes") == 0)
847 attr_data = (AttributesSubParserData*)data;
848 g_assert (!attr_data->attr_name);
849 g_slice_free (AttributesSubParserData, attr_data);
852 else if (strcmp (tagname, "cell-packing") == 0)
854 g_slice_free (CellPackingSubParserData, (gpointer)data);
861 _gtk_cell_layout_buildable_add_child (GtkBuildable *buildable,
866 g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
867 g_return_if_fail (GTK_IS_CELL_RENDERER (child));
869 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);