]> Pileus Git - ~andy/gtk/blob - gtk/gtkcelllayout.c
Removed special casing code in GtkCellLayout in favor of default implementation.
[~andy/gtk] / gtk / gtkcelllayout.c
1 /* gtkcelllayout.c
2  * Copyright (C) 2003  Kristian Rietveld  <kris@gtk.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:gtkcelllayout
22  * @Short_Description: An interface for packing cells
23  * @Title: GtkCellLayout
24  *
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.
28  *
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.
38  *
39  * <refsect2 id="GtkCellLayout-BUILDER-UI">
40  * <title>GtkCellLayouts as GtkBuildable</title>
41  * <para>
42  * Implementations of GtkCellLayout which also implement the GtkBuildable
43  * interface (#GtkCellView, #GtkIconView, #GtkComboBox, #GtkComboBoxEntry,
44  * #GtkEntryCompletion, #GtkTreeViewColumn) accept GtkCellRenderer objects
45  * as &lt;child&gt; elements in UI definitions. They support a custom
46  * &lt;attributes&gt; element for their children, which can contain
47  * multiple &lt;attribute&gt; elements. Each &lt;attribute&gt; element has
48  * a name attribute which specifies a property of the cell renderer; the
49  * content of the element is the attribute value.
50  *
51  * <example>
52  * <title>A UI definition fragment specifying attributes</title>
53  * <programlisting><![CDATA[
54  * <object class="GtkCellView">
55  *   <child>
56  *     <object class="GtkCellRendererText"/>
57  *     <attributes>
58  *       <attribute name="text">0</attribute>
59  *     </attributes>
60  *   </child>"
61  * </object>
62  * ]]></programlisting>
63  * </example>
64  *
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 &lt;cell-packing&gt; attribute which
69  * can contain multiple &lt;property&gt; elements defined in the normal way.
70  * <example>
71  * <title>A UI definition fragment specifying cell properties</title>
72  * <programlisting><![CDATA[
73  * <object class="GtkTreeViewColumn">
74  *   <child>
75  *     <object class="GtkCellRendererText"/>
76  *     <cell-packing>
77  *       <property name="align">True</property>
78  *       <property name="expand">False</property>
79  *     </cell-packing>
80  *   </child>"
81  * </object>
82  * ]]></programlisting>
83  * </example>
84  * </para>
85  * </refsect2>
86  */
87
88 #include "config.h"
89 #include <string.h>
90 #include <stdlib.h>
91 #include <errno.h>
92 #include "gtkcelllayout.h"
93 #include "gtkbuilderprivate.h"
94 #include "gtkintl.h"
95
96
97 typedef GtkCellLayoutIface GtkCellLayoutInterface;
98 G_DEFINE_INTERFACE (GtkCellLayout, gtk_cell_layout, G_TYPE_OBJECT);
99
100 static void   gtk_cell_layout_default_pack_start         (GtkCellLayout         *cell_layout,
101                                                           GtkCellRenderer       *cell,
102                                                           gboolean               expand);
103 static void   gtk_cell_layout_default_pack_end           (GtkCellLayout         *cell_layout,
104                                                           GtkCellRenderer       *cell,
105                                                           gboolean               expand);
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,
110                                                           gint                   column);
111 static void   gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
112                                                           GtkCellRenderer       *cell,
113                                                           GtkCellLayoutDataFunc  func,
114                                                           gpointer               func_data,
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,
120                                                           gint                   position);
121 static GList *gtk_cell_layout_default_get_cells          (GtkCellLayout         *cell_layout);
122
123
124 static void
125 gtk_cell_layout_default_init (GtkCellLayoutIface *iface)
126 {
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;
135 }
136
137
138 /* Default implementation is to fall back on an underlying cell area */
139 static void
140 gtk_cell_layout_default_pack_start (GtkCellLayout         *cell_layout,
141                                     GtkCellRenderer       *cell,
142                                     gboolean               expand)
143 {
144   GtkCellLayoutIface *iface;
145   GtkCellArea        *area;
146
147   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
148
149   if (iface->get_area)
150     {
151       area = iface->get_area (cell_layout);
152
153       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
154     }
155 }
156
157 static void
158 gtk_cell_layout_default_pack_end (GtkCellLayout         *cell_layout,
159                                   GtkCellRenderer       *cell,
160                                   gboolean               expand)
161 {
162   GtkCellLayoutIface *iface;
163   GtkCellArea        *area;
164
165   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
166
167   if (iface->get_area)
168     {
169       area = iface->get_area (cell_layout);
170
171       gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
172     }
173 }
174
175 static void
176 gtk_cell_layout_default_clear (GtkCellLayout *cell_layout)
177 {
178   GtkCellLayoutIface *iface;
179   GtkCellArea        *area;
180
181   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
182
183   if (iface->get_area)
184     {
185       area = iface->get_area (cell_layout);
186
187       gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
188     }
189 }
190
191 static void
192 gtk_cell_layout_default_add_attribute (GtkCellLayout         *cell_layout,
193                                        GtkCellRenderer       *cell,
194                                        const gchar           *attribute,
195                                        gint                   column)
196 {
197   GtkCellLayoutIface *iface;
198   GtkCellArea        *area;
199
200   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
201
202   if (iface->get_area)
203     {
204       area = iface->get_area (cell_layout);
205
206       gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
207     }
208 }
209
210 static void
211 gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
212                                             GtkCellRenderer       *cell,
213                                             GtkCellLayoutDataFunc  func,
214                                             gpointer               func_data,
215                                             GDestroyNotify         destroy)
216 {
217   GtkCellLayoutIface *iface;
218   GtkCellArea        *area;
219
220   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
221
222   if (iface->get_area)
223     {
224       area = iface->get_area (cell_layout);
225
226       _gtk_cell_area_set_cell_data_func_with_proxy (area, cell, 
227                                                     (GFunc)func, func_data, destroy, 
228                                                     cell_layout);
229     }
230 }
231
232 static void
233 gtk_cell_layout_default_clear_attributes (GtkCellLayout         *cell_layout,
234                                           GtkCellRenderer       *cell)
235 {
236   GtkCellLayoutIface *iface;
237   GtkCellArea        *area;
238
239   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
240
241   if (iface->get_area)
242     {
243       area = iface->get_area (cell_layout);
244
245       gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
246     }
247 }
248
249 static void
250 gtk_cell_layout_default_reorder (GtkCellLayout         *cell_layout,
251                                  GtkCellRenderer       *cell,
252                                  gint                   position)
253 {
254   GtkCellLayoutIface *iface;
255   GtkCellArea        *area;
256
257   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
258
259   if (iface->get_area)
260     {
261       area = iface->get_area (cell_layout);
262
263       gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
264     }
265 }
266
267 static GList *
268 gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout)
269 {
270   GtkCellLayoutIface *iface;
271   GtkCellArea        *area;
272
273   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
274
275   if (iface->get_area)
276     {
277       area = iface->get_area (cell_layout);
278
279       return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
280     }
281   return NULL;
282 }
283
284
285 /**
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
290  *
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.
294  *
295  * Note that reusing the same cell renderer is not supported.
296  *
297  * Since: 2.4
298  */
299 void
300 gtk_cell_layout_pack_start (GtkCellLayout   *cell_layout,
301                             GtkCellRenderer *cell,
302                             gboolean         expand)
303 {
304   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
305   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
306
307   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_start (cell_layout, cell, expand);
308 }
309
310 /**
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
315  *
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.
319  *
320  * Note that reusing the same cell renderer is not supported.
321  *
322  * Since: 2.4
323  */
324 void
325 gtk_cell_layout_pack_end (GtkCellLayout   *cell_layout,
326                           GtkCellRenderer *cell,
327                           gboolean         expand)
328 {
329   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
330   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
331
332   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_end (cell_layout, cell, expand);
333 }
334
335 /**
336  * gtk_cell_layout_clear:
337  * @cell_layout: a #GtkCellLayout
338  *
339  * Unsets all the mappings on all renderers on @cell_layout and
340  * removes all renderers from @cell_layout.
341  *
342  * Since: 2.4
343  */
344 void
345 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
346 {
347   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
348
349   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear (cell_layout);
350 }
351
352 static void
353 gtk_cell_layout_set_attributesv (GtkCellLayout   *cell_layout,
354                                  GtkCellRenderer *cell,
355                                  va_list          args)
356 {
357   gchar *attribute;
358   gint column;
359
360   attribute = va_arg (args, gchar *);
361
362   gtk_cell_layout_clear_attributes (cell_layout, cell);
363
364   while (attribute != NULL)
365     {
366       column = va_arg (args, gint);
367
368       gtk_cell_layout_add_attribute (cell_layout, cell, attribute, column);
369
370       attribute = va_arg (args, gchar *);
371     }
372 }
373
374 /**
375  * gtk_cell_layout_set_attributes:
376  * @cell_layout: a #GtkCellLayout
377  * @cell: a #GtkCellRenderer
378  * @Varargs: a %NULL-terminated list of attributes
379  *
380  * Sets the attributes in list as the attributes of @cell_layout.
381  *
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.
385  *
386  * Since: 2.4
387  */
388 void
389 gtk_cell_layout_set_attributes (GtkCellLayout   *cell_layout,
390                                 GtkCellRenderer *cell,
391                                 ...)
392 {
393   va_list args;
394
395   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
396   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
397
398   va_start (args, cell);
399   gtk_cell_layout_set_attributesv (cell_layout, cell, args);
400   va_end (args);
401 }
402
403 /**
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
409  *
410  * Adds an attribute mapping to the list in @cell_layout.
411  *
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.
416  *
417  * Since: 2.4
418  */
419 void
420 gtk_cell_layout_add_attribute (GtkCellLayout   *cell_layout,
421                                GtkCellRenderer *cell,
422                                const gchar     *attribute,
423                                gint             column)
424 {
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);
429
430   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->add_attribute (cell_layout, cell, attribute, column);
431 }
432
433 /**
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
440  *
441  * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
442  *
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.
446  *
447  * @func may be %NULL to remove a previously set function.
448  *
449  * Since: 2.4
450  */
451 void
452 gtk_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
453                                     GtkCellRenderer       *cell,
454                                     GtkCellLayoutDataFunc  func,
455                                     gpointer               func_data,
456                                     GDestroyNotify         destroy)
457 {
458   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
459   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
460
461   GTK_CELL_LAYOUT_GET_IFACE 
462     (cell_layout)->set_cell_data_func (cell_layout, cell, func, func_data, destroy);
463 }
464
465 /**
466  * gtk_cell_layout_clear_attributes:
467  * @cell_layout: a #GtkCellLayout
468  * @cell: a #GtkCellRenderer to clear the attribute mapping on
469  *
470  * Clears all existing attributes previously set with
471  * gtk_cell_layout_set_attributes().
472  *
473  * Since: 2.4
474  */
475 void
476 gtk_cell_layout_clear_attributes (GtkCellLayout   *cell_layout,
477                                   GtkCellRenderer *cell)
478 {
479   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
480   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
481
482   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear_attributes (cell_layout, cell);
483 }
484
485 /**
486  * gtk_cell_layout_reorder:
487  * @cell_layout: a #GtkCellLayout
488  * @cell: a #GtkCellRenderer to reorder
489  * @position: new position to insert @cell at
490  *
491  * Re-inserts @cell at @position.
492  *
493  * Note that @cell has already to be packed into @cell_layout
494  * for this to function properly.
495  *
496  * Since: 2.4
497  */
498 void
499 gtk_cell_layout_reorder (GtkCellLayout   *cell_layout,
500                          GtkCellRenderer *cell,
501                          gint             position)
502 {
503   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
504   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
505
506   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->reorder (cell_layout, cell, position);
507 }
508
509 /**
510  * gtk_cell_layout_get_cells:
511  * @cell_layout: a #GtkCellLayout
512  *
513  * Returns the cell renderers which have been added to @cell_layout.
514  *
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.
519  *
520  * Since: 2.12
521  */
522 GList *
523 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
524 {
525   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
526
527   return GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->get_cells (cell_layout);
528 }
529
530 /**
531  * gtk_cell_layout_get_area:
532  * @cell_layout: a #GtkCellLayout
533  *
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.
537  *
538  * Return value: (transfer none): the cell area used by @cell_layout.
539  *
540  * Since: 3.0
541  */
542 GtkCellArea *
543 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
544 {
545   GtkCellLayoutIface *iface;
546
547   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
548
549   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);  
550   if (iface->get_area)
551     return iface->get_area (cell_layout);
552
553   return NULL;
554 }
555
556 /* Attribute parsing */
557 typedef struct {
558   GtkCellLayout   *cell_layout;
559   GtkCellRenderer *renderer;
560   gchar           *attr_name;
561 } AttributesSubParserData;
562
563 static void
564 attributes_start_element (GMarkupParseContext *context,
565                           const gchar         *element_name,
566                           const gchar        **names,
567                           const gchar        **values,
568                           gpointer             user_data,
569                           GError             **error)
570 {
571   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
572   guint i;
573
574   if (strcmp (element_name, "attribute") == 0)
575     {
576       for (i = 0; names[i]; i++)
577         if (strcmp (names[i], "name") == 0)
578           parser_data->attr_name = g_strdup (values[i]);
579     }
580   else if (strcmp (element_name, "attributes") == 0)
581     return;
582   else
583     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
584 }
585
586 static void
587 attributes_text_element (GMarkupParseContext *context,
588                          const gchar         *text,
589                          gsize                text_len,
590                          gpointer             user_data,
591                          GError             **error)
592 {
593   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
594   glong l;
595   gchar *endptr;
596   gchar *string;
597   
598   if (!parser_data->attr_name)
599     return;
600
601   string = g_strndup (text, text_len);
602   errno = 0;
603   l = g_ascii_strtoll (string, &endptr, 0);
604   if (errno || endptr == string)
605     {
606       g_set_error (error, 
607                    GTK_BUILDER_ERROR,
608                    GTK_BUILDER_ERROR_INVALID_VALUE,
609                    "Could not parse integer `%s'",
610                    string);
611       g_free (string);
612       return;
613     }
614   g_free (string);
615
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;
621 }
622
623 static const GMarkupParser attributes_parser =
624   {
625     attributes_start_element,
626     NULL,
627     attributes_text_element,
628   };
629
630
631 /* Cell packing parsing */
632 static void
633 gtk_cell_layout_buildable_set_cell_property (GtkCellArea     *area,
634                                              GtkBuilder      *builder,
635                                              GtkCellRenderer *cell,
636                                              gchar           *name,
637                                              const gchar     *value)
638 {
639   GParamSpec *pspec;
640   GValue gvalue = { 0, };
641   GError *error = NULL;
642
643   pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
644   if (!pspec)
645     {
646       g_warning ("%s does not have a property called %s",
647                  g_type_name (G_OBJECT_TYPE (area)), name);
648       return;
649     }
650
651   if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
652     {
653       g_warning ("Could not read property %s:%s with value %s of type %s: %s",
654                  g_type_name (G_OBJECT_TYPE (area)),
655                  name,
656                  value,
657                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
658                  error->message);
659       g_error_free (error);
660       return;
661     }
662
663   gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
664   g_value_unset (&gvalue);
665 }
666
667 typedef struct {
668   GtkBuilder      *builder;
669   GtkCellLayout   *cell_layout;
670   GtkCellRenderer *renderer;
671   gchar           *cell_prop_name;
672   gchar           *context;
673   gboolean         translatable;
674 } CellPackingSubParserData;
675
676 static void
677 cell_packing_start_element (GMarkupParseContext *context,
678                             const gchar         *element_name,
679                             const gchar        **names,
680                             const gchar        **values,
681                             gpointer             user_data,
682                             GError             **error)
683 {
684   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
685   guint i;
686
687   if (strcmp (element_name, "property") == 0)
688     {
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)
693           {
694             if (!_gtk_builder_boolean_from_string (values[i],
695                                                    &parser_data->translatable,
696                                                    error))
697               return;
698           }
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]);
703         else
704           g_warning ("Unsupported attribute for GtkCellLayout Cell "
705                      "property: %s\n", names[i]);
706     }
707   else if (strcmp (element_name, "cell-packing") == 0)
708     return;
709   else
710     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
711 }
712
713 static void
714 cell_packing_text_element (GMarkupParseContext *context,
715                            const gchar         *text,
716                            gsize                text_len,
717                            gpointer             user_data,
718                            GError             **error)
719 {
720   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
721   GtkCellArea *area;
722   gchar* value;
723
724   if (!parser_data->cell_prop_name)
725     return;
726
727   if (parser_data->translatable && text_len)
728     {
729       const gchar* domain;
730       domain = gtk_builder_get_translation_domain (parser_data->builder);
731
732       value = _gtk_builder_parser_translate (domain,
733                                              parser_data->context,
734                                              text);
735     }
736   else
737     {
738       value = g_strdup (text);
739     }
740
741   area = gtk_cell_layout_get_area (parser_data->cell_layout);
742
743   if (!area)
744     {
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)));
747       return;
748     }
749
750   gtk_cell_layout_buildable_set_cell_property (area, 
751                                                parser_data->builder,
752                                                parser_data->renderer,
753                                                parser_data->cell_prop_name,
754                                                value);
755
756   g_free (parser_data->cell_prop_name);
757   g_free (parser_data->context);
758   g_free (value);
759   parser_data->cell_prop_name = NULL;
760   parser_data->context = NULL;
761   parser_data->translatable = FALSE;
762 }
763
764 static const GMarkupParser cell_packing_parser =
765   {
766     cell_packing_start_element,
767     NULL,
768     cell_packing_text_element,
769   };
770
771 gboolean
772 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable  *buildable,
773                                              GtkBuilder    *builder,
774                                              GObject       *child,
775                                              const gchar   *tagname,
776                                              GMarkupParser *parser,
777                                              gpointer      *data)
778 {
779   AttributesSubParserData  *attr_data;
780   CellPackingSubParserData *packing_data;
781
782   if (!child)
783     return FALSE;
784
785   if (strcmp (tagname, "attributes") == 0)
786     {
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;
791
792       *parser = attributes_parser;
793       *data = attr_data;
794       return TRUE;
795     }
796   else if (strcmp (tagname, "cell-packing") == 0)
797     {
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);
802
803       *parser = cell_packing_parser;
804       *data = packing_data;
805       return TRUE;
806     }
807
808   return FALSE;
809 }
810
811 gboolean
812 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
813                                            GtkBuilder   *builder,
814                                            GObject      *child,
815                                            const gchar  *tagname,
816                                            gpointer     *data)
817 {
818   AttributesSubParserData *attr_data;
819
820   if (strcmp (tagname, "attributes") == 0)
821     {
822       attr_data = (AttributesSubParserData*)data;
823       g_assert (!attr_data->attr_name);
824       g_slice_free (AttributesSubParserData, attr_data);
825       return TRUE;
826     }
827   else if (strcmp (tagname, "cell-packing") == 0)
828     {
829       g_slice_free (CellPackingSubParserData, (gpointer)data);
830       return TRUE;
831     }
832   return FALSE;
833 }
834
835 void
836 _gtk_cell_layout_buildable_add_child (GtkBuildable      *buildable,
837                                       GtkBuilder        *builder,
838                                       GObject           *child,
839                                       const gchar       *type)
840 {
841   g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
842   g_return_if_fail (GTK_IS_CELL_RENDERER (child));
843
844   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);
845 }