]> Pileus Git - ~andy/gtk/blob - gtk/gtkcelllayout.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gtkcelllayout
20  * @Short_Description: An interface for packing cells
21  * @Title: GtkCellLayout
22  *
23  * #GtkCellLayout is an interface to be implemented by all objects which
24  * want to provide a #GtkTreeViewColumn<!-- -->-like API for packing cells, setting
25  * attributes and data funcs.
26  *
27  * One of the notable features provided by implementations of GtkCellLayout
28  * are <emphasis>attributes</emphasis>. Attributes let you set the properties
29  * in flexible ways. They can just be set to constant values like regular
30  * properties. But they can also be mapped to a column of the underlying
31  * tree model with gtk_cell_layout_set_attributes(), which means that the value
32  * of the attribute can change from cell to cell as they are rendered by the
33  * cell renderer. Finally, it is possible to specify a function with
34  * gtk_cell_layout_set_cell_data_func() that is called to determine the value
35  * of the attribute for each cell that is rendered.
36  *
37  * <refsect2 id="GtkCellLayout-BUILDER-UI">
38  * <title>GtkCellLayouts as GtkBuildable</title>
39  * <para>
40  * Implementations of GtkCellLayout which also implement the GtkBuildable
41  * interface (#GtkCellView, #GtkIconView, #GtkComboBox,
42  * #GtkEntryCompletion, #GtkTreeViewColumn) accept GtkCellRenderer objects
43  * as &lt;child&gt; elements in UI definitions. They support a custom
44  * &lt;attributes&gt; element for their children, which can contain
45  * multiple &lt;attribute&gt; elements. Each &lt;attribute&gt; element has
46  * a name attribute which specifies a property of the cell renderer; the
47  * content of the element is the attribute value.
48  *
49  * <example>
50  * <title>A UI definition fragment specifying attributes</title>
51  * <programlisting><![CDATA[
52  * <object class="GtkCellView">
53  *   <child>
54  *     <object class="GtkCellRendererText"/>
55  *     <attributes>
56  *       <attribute name="text">0</attribute>
57  *     </attributes>
58  *   </child>"
59  * </object>
60  * ]]></programlisting>
61  * </example>
62  *
63  * Furthermore for implementations of GtkCellLayout that use a #GtkCellArea
64  * to lay out cells (all GtkCellLayouts in GTK+ use a GtkCellArea)
65  * <link linkend="cell-properties">cell properties</link> can also be defined
66  * in the format by specifying the custom &lt;cell-packing&gt; attribute which
67  * can contain multiple &lt;property&gt; elements defined in the normal way.
68  * <example>
69  * <title>A UI definition fragment specifying cell properties</title>
70  * <programlisting><![CDATA[
71  * <object class="GtkTreeViewColumn">
72  *   <child>
73  *     <object class="GtkCellRendererText"/>
74  *     <cell-packing>
75  *       <property name="align">True</property>
76  *       <property name="expand">False</property>
77  *     </cell-packing>
78  *   </child>"
79  * </object>
80  * ]]></programlisting>
81  * </example>
82  * </para>
83  * </refsect2>
84  *
85  * <refsect2>
86  * <title>Subclassing GtkCellLayout implementations</title>
87  * <para>
88  * When subclassing a widget that implements #GtkCellLayout like
89  * #GtkIconView or #GtkComboBox, there are some considerations related
90  * to the fact that these widgets internally use a #GtkCellArea.
91  * The cell area is exposed as a construct-only property by these
92  * widgets. This means that it is possible to e.g. do
93  * <informalexample><programlisting>
94  * combo = g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", my_cell_area, NULL);
95  * </programlisting></informalexample>
96  * to use a custom cell area with a combo box. But construct properties
97  * are only initialized <emphasis>after</emphasis> instance init()
98  * functions have run, which means that using functions which rely on
99  * the existence of the cell area in your subclass' init() function will
100  * cause the default cell area to be instantiated. In this case, a provided
101  * construct property value will be ignored (with a warning, to alert
102  * you to the problem).
103  * <informalexample><programlisting>
104  * static void
105  * my_combo_box_init (MyComboBox *b)
106  * {
107  *   GtkCellRenderer *cell;
108  *
109  *   cell = gtk_cell_renderer_pixbuf_new ();
110  *   /&ast; The following call causes the default cell area for combo boxes,
111  *    &ast; a GtkCellAreaBox, to be instantiated
112  *    &ast;/
113  *   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (b), cell, FALSE);
114  *   ...
115  * }
116  *
117  * GtkWidget *
118  * my_combo_box_new (GtkCellArea *area)
119  * {
120  *   /&ast; This call is going to cause a warning
121  *    &ast; about area being ignored
122  *    &ast;/
123  *   return g_object_new (MY_TYPE_COMBO_BOX, "cell-area", area, NULL);
124  * }
125  * </programlisting></informalexample>
126  * If supporting alternative cell areas with your derived widget is
127  * not important, then this does not have to concern you. If you want
128  * to support alternative cell areas, you can do so by moving the
129  * problematic calls out of init() and into a constructor()
130  * for your class.
131  * </para>
132  * </refsect2>
133  */
134
135 #include "config.h"
136 #include <string.h>
137 #include <stdlib.h>
138 #include <errno.h>
139 #include "gtkcelllayout.h"
140 #include "gtkbuilderprivate.h"
141 #include "gtkintl.h"
142
143 #define warn_no_cell_area(func)                                 \
144   g_critical ("%s: Called but no GtkCellArea is available yet", func)
145
146 typedef GtkCellLayoutIface GtkCellLayoutInterface;
147 G_DEFINE_INTERFACE (GtkCellLayout, gtk_cell_layout, G_TYPE_OBJECT);
148
149 static void   gtk_cell_layout_default_pack_start         (GtkCellLayout         *cell_layout,
150                                                           GtkCellRenderer       *cell,
151                                                           gboolean               expand);
152 static void   gtk_cell_layout_default_pack_end           (GtkCellLayout         *cell_layout,
153                                                           GtkCellRenderer       *cell,
154                                                           gboolean               expand);
155 static void   gtk_cell_layout_default_clear              (GtkCellLayout         *cell_layout);
156 static void   gtk_cell_layout_default_add_attribute      (GtkCellLayout         *cell_layout,
157                                                           GtkCellRenderer       *cell,
158                                                           const gchar           *attribute,
159                                                           gint                   column);
160 static void   gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
161                                                           GtkCellRenderer       *cell,
162                                                           GtkCellLayoutDataFunc  func,
163                                                           gpointer               func_data,
164                                                           GDestroyNotify         destroy);
165 static void   gtk_cell_layout_default_clear_attributes   (GtkCellLayout         *cell_layout,
166                                                           GtkCellRenderer       *cell);
167 static void   gtk_cell_layout_default_reorder            (GtkCellLayout         *cell_layout,
168                                                           GtkCellRenderer       *cell,
169                                                           gint                   position);
170 static GList *gtk_cell_layout_default_get_cells          (GtkCellLayout         *cell_layout);
171
172
173 static void
174 gtk_cell_layout_default_init (GtkCellLayoutIface *iface)
175 {
176   iface->pack_start         = gtk_cell_layout_default_pack_start;
177   iface->pack_end           = gtk_cell_layout_default_pack_end;
178   iface->clear              = gtk_cell_layout_default_clear;
179   iface->add_attribute      = gtk_cell_layout_default_add_attribute;
180   iface->set_cell_data_func = gtk_cell_layout_default_set_cell_data_func;
181   iface->clear_attributes   = gtk_cell_layout_default_clear_attributes;
182   iface->reorder            = gtk_cell_layout_default_reorder;
183   iface->get_cells          = gtk_cell_layout_default_get_cells;
184 }
185
186 /* Default implementation is to fall back on an underlying cell area */
187 static void
188 gtk_cell_layout_default_pack_start (GtkCellLayout         *cell_layout,
189                                     GtkCellRenderer       *cell,
190                                     gboolean               expand)
191 {
192   GtkCellLayoutIface *iface;
193   GtkCellArea        *area;
194
195   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
196
197   if (iface->get_area)
198     {
199       area = iface->get_area (cell_layout);
200
201       if (area)
202         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (area), cell, expand);
203       else
204         warn_no_cell_area ("GtkCellLayoutIface->pack_start()");
205     }
206 }
207
208 static void
209 gtk_cell_layout_default_pack_end (GtkCellLayout         *cell_layout,
210                                   GtkCellRenderer       *cell,
211                                   gboolean               expand)
212 {
213   GtkCellLayoutIface *iface;
214   GtkCellArea        *area;
215
216   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
217
218   if (iface->get_area)
219     {
220       area = iface->get_area (cell_layout);
221
222       if (area)
223         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (area), cell, expand);
224       else
225         warn_no_cell_area ("GtkCellLayoutIface->pack_end()");
226     }
227 }
228
229 static void
230 gtk_cell_layout_default_clear (GtkCellLayout *cell_layout)
231 {
232   GtkCellLayoutIface *iface;
233   GtkCellArea        *area;
234
235   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
236
237   if (iface->get_area)
238     {
239       area = iface->get_area (cell_layout);
240
241       if (area)
242         gtk_cell_layout_clear (GTK_CELL_LAYOUT (area));
243       else
244         warn_no_cell_area ("GtkCellLayoutIface->clear()");
245     }
246 }
247
248 static void
249 gtk_cell_layout_default_add_attribute (GtkCellLayout         *cell_layout,
250                                        GtkCellRenderer       *cell,
251                                        const gchar           *attribute,
252                                        gint                   column)
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       if (area)
264         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (area), cell, attribute, column);
265       else
266         warn_no_cell_area ("GtkCellLayoutIface->add_attribute()");
267     }
268 }
269
270 static void
271 gtk_cell_layout_default_set_cell_data_func (GtkCellLayout         *cell_layout,
272                                             GtkCellRenderer       *cell,
273                                             GtkCellLayoutDataFunc  func,
274                                             gpointer               func_data,
275                                             GDestroyNotify         destroy)
276 {
277   GtkCellLayoutIface *iface;
278   GtkCellArea        *area;
279
280   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
281
282   if (iface->get_area)
283     {
284       area = iface->get_area (cell_layout);
285
286       if (area)
287         _gtk_cell_area_set_cell_data_func_with_proxy (area, cell, 
288                                                       (GFunc)func, func_data, destroy, 
289                                                       cell_layout);
290       else
291         warn_no_cell_area ("GtkCellLayoutIface->set_cell_data_func()");
292     }
293 }
294
295 static void
296 gtk_cell_layout_default_clear_attributes (GtkCellLayout         *cell_layout,
297                                           GtkCellRenderer       *cell)
298 {
299   GtkCellLayoutIface *iface;
300   GtkCellArea        *area;
301
302   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
303
304   if (iface->get_area)
305     {
306       area = iface->get_area (cell_layout);
307
308       if (area)
309         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (area), cell);
310       else
311         warn_no_cell_area ("GtkCellLayoutIface->clear_attributes()");
312     }
313 }
314
315 static void
316 gtk_cell_layout_default_reorder (GtkCellLayout         *cell_layout,
317                                  GtkCellRenderer       *cell,
318                                  gint                   position)
319 {
320   GtkCellLayoutIface *iface;
321   GtkCellArea        *area;
322
323   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
324
325   if (iface->get_area)
326     {
327       area = iface->get_area (cell_layout);
328
329       if (area)
330         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (area), cell, position);
331       else
332         warn_no_cell_area ("GtkCellLayoutIface->reorder()");
333     }
334 }
335
336 static GList *
337 gtk_cell_layout_default_get_cells (GtkCellLayout *cell_layout)
338 {
339   GtkCellLayoutIface *iface;
340   GtkCellArea        *area;
341
342   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);
343
344   if (iface->get_area)
345     {
346       area = iface->get_area (cell_layout);
347
348       if (area)
349         return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
350       else
351         warn_no_cell_area ("GtkCellLayoutIface->get_cells()");
352     }
353   return NULL;
354 }
355
356
357 /**
358  * gtk_cell_layout_pack_start:
359  * @cell_layout: a #GtkCellLayout
360  * @cell: a #GtkCellRenderer
361  * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
362  *
363  * Packs the @cell into the beginning of @cell_layout. If @expand is %FALSE,
364  * then the @cell is allocated no more space than it needs. Any unused space
365  * is divided evenly between cells for which @expand is %TRUE.
366  *
367  * Note that reusing the same cell renderer is not supported.
368  *
369  * Since: 2.4
370  */
371 void
372 gtk_cell_layout_pack_start (GtkCellLayout   *cell_layout,
373                             GtkCellRenderer *cell,
374                             gboolean         expand)
375 {
376   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
377   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
378
379   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_start (cell_layout, cell, expand);
380 }
381
382 /**
383  * gtk_cell_layout_pack_end:
384  * @cell_layout: a #GtkCellLayout
385  * @cell: a #GtkCellRenderer
386  * @expand: %TRUE if @cell is to be given extra space allocated to @cell_layout
387  *
388  * Adds the @cell to the end of @cell_layout. If @expand is %FALSE, then the
389  * @cell is allocated no more space than it needs. Any unused space is
390  * divided evenly between cells for which @expand is %TRUE.
391  *
392  * Note that reusing the same cell renderer is not supported.
393  *
394  * Since: 2.4
395  */
396 void
397 gtk_cell_layout_pack_end (GtkCellLayout   *cell_layout,
398                           GtkCellRenderer *cell,
399                           gboolean         expand)
400 {
401   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
402   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
403
404   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->pack_end (cell_layout, cell, expand);
405 }
406
407 /**
408  * gtk_cell_layout_clear:
409  * @cell_layout: a #GtkCellLayout
410  *
411  * Unsets all the mappings on all renderers on @cell_layout and
412  * removes all renderers from @cell_layout.
413  *
414  * Since: 2.4
415  */
416 void
417 gtk_cell_layout_clear (GtkCellLayout *cell_layout)
418 {
419   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
420
421   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear (cell_layout);
422 }
423
424 static void
425 gtk_cell_layout_set_attributesv (GtkCellLayout   *cell_layout,
426                                  GtkCellRenderer *cell,
427                                  va_list          args)
428 {
429   gchar *attribute;
430   gint column;
431
432   attribute = va_arg (args, gchar *);
433
434   gtk_cell_layout_clear_attributes (cell_layout, cell);
435
436   while (attribute != NULL)
437     {
438       column = va_arg (args, gint);
439
440       gtk_cell_layout_add_attribute (cell_layout, cell, attribute, column);
441
442       attribute = va_arg (args, gchar *);
443     }
444 }
445
446 /**
447  * gtk_cell_layout_set_attributes:
448  * @cell_layout: a #GtkCellLayout
449  * @cell: a #GtkCellRenderer
450  * @...: a %NULL-terminated list of attributes
451  *
452  * Sets the attributes in list as the attributes of @cell_layout.
453  *
454  * The attributes should be in attribute/column order, as in
455  * gtk_cell_layout_add_attribute(). All existing attributes are
456  * removed, and replaced with the new attributes.
457  *
458  * Since: 2.4
459  */
460 void
461 gtk_cell_layout_set_attributes (GtkCellLayout   *cell_layout,
462                                 GtkCellRenderer *cell,
463                                 ...)
464 {
465   va_list args;
466
467   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
468   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
469
470   va_start (args, cell);
471   gtk_cell_layout_set_attributesv (cell_layout, cell, args);
472   va_end (args);
473 }
474
475 /**
476  * gtk_cell_layout_add_attribute:
477  * @cell_layout: a #GtkCellLayout
478  * @cell: a #GtkCellRenderer
479  * @attribute: an attribute on the renderer
480  * @column: the column position on the model to get the attribute from
481  *
482  * Adds an attribute mapping to the list in @cell_layout.
483  *
484  * The @column is the column of the model to get a value from, and the
485  * @attribute is the parameter on @cell to be set from the value. So for
486  * example if column 2 of the model contains strings, you could have the
487  * "text" attribute of a #GtkCellRendererText get its values from column 2.
488  *
489  * Since: 2.4
490  */
491 void
492 gtk_cell_layout_add_attribute (GtkCellLayout   *cell_layout,
493                                GtkCellRenderer *cell,
494                                const gchar     *attribute,
495                                gint             column)
496 {
497   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
498   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
499   g_return_if_fail (attribute != NULL);
500   g_return_if_fail (column >= 0);
501
502   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->add_attribute (cell_layout, cell, attribute, column);
503 }
504
505 /**
506  * gtk_cell_layout_set_cell_data_func:
507  * @cell_layout: a #GtkCellLayout
508  * @cell: a #GtkCellRenderer
509  * @func: (allow-none): the #GtkCellLayoutDataFunc to use, or %NULL
510  * @func_data: user data for @func
511  * @destroy: destroy notify for @func_data
512  *
513  * Sets the #GtkCellLayoutDataFunc to use for @cell_layout.
514  *
515  * This function is used instead of the standard attributes mapping
516  * for setting the column value, and should set the value of @cell_layout's
517  * cell renderer(s) as appropriate.
518  *
519  * @func may be %NULL to remove a previously set function.
520  *
521  * Since: 2.4
522  */
523 void
524 gtk_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
525                                     GtkCellRenderer       *cell,
526                                     GtkCellLayoutDataFunc  func,
527                                     gpointer               func_data,
528                                     GDestroyNotify         destroy)
529 {
530   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
531   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
532
533   GTK_CELL_LAYOUT_GET_IFACE 
534     (cell_layout)->set_cell_data_func (cell_layout, cell, func, func_data, destroy);
535 }
536
537 /**
538  * gtk_cell_layout_clear_attributes:
539  * @cell_layout: a #GtkCellLayout
540  * @cell: a #GtkCellRenderer to clear the attribute mapping on
541  *
542  * Clears all existing attributes previously set with
543  * gtk_cell_layout_set_attributes().
544  *
545  * Since: 2.4
546  */
547 void
548 gtk_cell_layout_clear_attributes (GtkCellLayout   *cell_layout,
549                                   GtkCellRenderer *cell)
550 {
551   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
552   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
553
554   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->clear_attributes (cell_layout, cell);
555 }
556
557 /**
558  * gtk_cell_layout_reorder:
559  * @cell_layout: a #GtkCellLayout
560  * @cell: a #GtkCellRenderer to reorder
561  * @position: new position to insert @cell at
562  *
563  * Re-inserts @cell at @position.
564  *
565  * Note that @cell has already to be packed into @cell_layout
566  * for this to function properly.
567  *
568  * Since: 2.4
569  */
570 void
571 gtk_cell_layout_reorder (GtkCellLayout   *cell_layout,
572                          GtkCellRenderer *cell,
573                          gint             position)
574 {
575   g_return_if_fail (GTK_IS_CELL_LAYOUT (cell_layout));
576   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
577
578   GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->reorder (cell_layout, cell, position);
579 }
580
581 /**
582  * gtk_cell_layout_get_cells:
583  * @cell_layout: a #GtkCellLayout
584  *
585  * Returns the cell renderers which have been added to @cell_layout.
586  *
587  * Return value: (element-type GtkCellRenderer) (transfer container):
588  *     a list of cell renderers. The list, but not the renderers has
589  *     been newly allocated and should be freed with g_list_free()
590  *     when no longer needed.
591  *
592  * Since: 2.12
593  */
594 GList *
595 gtk_cell_layout_get_cells (GtkCellLayout *cell_layout)
596 {
597   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
598
599   return GTK_CELL_LAYOUT_GET_IFACE (cell_layout)->get_cells (cell_layout);
600 }
601
602 /**
603  * gtk_cell_layout_get_area:
604  * @cell_layout: a #GtkCellLayout
605  *
606  * Returns the underlying #GtkCellArea which might be @cell_layout
607  * if called on a #GtkCellArea or might be %NULL if no #GtkCellArea
608  * is used by @cell_layout.
609  *
610  * Return value: (transfer none): the cell area used by @cell_layout.
611  *
612  * Since: 3.0
613  */
614 GtkCellArea *
615 gtk_cell_layout_get_area (GtkCellLayout *cell_layout)
616 {
617   GtkCellLayoutIface *iface;
618
619   g_return_val_if_fail (GTK_IS_CELL_LAYOUT (cell_layout), NULL);
620
621   iface = GTK_CELL_LAYOUT_GET_IFACE (cell_layout);  
622   if (iface->get_area)
623     return iface->get_area (cell_layout);
624
625   return NULL;
626 }
627
628 /* Attribute parsing */
629 typedef struct {
630   GtkCellLayout   *cell_layout;
631   GtkCellRenderer *renderer;
632   gchar           *attr_name;
633 } AttributesSubParserData;
634
635 static void
636 attributes_start_element (GMarkupParseContext *context,
637                           const gchar         *element_name,
638                           const gchar        **names,
639                           const gchar        **values,
640                           gpointer             user_data,
641                           GError             **error)
642 {
643   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
644   guint i;
645
646   if (strcmp (element_name, "attribute") == 0)
647     {
648       for (i = 0; names[i]; i++)
649         if (strcmp (names[i], "name") == 0)
650           parser_data->attr_name = g_strdup (values[i]);
651     }
652   else if (strcmp (element_name, "attributes") == 0)
653     return;
654   else
655     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
656 }
657
658 static void
659 attributes_text_element (GMarkupParseContext *context,
660                          const gchar         *text,
661                          gsize                text_len,
662                          gpointer             user_data,
663                          GError             **error)
664 {
665   AttributesSubParserData *parser_data = (AttributesSubParserData*)user_data;
666   glong l;
667   gchar *endptr;
668   gchar *string;
669   
670   if (!parser_data->attr_name)
671     return;
672
673   string = g_strndup (text, text_len);
674   errno = 0;
675   l = g_ascii_strtoll (string, &endptr, 0);
676   if (errno || endptr == string)
677     {
678       g_set_error (error, 
679                    GTK_BUILDER_ERROR,
680                    GTK_BUILDER_ERROR_INVALID_VALUE,
681                    "Could not parse integer `%s'",
682                    string);
683       g_free (string);
684       return;
685     }
686   g_free (string);
687
688   gtk_cell_layout_add_attribute (parser_data->cell_layout,
689                                  parser_data->renderer,
690                                  parser_data->attr_name, l);
691   g_free (parser_data->attr_name);
692   parser_data->attr_name = NULL;
693 }
694
695 static const GMarkupParser attributes_parser =
696   {
697     attributes_start_element,
698     NULL,
699     attributes_text_element,
700   };
701
702
703 /* Cell packing parsing */
704 static void
705 gtk_cell_layout_buildable_set_cell_property (GtkCellArea     *area,
706                                              GtkBuilder      *builder,
707                                              GtkCellRenderer *cell,
708                                              gchar           *name,
709                                              const gchar     *value)
710 {
711   GParamSpec *pspec;
712   GValue gvalue = G_VALUE_INIT;
713   GError *error = NULL;
714
715   pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_GET_CLASS (area), name);
716   if (!pspec)
717     {
718       g_warning ("%s does not have a property called %s",
719                  g_type_name (G_OBJECT_TYPE (area)), name);
720       return;
721     }
722
723   if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
724     {
725       g_warning ("Could not read property %s:%s with value %s of type %s: %s",
726                  g_type_name (G_OBJECT_TYPE (area)),
727                  name,
728                  value,
729                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
730                  error->message);
731       g_error_free (error);
732       return;
733     }
734
735   gtk_cell_area_cell_set_property (area, cell, name, &gvalue);
736   g_value_unset (&gvalue);
737 }
738
739 typedef struct {
740   GtkBuilder      *builder;
741   GtkCellLayout   *cell_layout;
742   GtkCellRenderer *renderer;
743   GString         *string;
744   gchar           *cell_prop_name;
745   gchar           *context;
746   gboolean         translatable;
747 } CellPackingSubParserData;
748
749 static void
750 cell_packing_start_element (GMarkupParseContext *context,
751                             const gchar         *element_name,
752                             const gchar        **names,
753                             const gchar        **values,
754                             gpointer             user_data,
755                             GError             **error)
756 {
757   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
758   guint i;
759
760   if (strcmp (element_name, "property") == 0)
761     {
762       for (i = 0; names[i]; i++)
763         if (strcmp (names[i], "name") == 0)
764           parser_data->cell_prop_name = g_strdup (values[i]);
765         else if (strcmp (names[i], "translatable") == 0)
766           {
767             if (!_gtk_builder_boolean_from_string (values[i],
768                                                    &parser_data->translatable,
769                                                    error))
770               return;
771           }
772         else if (strcmp (names[i], "comments") == 0)
773           ; /* for translators */
774         else if (strcmp (names[i], "context") == 0)
775           parser_data->context = g_strdup (values[i]);
776         else
777           g_warning ("Unsupported attribute for GtkCellLayout Cell "
778                      "property: %s\n", names[i]);
779     }
780   else if (strcmp (element_name, "cell-packing") == 0)
781     return;
782   else
783     g_warning ("Unsupported tag for GtkCellLayout: %s\n", element_name);
784 }
785
786 static void
787 cell_packing_text_element (GMarkupParseContext *context,
788                            const gchar         *text,
789                            gsize                text_len,
790                            gpointer             user_data,
791                            GError             **error)
792 {
793   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
794
795   if (parser_data->cell_prop_name)
796     g_string_append_len (parser_data->string, text, text_len);
797 }
798
799 static void
800 cell_packing_end_element (GMarkupParseContext *context,
801                           const gchar         *element_name,
802                           gpointer             user_data,
803                           GError             **error)
804 {
805   CellPackingSubParserData *parser_data = (CellPackingSubParserData*)user_data;
806   GtkCellArea *area;
807
808   area = gtk_cell_layout_get_area (parser_data->cell_layout);
809
810   if (area)
811     {
812       /* translate the string */
813       if (parser_data->string->len && parser_data->translatable)
814         {
815           gchar *translated;
816           const gchar* domain;
817
818           domain = gtk_builder_get_translation_domain (parser_data->builder);
819
820           translated = _gtk_builder_parser_translate (domain,
821                                                       parser_data->context,
822                                                       parser_data->string->str);
823           g_string_set_size (parser_data->string, 0);
824           g_string_append (parser_data->string, translated);
825         }
826
827       if (parser_data->cell_prop_name)
828         gtk_cell_layout_buildable_set_cell_property (area, 
829                                                      parser_data->builder,
830                                                      parser_data->renderer,
831                                                      parser_data->cell_prop_name,
832                                                      parser_data->string->str);
833     }
834   else
835     g_warning ("%s does not have an internal GtkCellArea class and cannot apply child cell properties",
836                g_type_name (G_OBJECT_TYPE (parser_data->cell_layout)));
837
838   g_string_set_size (parser_data->string, 0);
839   g_free (parser_data->cell_prop_name);
840   g_free (parser_data->context);
841   parser_data->cell_prop_name = NULL;
842   parser_data->context = NULL;
843   parser_data->translatable = FALSE;
844 }
845
846
847 static const GMarkupParser cell_packing_parser =
848   {
849     cell_packing_start_element,
850     cell_packing_end_element,
851     cell_packing_text_element,
852   };
853
854 gboolean
855 _gtk_cell_layout_buildable_custom_tag_start (GtkBuildable  *buildable,
856                                              GtkBuilder    *builder,
857                                              GObject       *child,
858                                              const gchar   *tagname,
859                                              GMarkupParser *parser,
860                                              gpointer      *data)
861 {
862   AttributesSubParserData  *attr_data;
863   CellPackingSubParserData *packing_data;
864
865   if (!child)
866     return FALSE;
867
868   if (strcmp (tagname, "attributes") == 0)
869     {
870       attr_data = g_slice_new0 (AttributesSubParserData);
871       attr_data->cell_layout = GTK_CELL_LAYOUT (buildable);
872       attr_data->renderer = GTK_CELL_RENDERER (child);
873       attr_data->attr_name = NULL;
874
875       *parser = attributes_parser;
876       *data = attr_data;
877       return TRUE;
878     }
879   else if (strcmp (tagname, "cell-packing") == 0)
880     {
881       packing_data = g_slice_new0 (CellPackingSubParserData);
882       packing_data->string = g_string_new ("");
883       packing_data->builder = builder;
884       packing_data->cell_layout = GTK_CELL_LAYOUT (buildable);
885       packing_data->renderer = GTK_CELL_RENDERER (child);
886
887       *parser = cell_packing_parser;
888       *data = packing_data;
889       return TRUE;
890     }
891
892   return FALSE;
893 }
894
895 gboolean
896 _gtk_cell_layout_buildable_custom_tag_end (GtkBuildable *buildable,
897                                            GtkBuilder   *builder,
898                                            GObject      *child,
899                                            const gchar  *tagname,
900                                            gpointer     *data)
901 {
902   AttributesSubParserData *attr_data;
903   CellPackingSubParserData *packing_data;
904
905   if (strcmp (tagname, "attributes") == 0)
906     {
907       attr_data = (AttributesSubParserData*)data;
908       g_assert (!attr_data->attr_name);
909       g_slice_free (AttributesSubParserData, attr_data);
910       return TRUE;
911     }
912   else if (strcmp (tagname, "cell-packing") == 0)
913     {
914       packing_data = (CellPackingSubParserData *)data;
915       g_string_free (packing_data->string, TRUE);
916       g_slice_free (CellPackingSubParserData, packing_data);
917       return TRUE;
918     }
919   return FALSE;
920 }
921
922 void
923 _gtk_cell_layout_buildable_add_child (GtkBuildable      *buildable,
924                                       GtkBuilder        *builder,
925                                       GObject           *child,
926                                       const gchar       *type)
927 {
928   g_return_if_fail (GTK_IS_CELL_LAYOUT (buildable));
929   g_return_if_fail (GTK_IS_CELL_RENDERER (child));
930
931   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (buildable), GTK_CELL_RENDERER (child), FALSE);
932 }