]> Pileus Git - ~andy/gtk/blob - gtk/gtkcelllayout.c
Fixed GtkCellLayout to parse <cell-packing> property text accumulatively.
[~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 #define warn_no_cell_area(func)                                 \
97   g_critical ("%s: Called but no GtkCellArea is available yet", func)
98
99 typedef GtkCellLayoutIface GtkCellLayoutInterface;
100 G_DEFINE_INTERFACE (GtkCellLayout, gtk_cell_layout, G_TYPE_OBJECT);
101
102 static void   gtk_cell_layout_default_pack_start         (GtkCellLayout         *cell_layout,
103                                                           GtkCellRenderer       *cell,
104                                                           gboolean               expand);
105 static void   gtk_cell_layout_default_pack_end           (GtkCellLayout         *cell_layout,
106                                                           GtkCellRenderer       *cell,
107                                                           gboolean               expand);
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,
112                                                           gint                   column);
113 static void   gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
114                                                           GtkCellRenderer       *cell,
115                                                           GtkCellLayoutDataFunc  func,
116                                                           gpointer               func_data,
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,
122                                                           gint                   position);
123 static GList *gtk_cell_layout_default_get_cells          (GtkCellLayout         *cell_layout);
124
125
126 static void
127 gtk_cell_layout_default_init (GtkCellLayoutIface *iface)
128 {
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;
137 }
138
139 /* Default implementation is to fall back on an underlying cell area */
140 static void
141 gtk_cell_layout_default_pack_start (GtkCellLayout         *cell_layout,
142                                     GtkCellRenderer       *cell,
143                                     gboolean               expand)
144 {
145   GtkCellLayoutIface *iface;
146   GtkCellArea        *area;
147
148   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
149
150   if (iface->get_area)
151     {
152       area = iface->get_area (cell_layout);
153
154       if (area)
155         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
156       else
157         warn_no_cell_area ("GtkCellLayoutIface->pack_start()");
158     }
159 }
160
161 static void
162 gtk_cell_layout_default_pack_end (GtkCellLayout         *cell_layout,
163                                   GtkCellRenderer       *cell,
164                                   gboolean               expand)
165 {
166   GtkCellLayoutIface *iface;
167   GtkCellArea        *area;
168
169   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
170
171   if (iface->get_area)
172     {
173       area = iface->get_area (cell_layout);
174
175       if (area)
176         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
177       else
178         warn_no_cell_area ("GtkCellLayoutIface->pack_end()");
179     }
180 }
181
182 static void
183 gtk_cell_layout_default_clear (GtkCellLayout *cell_layout)
184 {
185   GtkCellLayoutIface *iface;
186   GtkCellArea        *area;
187
188   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
189
190   if (iface->get_area)
191     {
192       area = iface->get_area (cell_layout);
193
194       if (area)
195         gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
196       else
197         warn_no_cell_area ("GtkCellLayoutIface->clear()");
198     }
199 }
200
201 static void
202 gtk_cell_layout_default_add_attribute (GtkCellLayout         *cell_layout,
203                                        GtkCellRenderer       *cell,
204                                        const gchar           *attribute,
205                                        gint                   column)
206 {
207   GtkCellLayoutIface *iface;
208   GtkCellArea        *area;
209
210   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
211
212   if (iface->get_area)
213     {
214       area = iface->get_area (cell_layout);
215
216       if (area)
217         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
218       else
219         warn_no_cell_area ("GtkCellLayoutIface->add_attribute()");
220     }
221 }
222
223 static void
224 gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
225                                             GtkCellRenderer       *cell,
226                                             GtkCellLayoutDataFunc  func,
227                                             gpointer               func_data,
228                                             GDestroyNotify         destroy)
229 {
230   GtkCellLayoutIface *iface;
231   GtkCellArea        *area;
232
233   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
234
235   if (iface->get_area)
236     {
237       area = iface->get_area (cell_layout);
238
239       if (area)
240         _gtk_cell_area_set_cell_data_func_with_proxy (area, cell, 
241                                                       (GFunc)func, func_data, destroy, 
242                                                       cell_layout);
243       else
244         warn_no_cell_area ("GtkCellLayoutIface->set_cell_data_func()");
245     }
246 }
247
248 static void
249 gtk_cell_layout_default_clear_attributes (GtkCellLayout         *cell_layout,
250                                           GtkCellRenderer       *cell)
251 {
252   GtkCellLayoutIface *iface;
253   GtkCellArea        *area;
254
255   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
256
257   if (iface->get_area)
258     {
259       area = iface->get_area (cell_layout);
260
261       if (area)
262         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
263       else
264         warn_no_cell_area ("GtkCellLayoutIface->clear_attributes()");
265     }
266 }
267
268 static void
269 gtk_cell_layout_default_reorder (GtkCellLayout         *cell_layout,
270                                  GtkCellRenderer       *cell,
271                                  gint                   position)
272 {
273   GtkCellLayoutIface *iface;
274   GtkCellArea        *area;
275
276   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
277
278   if (iface->get_area)
279     {
280       area = iface->get_area (cell_layout);
281
282       if (area)
283         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
284       else
285         warn_no_cell_area ("GtkCellLayoutIface->reorder()");
286     }
287 }
288
289 static GList *
290 gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout)
291 {
292   GtkCellLayoutIface *iface;
293   GtkCellArea        *area;
294
295   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
296
297   if (iface->get_area)
298     {
299       area = iface->get_area (cell_layout);
300
301       if (area)
302         return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
303       else
304         warn_no_cell_area ("GtkCellLayoutIface->get_cells()");
305     }
306   return NULL;
307 }
308
309
310 /**
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
315  *
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.
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_start (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_start (cell_layout, cell, expand);
333 }
334
335 /**
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
340  *
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.
344  *
345  * Note that reusing the same cell renderer is not supported.
346  *
347  * Since: 2.4
348  */
349 void
350 gtk_cell_layout_pack_end (GtkCellLayout   *cell_layout,
351                           GtkCellRenderer *cell,
352                           gboolean         expand)
353 {
354   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
355   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
356
357   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_end (cell_layout, cell, expand);
358 }
359
360 /**
361  * gtk_cell_layout_clear:
362  * @cell_layout: a #GtkCellLayout
363  *
364  * Unsets all the mappings on all renderers on @cell_layout and
365  * removes all renderers from @cell_layout.
366  *
367  * Since: 2.4
368  */
369 void
370 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
371 {
372   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
373
374   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear (cell_layout);
375 }
376
377 static void
378 gtk_cell_layout_set_attributesv (GtkCellLayout   *cell_layout,
379                                  GtkCellRenderer *cell,
380                                  va_list          args)
381 {
382   gchar *attribute;
383   gint column;
384
385   attribute = va_arg (args, gchar *);
386
387   gtk_cell_layout_clear_attributes (cell_layout, cell);
388
389   while (attribute != NULL)
390     {
391       column = va_arg (args, gint);
392
393       gtk_cell_layout_add_attribute (cell_layout, cell, attribute, column);
394
395       attribute = va_arg (args, gchar *);
396     }
397 }
398
399 /**
400  * gtk_cell_layout_set_attributes:
401  * @cell_layout: a #GtkCellLayout
402  * @cell: a #GtkCellRenderer
403  * @Varargs: a %NULL-terminated list of attributes
404  *
405  * Sets the attributes in list as the attributes of @cell_layout.
406  *
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.
410  *
411  * Since: 2.4
412  */
413 void
414 gtk_cell_layout_set_attributes (GtkCellLayout   *cell_layout,
415                                 GtkCellRenderer *cell,
416                                 ...)
417 {
418   va_list args;
419
420   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
421   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
422
423   va_start (args, cell);
424   gtk_cell_layout_set_attributesv (cell_layout, cell, args);
425   va_end (args);
426 }
427
428 /**
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
434  *
435  * Adds an attribute mapping to the list in @cell_layout.
436  *
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.
441  *
442  * Since: 2.4
443  */
444 void
445 gtk_cell_layout_add_attribute (GtkCellLayout   *cell_layout,
446                                GtkCellRenderer *cell,
447                                const gchar     *attribute,
448                                gint             column)
449 {
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);
454
455   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->add_attribute (cell_layout, cell, attribute, column);
456 }
457
458 /**
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
465  *
466  * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
467  *
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.
471  *
472  * @func may be %NULL to remove a previously set function.
473  *
474  * Since: 2.4
475  */
476 void
477 gtk_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
478                                     GtkCellRenderer       *cell,
479                                     GtkCellLayoutDataFunc  func,
480                                     gpointer               func_data,
481                                     GDestroyNotify         destroy)
482 {
483   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
484   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
485
486   GTK_CELL_LAYOUT_GET_IFACE 
487     (cell_layout)->set_cell_data_func (cell_layout, cell, func, func_data, destroy);
488 }
489
490 /**
491  * gtk_cell_layout_clear_attributes:
492  * @cell_layout: a #GtkCellLayout
493  * @cell: a #GtkCellRenderer to clear the attribute mapping on
494  *
495  * Clears all existing attributes previously set with
496  * gtk_cell_layout_set_attributes().
497  *
498  * Since: 2.4
499  */
500 void
501 gtk_cell_layout_clear_attributes (GtkCellLayout   *cell_layout,
502                                   GtkCellRenderer *cell)
503 {
504   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
505   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
506
507   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear_attributes (cell_layout, cell);
508 }
509
510 /**
511  * gtk_cell_layout_reorder:
512  * @cell_layout: a #GtkCellLayout
513  * @cell: a #GtkCellRenderer to reorder
514  * @position: new position to insert @cell at
515  *
516  * Re-inserts @cell at @position.
517  *
518  * Note that @cell has already to be packed into @cell_layout
519  * for this to function properly.
520  *
521  * Since: 2.4
522  */
523 void
524 gtk_cell_layout_reorder (GtkCellLayout   *cell_layout,
525                          GtkCellRenderer *cell,
526                          gint             position)
527 {
528   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
529   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
530
531   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->reorder (cell_layout, cell, position);
532 }
533
534 /**
535  * gtk_cell_layout_get_cells:
536  * @cell_layout: a #GtkCellLayout
537  *
538  * Returns the cell renderers which have been added to @cell_layout.
539  *
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.
544  *
545  * Since: 2.12
546  */
547 GList *
548 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
549 {
550   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
551
552   return GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->get_cells (cell_layout);
553 }
554
555 /**
556  * gtk_cell_layout_get_area:
557  * @cell_layout: a #GtkCellLayout
558  *
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.
562  *
563  * Return value: (transfer none): the cell area used by @cell_layout.
564  *
565  * Since: 3.0
566  */
567 GtkCellArea *
568 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
569 {
570   GtkCellLayoutIface *iface;
571
572   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
573
574   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);  
575   if (iface->get_area)
576     return iface->get_area (cell_layout);
577
578   return NULL;
579 }
580
581 /* Attribute parsing */
582 typedef struct {
583   GtkCellLayout   *cell_layout;
584   GtkCellRenderer *renderer;
585   gchar           *attr_name;
586 } AttributesSubParserData;
587
588 static void
589 attributes_start_element (GMarkupParseContext *context,
590                           const gchar         *element_name,
591                           const gchar        **names,
592                           const gchar        **values,
593                           gpointer             user_data,
594                           GError             **error)
595 {
596   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
597   guint i;
598
599   if (strcmp (element_name, "attribute") == 0)
600     {
601       for (i = 0; names[i]; i++)
602         if (strcmp (names[i], "name") == 0)
603           parser_data->attr_name = g_strdup (values[i]);
604     }
605   else if (strcmp (element_name, "attributes") == 0)
606     return;
607   else
608     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
609 }
610
611 static void
612 attributes_text_element (GMarkupParseContext *context,
613                          const gchar         *text,
614                          gsize                text_len,
615                          gpointer             user_data,
616                          GError             **error)
617 {
618   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
619   glong l;
620   gchar *endptr;
621   gchar *string;
622   
623   if (!parser_data->attr_name)
624     return;
625
626   string = g_strndup (text, text_len);
627   errno = 0;
628   l = g_ascii_strtoll (string, &endptr, 0);
629   if (errno || endptr == string)
630     {
631       g_set_error (error, 
632                    GTK_BUILDER_ERROR,
633                    GTK_BUILDER_ERROR_INVALID_VALUE,
634                    "Could not parse integer `%s'",
635                    string);
636       g_free (string);
637       return;
638     }
639   g_free (string);
640
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;
646 }
647
648 static const GMarkupParser attributes_parser =
649   {
650     attributes_start_element,
651     NULL,
652     attributes_text_element,
653   };
654
655
656 /* Cell packing parsing */
657 static void
658 gtk_cell_layout_buildable_set_cell_property (GtkCellArea     *area,
659                                              GtkBuilder      *builder,
660                                              GtkCellRenderer *cell,
661                                              gchar           *name,
662                                              const gchar     *value)
663 {
664   GParamSpec *pspec;
665   GValue gvalue = { 0, };
666   GError *error = NULL;
667
668   pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
669   if (!pspec)
670     {
671       g_warning ("%s does not have a property called %s",
672                  g_type_name (G_OBJECT_TYPE (area)), name);
673       return;
674     }
675
676   if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
677     {
678       g_warning ("Could not read property %s:%s with value %s of type %s: %s",
679                  g_type_name (G_OBJECT_TYPE (area)),
680                  name,
681                  value,
682                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
683                  error->message);
684       g_error_free (error);
685       return;
686     }
687
688   gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
689   g_value_unset (&gvalue);
690 }
691
692 typedef struct {
693   GtkBuilder      *builder;
694   GtkCellLayout   *cell_layout;
695   GtkCellRenderer *renderer;
696   GString         *string;
697   gchar           *cell_prop_name;
698   gchar           *context;
699   gboolean         translatable;
700 } CellPackingSubParserData;
701
702 static void
703 cell_packing_start_element (GMarkupParseContext *context,
704                             const gchar         *element_name,
705                             const gchar        **names,
706                             const gchar        **values,
707                             gpointer             user_data,
708                             GError             **error)
709 {
710   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
711   guint i;
712
713   if (strcmp (element_name, "property") == 0)
714     {
715       for (i = 0; names[i]; i++)
716         if (strcmp (names[i], "name") == 0)
717           parser_data->cell_prop_name = g_strdup (values[i]);
718         else if (strcmp (names[i], "translatable") == 0)
719           {
720             if (!_gtk_builder_boolean_from_string (values[i],
721                                                    &parser_data->translatable,
722                                                    error))
723               return;
724           }
725         else if (strcmp (names[i], "comments") == 0)
726           ; /* for translators */
727         else if (strcmp (names[i], "context") == 0)
728           parser_data->context = g_strdup (values[i]);
729         else
730           g_warning ("Unsupported attribute for GtkCellLayout Cell "
731                      "property: %s\n", names[i]);
732     }
733   else if (strcmp (element_name, "cell-packing") == 0)
734     return;
735   else
736     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
737 }
738
739 static void
740 cell_packing_text_element (GMarkupParseContext *context,
741                            const gchar         *text,
742                            gsize                text_len,
743                            gpointer             user_data,
744                            GError             **error)
745 {
746   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
747
748   if (parser_data->cell_prop_name)
749     g_string_append_len (parser_data->string, text, text_len);
750 }
751
752 static void
753 cell_packing_end_element (GMarkupParseContext *context,
754                           const gchar         *element_name,
755                           gpointer             user_data,
756                           GError             **error)
757 {
758   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
759   GtkCellArea *area;
760
761   /* Append the translated strings */
762   if (parser_data->string->len)
763     {
764       area = gtk_cell_layout_get_area (parser_data->cell_layout);
765
766       if (area)
767         {
768           if (parser_data->translatable)
769             {
770               gchar *translated;
771               const gchar* domain;
772
773               domain = gtk_builder_get_translation_domain (parser_data->builder);
774
775               translated = _gtk_builder_parser_translate (domain,
776                                                           parser_data->context,
777                                                           parser_data->string->str);
778               g_string_set_size (parser_data->string, 0);
779               g_string_append (parser_data->string, translated);
780             }
781
782           gtk_cell_layout_buildable_set_cell_property (area, 
783                                                        parser_data->builder,
784                                                        parser_data->renderer,
785                                                        parser_data->cell_prop_name,
786                                                        parser_data->string->str);
787         }
788       else
789         g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
790                    g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
791     }
792
793   g_string_set_size (parser_data->string, 0);
794   g_free (parser_data->cell_prop_name);
795   g_free (parser_data->context);
796   parser_data->cell_prop_name = NULL;
797   parser_data->context = NULL;
798   parser_data->translatable = FALSE;
799 }
800
801
802 static const GMarkupParser cell_packing_parser =
803   {
804     cell_packing_start_element,
805     cell_packing_end_element,
806     cell_packing_text_element,
807   };
808
809 gboolean
810 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable  *buildable,
811                                              GtkBuilder    *builder,
812                                              GObject       *child,
813                                              const gchar   *tagname,
814                                              GMarkupParser *parser,
815                                              gpointer      *data)
816 {
817   AttributesSubParserData  *attr_data;
818   CellPackingSubParserData *packing_data;
819
820   if (!child)
821     return FALSE;
822
823   if (strcmp (tagname, "attributes") == 0)
824     {
825       attr_data = g_slice_new0 (AttributesSubParserData);
826       attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
827       attr_data->renderer = GTK_CELL_RENDERER (child);
828       attr_data->attr_name = NULL;
829
830       *parser = attributes_parser;
831       *data = attr_data;
832       return TRUE;
833     }
834   else if (strcmp (tagname, "cell-packing") == 0)
835     {
836       packing_data = g_slice_new0 (CellPackingSubParserData);
837       packing_data->string = g_string_new ("");
838       packing_data->builder = builder;
839       packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
840       packing_data->renderer = GTK_CELL_RENDERER (child);
841
842       *parser = cell_packing_parser;
843       *data = packing_data;
844       return TRUE;
845     }
846
847   return FALSE;
848 }
849
850 gboolean
851 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
852                                            GtkBuilder   *builder,
853                                            GObject      *child,
854                                            const gchar  *tagname,
855                                            gpointer     *data)
856 {
857   AttributesSubParserData *attr_data;
858   CellPackingSubParserData *packing_data;
859
860   if (strcmp (tagname, "attributes") == 0)
861     {
862       attr_data = (AttributesSubParserData*)data;
863       g_assert (!attr_data->attr_name);
864       g_slice_free (AttributesSubParserData, attr_data);
865       return TRUE;
866     }
867   else if (strcmp (tagname, "cell-packing") == 0)
868     {
869       packing_data = (CellPackingSubParserData *)data;
870       g_string_free (packing_data->string, TRUE);
871       g_slice_free (CellPackingSubParserData, packing_data);
872       return TRUE;
873     }
874   return FALSE;
875 }
876
877 void
878 _gtk_cell_layout_buildable_add_child (GtkBuildable      *buildable,
879                                       GtkBuilder        *builder,
880                                       GObject           *child,
881                                       const gchar       *type)
882 {
883   g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
884   g_return_if_fail (GTK_IS_CELL_RENDERER (child));
885
886   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);
887 }