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);
102 gtk_cell_layout_default_init (GtkCellLayoutInterface *iface)
107 * gtk_cell_layout_pack_start:
108 * @cell_layout: a #GtkCellLayout
109 * @cell: a #GtkCellRenderer
110 * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
112 * Packs the @cell into the beginning of @cell_layout. If @expand is %FALSE,
113 * then the @cell is allocated no more space than it needs. Any unused space
114 * is divided evenly between cells for which @expand is %TRUE.
116 * Note that reusing the same cell renderer is not supported.
121 gtk_cell_layout_pack_start (GtkCellLayout *cell_layout,
122 GtkCellRenderer *cell,
125 GtkCellLayoutIface *iface;
128 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
129 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
131 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
133 if (iface->pack_start)
134 iface->pack_start (cell_layout, cell, expand);
137 area = iface->get_area (cell_layout);
140 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
145 * gtk_cell_layout_pack_end:
146 * @cell_layout: a #GtkCellLayout
147 * @cell: a #GtkCellRenderer
148 * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
150 * Adds the @cell to the end of @cell_layout. If @expand is %FALSE, then the
151 * @cell is allocated no more space than it needs. Any unused space is
152 * divided evenly between cells for which @expand is %TRUE.
154 * Note that reusing the same cell renderer is not supported.
159 gtk_cell_layout_pack_end (GtkCellLayout *cell_layout,
160 GtkCellRenderer *cell,
163 GtkCellLayoutIface *iface;
166 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
167 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
169 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
172 iface->pack_end (cell_layout, cell, expand);
175 area = iface->get_area (cell_layout);
178 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
183 * gtk_cell_layout_clear:
184 * @cell_layout: a #GtkCellLayout
186 * Unsets all the mappings on all renderers on @cell_layout and
187 * removes all renderers from @cell_layout.
192 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
194 GtkCellLayoutIface *iface;
197 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
199 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
202 iface->clear (cell_layout);
205 area = iface->get_area (cell_layout);
208 gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
213 gtk_cell_layout_set_attributesv (GtkCellLayout *cell_layout,
214 GtkCellRenderer *cell,
219 GtkCellLayoutIface *iface;
222 attribute = va_arg (args, gchar *);
224 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
227 area = iface->get_area (cell_layout);
229 if (iface->clear_attributes)
230 iface->clear_attributes (cell_layout, cell);
232 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
234 while (attribute != NULL)
236 column = va_arg (args, gint);
237 if (iface->add_attribute)
238 iface->add_attribute (cell_layout, cell, attribute, column);
240 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
242 attribute = va_arg (args, gchar *);
247 * gtk_cell_layout_set_attributes:
248 * @cell_layout: a #GtkCellLayout
249 * @cell: a #GtkCellRenderer
250 * @Varargs: a %NULL-terminated list of attributes
252 * Sets the attributes in list as the attributes of @cell_layout.
254 * The attributes should be in attribute/column order, as in
255 * gtk_cell_layout_add_attribute(). All existing attributes are
256 * removed, and replaced with the new attributes.
261 gtk_cell_layout_set_attributes (GtkCellLayout *cell_layout,
262 GtkCellRenderer *cell,
267 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
268 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
270 va_start (args, cell);
271 gtk_cell_layout_set_attributesv (cell_layout, cell, args);
276 * gtk_cell_layout_add_attribute:
277 * @cell_layout: a #GtkCellLayout
278 * @cell: a #GtkCellRenderer
279 * @attribute: an attribute on the renderer
280 * @column: the column position on the model to get the attribute from
282 * Adds an attribute mapping to the list in @cell_layout.
284 * The @column is the column of the model to get a value from, and the
285 * @attribute is the parameter on @cell to be set from the value. So for
286 * example if column 2 of the model contains strings, you could have the
287 * "text" attribute of a #GtkCellRendererText get its values from column 2.
292 gtk_cell_layout_add_attribute (GtkCellLayout *cell_layout,
293 GtkCellRenderer *cell,
294 const gchar *attribute,
297 GtkCellLayoutIface *iface;
300 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
301 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
302 g_return_if_fail (attribute != NULL);
303 g_return_if_fail (column >= 0);
305 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
307 if (iface->add_attribute)
308 iface->add_attribute (cell_layout,
314 area = iface->get_area (cell_layout);
317 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
322 * gtk_cell_layout_set_cell_data_func:
323 * @cell_layout: a #GtkCellLayout
324 * @cell: a #GtkCellRenderer
325 * @func (allow-none: the #GtkCellLayoutDataFunc to use, or %NULL
326 * @func_data: user data for @func
327 * @destroy: destroy notify for @func_data
329 * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
331 * This function is used instead of the standard attributes mapping
332 * for setting the column value, and should set the value of @cell_layout's
333 * cell renderer(s) as appropriate.
335 * @func may be %NULL to remove a previously set function.
340 gtk_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
341 GtkCellRenderer *cell,
342 GtkCellLayoutDataFunc func,
344 GDestroyNotify destroy)
346 GtkCellLayoutIface *iface;
349 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
350 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
352 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
354 if (iface->set_cell_data_func)
355 iface->set_cell_data_func (cell_layout,
362 area = iface->get_area (cell_layout);
366 /* Ensure that the correct proxy object is sent to 'func' */
367 if (GTK_CELL_LAYOUT (area) != cell_layout)
368 _gtk_cell_area_set_cell_data_func_with_proxy (area, cell,
369 (GFunc)func, func_data, destroy,
372 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (area), cell,
373 func, func_data, destroy);
379 * gtk_cell_layout_clear_attributes:
380 * @cell_layout: a #GtkCellLayout
381 * @cell: a #GtkCellRenderer to clear the attribute mapping on
383 * Clears all existing attributes previously set with
384 * gtk_cell_layout_set_attributes().
389 gtk_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
390 GtkCellRenderer *cell)
392 GtkCellLayoutIface *iface;
395 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
396 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
398 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
400 if (iface->clear_attributes)
401 iface->clear_attributes (cell_layout, cell);
404 area = iface->get_area (cell_layout);
407 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
412 * gtk_cell_layout_reorder:
413 * @cell_layout: a #GtkCellLayout
414 * @cell: a #GtkCellRenderer to reorder
415 * @position: new position to insert @cell at
417 * Re-inserts @cell at @position.
419 * Note that @cell has already to be packed into @cell_layout
420 * for this to function properly.
425 gtk_cell_layout_reorder (GtkCellLayout *cell_layout,
426 GtkCellRenderer *cell,
429 GtkCellLayoutIface *iface;
432 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
433 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
435 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
438 iface->reorder (cell_layout, cell, position);
441 area = iface->get_area (cell_layout);
444 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
449 * gtk_cell_layout_get_cells:
450 * @cell_layout: a #GtkCellLayout
452 * Returns the cell renderers which have been added to @cell_layout.
454 * Return value: (element-type GtkCellRenderer) (transfer container):
455 * a list of cell renderers. The list, but not the renderers has
456 * been newly allocated and should be freed with g_list_free()
457 * when no longer needed.
462 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
464 GtkCellLayoutIface *iface;
467 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
469 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
470 if (iface->get_cells)
471 return iface->get_cells (cell_layout);
474 area = iface->get_area (cell_layout);
477 return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
484 * gtk_cell_layout_get_area:
485 * @cell_layout: a #GtkCellLayout
487 * Returns the underlying #GtkCellArea which might be @cell_layout
488 * if called on a #GtkCellArea or might be %NULL if no #GtkCellArea
489 * is used by @cell_layout.
491 * Return value: (transfer none): the cell area used by @cell_layout.
496 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
498 GtkCellLayoutIface *iface;
500 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
502 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
504 return iface->get_area (cell_layout);
509 /* Attribute parsing */
511 GtkCellLayout *cell_layout;
512 GtkCellRenderer *renderer;
514 } AttributesSubParserData;
517 attributes_start_element (GMarkupParseContext *context,
518 const gchar *element_name,
520 const gchar **values,
524 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
527 if (strcmp (element_name, "attribute") == 0)
529 for (i = 0; names[i]; i++)
530 if (strcmp (names[i], "name") == 0)
531 parser_data->attr_name = g_strdup (values[i]);
533 else if (strcmp (element_name, "attributes") == 0)
536 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
540 attributes_text_element (GMarkupParseContext *context,
546 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
551 if (!parser_data->attr_name)
554 string = g_strndup (text, text_len);
556 l = g_ascii_strtoll (string, &endptr, 0);
557 if (errno || endptr == string)
561 GTK_BUILDER_ERROR_INVALID_VALUE,
562 "Could not parse integer `%s'",
569 gtk_cell_layout_add_attribute (parser_data->cell_layout,
570 parser_data->renderer,
571 parser_data->attr_name, l);
572 g_free (parser_data->attr_name);
573 parser_data->attr_name = NULL;
576 static const GMarkupParser attributes_parser =
578 attributes_start_element,
580 attributes_text_element,
584 /* Cell packing parsing */
586 gtk_cell_layout_buildable_set_cell_property (GtkCellArea *area,
588 GtkCellRenderer *cell,
593 GValue gvalue = { 0, };
594 GError *error = NULL;
596 pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
599 g_warning ("%s does not have a property called %s",
600 g_type_name (G_OBJECT_TYPE (area)), name);
604 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
606 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
607 g_type_name (G_OBJECT_TYPE (area)),
610 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
612 g_error_free (error);
616 gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
617 g_value_unset (&gvalue);
622 GtkCellLayout *cell_layout;
623 GtkCellRenderer *renderer;
624 gchar *cell_prop_name;
626 gboolean translatable;
627 } CellPackingSubParserData;
630 cell_packing_start_element (GMarkupParseContext *context,
631 const gchar *element_name,
633 const gchar **values,
637 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
640 if (strcmp (element_name, "property") == 0)
642 for (i = 0; names[i]; i++)
643 if (strcmp (names[i], "name") == 0)
644 parser_data->cell_prop_name = g_strdup (values[i]);
645 else if (strcmp (names[i], "translatable") == 0)
647 if (!_gtk_builder_boolean_from_string (values[i],
648 &parser_data->translatable,
652 else if (strcmp (names[i], "comments") == 0)
653 ; /* for translators */
654 else if (strcmp (names[i], "context") == 0)
655 parser_data->context = g_strdup (values[i]);
657 g_warning ("Unsupported attribute for GtkCellLayout Cell "
658 "property: %s\n", names[i]);
660 else if (strcmp (element_name, "cell-packing") == 0)
663 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
667 cell_packing_text_element (GMarkupParseContext *context,
673 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
677 if (!parser_data->cell_prop_name)
680 if (parser_data->translatable && text_len)
683 domain = gtk_builder_get_translation_domain (parser_data->builder);
685 value = _gtk_builder_parser_translate (domain,
686 parser_data->context,
691 value = g_strdup (text);
694 area = gtk_cell_layout_get_area (parser_data->cell_layout);
698 g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
699 g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
703 gtk_cell_layout_buildable_set_cell_property (area,
704 parser_data->builder,
705 parser_data->renderer,
706 parser_data->cell_prop_name,
709 g_free (parser_data->cell_prop_name);
710 g_free (parser_data->context);
712 parser_data->cell_prop_name = NULL;
713 parser_data->context = NULL;
714 parser_data->translatable = FALSE;
717 static const GMarkupParser cell_packing_parser =
719 cell_packing_start_element,
721 cell_packing_text_element,
725 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable *buildable,
728 const gchar *tagname,
729 GMarkupParser *parser,
732 AttributesSubParserData *attr_data;
733 CellPackingSubParserData *packing_data;
738 if (strcmp (tagname, "attributes") == 0)
740 attr_data = g_slice_new0 (AttributesSubParserData);
741 attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
742 attr_data->renderer = GTK_CELL_RENDERER (child);
743 attr_data->attr_name = NULL;
745 *parser = attributes_parser;
749 else if (strcmp (tagname, "cell-packing") == 0)
751 packing_data = g_slice_new0 (CellPackingSubParserData);
752 packing_data->builder = builder;
753 packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
754 packing_data->renderer = GTK_CELL_RENDERER (child);
756 *parser = cell_packing_parser;
757 *data = packing_data;
765 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
768 const gchar *tagname,
771 AttributesSubParserData *attr_data;
773 if (strcmp (tagname, "attributes") == 0)
775 attr_data = (AttributesSubParserData*)data;
776 g_assert (!attr_data->attr_name);
777 g_slice_free (AttributesSubParserData, attr_data);
780 else if (strcmp (tagname, "cell-packing") == 0)
782 g_slice_free (CellPackingSubParserData, (gpointer)data);
789 _gtk_cell_layout_buildable_add_child (GtkBuildable *buildable,
794 g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
795 g_return_if_fail (GTK_IS_CELL_RENDERER (child));
797 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);