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 (most, of not 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. The
253 * attributes should be in attribute/column order, as in
254 * gtk_cell_layout_add_attribute(). All existing attributes are removed, and
255 * replaced with the new attributes.
260 gtk_cell_layout_set_attributes (GtkCellLayout *cell_layout,
261 GtkCellRenderer *cell,
266 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
267 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
269 va_start (args, cell);
270 gtk_cell_layout_set_attributesv (cell_layout, cell, args);
275 * gtk_cell_layout_add_attribute:
276 * @cell_layout: A #GtkCellLayout.
277 * @cell: A #GtkCellRenderer.
278 * @attribute: An attribute on the renderer.
279 * @column: The column position on the model to get the attribute from.
281 * Adds an attribute mapping to the list in @cell_layout. The @column is the
282 * column of the model to get a value from, and the @attribute is the
283 * parameter on @cell to be set from the value. So for example if column 2
284 * of the model contains strings, you could have the "text" attribute of a
285 * #GtkCellRendererText get its values from column 2.
290 gtk_cell_layout_add_attribute (GtkCellLayout *cell_layout,
291 GtkCellRenderer *cell,
292 const gchar *attribute,
295 GtkCellLayoutIface *iface;
298 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
299 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
300 g_return_if_fail (attribute != NULL);
301 g_return_if_fail (column >= 0);
303 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
305 if (iface->add_attribute)
306 iface->add_attribute (cell_layout,
312 area = iface->get_area (cell_layout);
315 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
320 * gtk_cell_layout_set_cell_data_func:
321 * @cell_layout: A #GtkCellLayout.
322 * @cell: A #GtkCellRenderer.
323 * @func: The #GtkCellLayoutDataFunc to use.
324 * @func_data: The user data for @func.
325 * @destroy: The destroy notification for @func_data.
327 * Sets the #GtkCellLayoutDataFunc to use for @cell_layout. This function
328 * is used instead of the standard attributes mapping for setting the
329 * column value, and should set the value of @cell_layout's cell renderer(s)
330 * as appropriate. @func may be %NULL to remove and older one.
335 gtk_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
336 GtkCellRenderer *cell,
337 GtkCellLayoutDataFunc func,
339 GDestroyNotify destroy)
341 GtkCellLayoutIface *iface;
344 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
345 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
347 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
349 if (iface->set_cell_data_func)
350 iface->set_cell_data_func (cell_layout,
357 area = iface->get_area (cell_layout);
360 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (area), cell, func, func_data, destroy);
365 * gtk_cell_layout_clear_attributes:
366 * @cell_layout: A #GtkCellLayout.
367 * @cell: A #GtkCellRenderer to clear the attribute mapping on.
369 * Clears all existing attributes previously set with
370 * gtk_cell_layout_set_attributes().
375 gtk_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
376 GtkCellRenderer *cell)
378 GtkCellLayoutIface *iface;
381 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
382 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
384 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
386 if (iface->clear_attributes)
387 iface->clear_attributes (cell_layout, cell);
390 area = iface->get_area (cell_layout);
393 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
398 * gtk_cell_layout_reorder:
399 * @cell_layout: A #GtkCellLayout.
400 * @cell: A #GtkCellRenderer to reorder.
401 * @position: New position to insert @cell at.
403 * Re-inserts @cell at @position. Note that @cell has already to be packed
404 * into @cell_layout for this to function properly.
409 gtk_cell_layout_reorder (GtkCellLayout *cell_layout,
410 GtkCellRenderer *cell,
413 GtkCellLayoutIface *iface;
416 g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
417 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
419 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
422 iface->reorder (cell_layout, cell, position);
425 area = iface->get_area (cell_layout);
428 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
433 * gtk_cell_layout_get_cells:
434 * @cell_layout: a #GtkCellLayout
436 * Returns the cell renderers which have been added to @cell_layout.
438 * Return value: (element-type GtkCellRenderer) (transfer container): a list of cell renderers. The list, but not the
439 * renderers has been newly allocated and should be freed with
440 * g_list_free() when no longer needed.
445 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
447 GtkCellLayoutIface *iface;
450 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
452 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
453 if (iface->get_cells)
454 return iface->get_cells (cell_layout);
457 area = iface->get_area (cell_layout);
460 return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
467 * gtk_cell_layout_get_area:
468 * @cell_layout: a #GtkCellLayout
470 * Returns the underlying #GtkCellArea which might be @cell_layout if called on a #GtkCellArea or
471 * might be %NULL if no #GtkCellArea is used by @cell_layout.
473 * Return value: (transfer none): a list of cell renderers. The list, but not the
474 * renderers has been newly allocated and should be freed with
475 * g_list_free() when no longer needed.
480 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
482 GtkCellLayoutIface *iface;
484 g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
486 iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
488 return iface->get_area (cell_layout);
493 /* Attribute parsing */
495 GtkCellLayout *cell_layout;
496 GtkCellRenderer *renderer;
498 } AttributesSubParserData;
501 attributes_start_element (GMarkupParseContext *context,
502 const gchar *element_name,
504 const gchar **values,
508 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
511 if (strcmp (element_name, "attribute") == 0)
513 for (i = 0; names[i]; i++)
514 if (strcmp (names[i], "name") == 0)
515 parser_data->attr_name = g_strdup (values[i]);
517 else if (strcmp (element_name, "attributes") == 0)
520 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
524 attributes_text_element (GMarkupParseContext *context,
530 AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
535 if (!parser_data->attr_name)
538 string = g_strndup (text, text_len);
540 l = g_ascii_strtoll (string, &endptr, 0);
541 if (errno || endptr == string)
545 GTK_BUILDER_ERROR_INVALID_VALUE,
546 "Could not parse integer `%s'",
553 gtk_cell_layout_add_attribute (parser_data->cell_layout,
554 parser_data->renderer,
555 parser_data->attr_name, l);
556 g_free (parser_data->attr_name);
557 parser_data->attr_name = NULL;
560 static const GMarkupParser attributes_parser =
562 attributes_start_element,
564 attributes_text_element,
568 /* Cell packing parsing */
570 gtk_cell_layout_buildable_set_cell_property (GtkCellArea *area,
572 GtkCellRenderer *cell,
577 GValue gvalue = { 0, };
578 GError *error = NULL;
580 pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
583 g_warning ("%s does not have a property called %s",
584 g_type_name (G_OBJECT_TYPE (area)), name);
588 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
590 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
591 g_type_name (G_OBJECT_TYPE (area)),
594 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
596 g_error_free (error);
600 gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
601 g_value_unset (&gvalue);
606 GtkCellLayout *cell_layout;
607 GtkCellRenderer *renderer;
608 gchar *cell_prop_name;
610 gboolean translatable;
611 } CellPackingSubParserData;
614 cell_packing_start_element (GMarkupParseContext *context,
615 const gchar *element_name,
617 const gchar **values,
621 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
624 if (strcmp (element_name, "property") == 0)
626 for (i = 0; names[i]; i++)
627 if (strcmp (names[i], "name") == 0)
628 parser_data->cell_prop_name = g_strdup (values[i]);
629 else if (strcmp (names[i], "translatable") == 0)
631 if (!_gtk_builder_boolean_from_string (values[i],
632 &parser_data->translatable,
636 else if (strcmp (names[i], "comments") == 0)
637 ; /* for translators */
638 else if (strcmp (names[i], "context") == 0)
639 parser_data->context = g_strdup (values[i]);
641 g_warning ("Unsupported attribute for GtkCellLayout Cell "
642 "property: %s\n", names[i]);
644 else if (strcmp (element_name, "cell-packing") == 0)
647 g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
651 cell_packing_text_element (GMarkupParseContext *context,
657 CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
661 if (!parser_data->cell_prop_name)
664 if (parser_data->translatable && text_len)
667 domain = gtk_builder_get_translation_domain (parser_data->builder);
669 value = _gtk_builder_parser_translate (domain,
670 parser_data->context,
675 value = g_strdup (text);
678 area = gtk_cell_layout_get_area (parser_data->cell_layout);
682 g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
683 g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
687 gtk_cell_layout_buildable_set_cell_property (area,
688 parser_data->builder,
689 parser_data->renderer,
690 parser_data->cell_prop_name,
693 g_free (parser_data->cell_prop_name);
694 g_free (parser_data->context);
696 parser_data->cell_prop_name = NULL;
697 parser_data->context = NULL;
698 parser_data->translatable = FALSE;
701 static const GMarkupParser cell_packing_parser =
703 cell_packing_start_element,
705 cell_packing_text_element,
709 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable *buildable,
712 const gchar *tagname,
713 GMarkupParser *parser,
716 AttributesSubParserData *attr_data;
717 CellPackingSubParserData *packing_data;
722 if (strcmp (tagname, "attributes") == 0)
724 attr_data = g_slice_new0 (AttributesSubParserData);
725 attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
726 attr_data->renderer = GTK_CELL_RENDERER (child);
727 attr_data->attr_name = NULL;
729 *parser = attributes_parser;
733 else if (strcmp (tagname, "cell-packing") == 0)
735 packing_data = g_slice_new0 (CellPackingSubParserData);
736 packing_data->builder = builder;
737 packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
738 packing_data->renderer = GTK_CELL_RENDERER (child);
740 *parser = cell_packing_parser;
741 *data = packing_data;
749 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
752 const gchar *tagname,
755 AttributesSubParserData *attr_data;
757 if (strcmp (tagname, "attributes") == 0)
759 attr_data = (AttributesSubParserData*)data;
760 g_assert (!attr_data->attr_name);
761 g_slice_free (AttributesSubParserData, attr_data);
764 else if (strcmp (tagname, "cell-packing") == 0)
766 g_slice_free (CellPackingSubParserData, (gpointer)data);
772 _gtk_cell_layout_buildable_add_child (GtkBuildable *buildable,
777 g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
778 g_return_if_fail (GTK_IS_CELL_RENDERER (child));
780 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);