]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuildable.c
ee3934ec27adc9c617b24570abcd0a2718ceef8d
[~andy/gtk] / gtk / gtkbuildable.c
1 /* gtkbuildable.c
2  * Copyright (C) 2006-2007 Async Open Source,
3  *                         Johan Dahlin <jdahlin@async.com.br>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21
22 #include <config.h>
23 #include "gtkbuildable.h"
24 #include "gtktypeutils.h"
25 #include "gtkintl.h"
26 #include "gtkalias.h"
27
28 GType
29 gtk_buildable_get_type (void)
30 {
31   static GType buildable_type = 0;
32
33   if (!buildable_type)
34     buildable_type =
35       g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkBuildable"),
36                                      sizeof (GtkBuildableIface),
37                                      NULL, 0, NULL, 0);
38
39   return buildable_type;
40 }
41
42 /**
43  * gtk_buildable_set_name:
44  * @buildable: a #GtkBuildable
45  * @name: name to set
46  *
47  * Sets the name of the buildable object, it's used to synchronize the name
48  * if the object already has it's own concept of name.
49  *
50  * #GtkWidget implements this to map the buildable name to the widget name
51  *
52  * Since: 2.12
53  **/
54 void
55 gtk_buildable_set_name (GtkBuildable *buildable,
56                         const gchar  *name)
57 {
58   GtkBuildableIface *iface;
59
60   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
61   g_return_if_fail (name != NULL);
62
63   iface = GTK_BUILDABLE_GET_IFACE (buildable);
64
65   if (iface->set_name)
66     (* iface->set_name) (buildable, name);
67   else
68     g_object_set_data_full (G_OBJECT (buildable),
69                             "gtk-builder-name",
70                             g_strdup (name),
71                             g_free);
72 }
73
74 /**
75  * gtk_buildable_get_name:
76  * @buildable: a #GtkBuildable
77  *
78  * Returns: the buildable name, the name which was set in
79  * the <link linkend="BUILDER-UI">GtkBuilder UI definition</link> used to
80  * construct the @buildable.
81  *
82  * #GtkWidget implements this to map the buildable name to the widget name
83  *
84  * Since: 2.12
85  **/
86 const gchar *
87 gtk_buildable_get_name (GtkBuildable *buildable)
88 {
89   GtkBuildableIface *iface;
90
91   g_return_val_if_fail (GTK_IS_BUILDABLE (buildable), NULL);
92
93   iface = GTK_BUILDABLE_GET_IFACE (buildable);
94
95   if (iface->get_name)
96     return (* iface->get_name) (buildable);
97   else
98     return (const gchar*)g_object_get_data (G_OBJECT (buildable),
99                                             "gtk-builder-name");
100 }
101
102 /**
103  * gtk_buildable_add:
104  * @buildable: a #GtkBuildable
105  * @builder: a #GtkBuilder
106  * @child: child to add
107  * @type: kind of child or %NULL
108  *
109  * Add a child to a buildable. type is an optional string
110  * describing how the child should be added.
111  *
112  * #GtkContainer implements this to be able to add a child widget
113  * to the container. #GtkNotebook uses the @type to distinguish between
114  * page labels (@type = "page-label") and normal children.
115  *
116  * Since: 2.12
117  **/
118 void
119 gtk_buildable_add (GtkBuildable *buildable,
120                    GtkBuilder   *builder,
121                    GObject      *child,
122                    const gchar  *type)
123 {
124   GtkBuildableIface *iface;
125
126   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
127   g_return_if_fail (GTK_IS_BUILDER (builder));
128
129   iface = GTK_BUILDABLE_GET_IFACE (buildable);
130   g_return_if_fail (iface->add != NULL);
131
132   (* iface->add) (buildable, builder, child, type);
133 }
134
135 /**
136  * gtk_buildable_set_property:
137  * @buildable: a #GtkBuildable
138  * @builder: a #GtkBuilder
139  * @name: name of property
140  * @value: value of property
141  *
142  * Sets the property name @name to @value on the buildable object @buildable
143  * which is created by the @builder.
144  *
145  * This is optional to implement and is normally not needed.
146  * g_object_set_property() is used as a fallback.
147  *
148  * #GtkWindow implements this to delay showing (::visible) itself until
149  * the whole interface is fully created.
150  *
151  * Since: 2.12
152  **/
153 void
154 gtk_buildable_set_property (GtkBuildable *buildable,
155                             GtkBuilder   *builder,
156                             const gchar  *name,
157                             const GValue *value)
158 {
159   GtkBuildableIface *iface;
160
161   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
162   g_return_if_fail (GTK_IS_BUILDER (builder));
163   g_return_if_fail (name != NULL);
164   g_return_if_fail (value != NULL);
165
166   iface = GTK_BUILDABLE_GET_IFACE (buildable);
167   if (iface->set_property)
168     (* iface->set_property) (buildable, builder, name, value);
169   else
170     g_object_set_property (G_OBJECT (buildable), name, value);
171 }
172
173 /**
174  * gtk_buildable_parser_finished:
175  * @buildable: a #GtkBuildable
176  * @builder: a #GtkBuilder
177  *
178  * Finish the parsing of a <link linkend="BUILDER-UI">GtkBuilder UI definition</link>
179  * snippet. Note that this will be called once for each time gtk_builder_add_from_file or
180  * gtk_builder_add_from_string is called on a builder.
181  *
182  * #GtkWindow implements this to delay showing (::visible) itself until
183  * the whole interface is fully created.
184  *
185  * Since: 2.12
186  **/
187 void
188 gtk_buildable_parser_finished (GtkBuildable *buildable,
189                                GtkBuilder   *builder)
190 {
191   GtkBuildableIface *iface;
192
193   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
194   g_return_if_fail (GTK_IS_BUILDER (builder));
195
196   iface = GTK_BUILDABLE_GET_IFACE (buildable);
197   if (iface->parser_finished)
198     (* iface->parser_finished) (buildable, builder);
199 }
200
201 /**
202  * gtk_buildable_construct_child
203  * @buildable: A #GtkBuildable
204  * @builder: #GtkBuilder used to construct this object
205  * @name: name of child to construct
206  *
207  * Construct a child of @buildable with the name @name.
208  *
209  * #GtkUIManager implements this to reference to a widget created in a &lt;ui&gt; tag
210  * which is outside of the normal <link linkend="BUILDER-UI">GtkBuilder UI definition</link>
211  * object hierarchy.
212  *
213  * Since: 2.12
214  **/
215 GObject *
216 gtk_buildable_construct_child (GtkBuildable *buildable,
217                                GtkBuilder   *builder,
218                                const gchar  *name)
219 {
220   GtkBuildableIface *iface;
221
222   g_return_val_if_fail (GTK_IS_BUILDABLE (buildable), NULL);
223   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
224   g_return_val_if_fail (name != NULL, NULL);
225
226   iface = GTK_BUILDABLE_GET_IFACE (buildable);
227   g_return_val_if_fail (iface->construct_child != NULL, NULL);
228
229   return (* iface->construct_child) (buildable, builder, name);
230 }
231
232 /**
233  * gtk_buildable_custom_tag_start
234  * @buildable: a #GtkBuildable
235  * @builder: a #GtkBuilder used to construct this object
236  * @child: child object or %NULL for non-child tags
237  * @tagname: name of tag
238  * @parser: a #GMarkupParser structure
239  * @data: user data that will be passed in to parser functions
240  *
241  * This is called when an unknown tag under &lt;child&gt; tag is found.
242  * 
243  * Called when an unknown tag is present under a &lt;child&gt; tag.
244  * If the buildable implementation wishes to handle the tag it should
245  * return %TRUE and fill in the @parser structure. Remember to either
246  * implement custom_tag_end or custom_tag_finish to free
247  * the user data allocated here.
248  *
249  * #GtkWidget implements this and parsers all &lt;accelerator&gt; tags to
250  * keyboard accelerators.
251  * #GtkContainer implements this to map properties defined under
252  * &lt;packing&gt; tag to child properties.
253  *
254  * Returns: %TRUE if a object has a custom implementation, %FALSE
255  *          if it doesn't.
256  *
257  * Since: 2.12
258  **/
259 gboolean
260 gtk_buildable_custom_tag_start (GtkBuildable  *buildable,
261                                 GtkBuilder    *builder,
262                                 GObject       *child,
263                                 const gchar   *tagname,
264                                 GMarkupParser *parser,
265                                 gpointer      *data)
266 {
267   GtkBuildableIface *iface;
268
269   g_return_val_if_fail (GTK_IS_BUILDABLE (buildable), FALSE);
270   g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
271   g_return_val_if_fail (tagname != NULL, FALSE);
272
273   iface = GTK_BUILDABLE_GET_IFACE (buildable);
274   g_return_val_if_fail (iface->custom_tag_start != NULL, FALSE);
275
276   return (* iface->custom_tag_start) (buildable, builder, child,
277                                       tagname, parser, data);
278 }
279
280 /**
281  * gtk_buildable_custom_tag_end
282  * @buildable: A #GtkBuildable
283  * @builder: #GtkBuilder used to construct this object
284  * @child: child object or %NULL for non-child tags
285  * @tagname: name of tag
286  * @data: user data that will be passed in to parser functions
287  *
288  * This is called for each custom tag handled by the buildable.
289  * It will be called when the end of the tag is reached.
290  *
291  * Since: 2.12
292  **/
293 void
294 gtk_buildable_custom_tag_end (GtkBuildable  *buildable,
295                               GtkBuilder    *builder,
296                               GObject       *child,
297                               const gchar   *tagname,
298                               gpointer      *data)
299 {
300   GtkBuildableIface *iface;
301
302   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
303   g_return_if_fail (GTK_IS_BUILDER (builder));
304   g_return_if_fail (tagname != NULL);
305
306   iface = GTK_BUILDABLE_GET_IFACE (buildable);
307   if (iface->custom_tag_end)
308     (* iface->custom_tag_end) (buildable, builder, child, tagname, data);
309 }
310
311 /**
312  * gtk_buildable_custom_finished:
313  * @buildable: a #GtkBuildable
314  * @builder: a #GtkBuilder
315  * @child: child object or %NULL for non-child tags
316  * @tagname: the name of the tag
317  * @data: user data created in custom_tag_start
318  *
319  * This is similar to gtk_buildable_parser_finished() but is
320  * called once for each custom tag handled by the @buildable.
321  * 
322  * Since: 2.12
323  **/
324 void
325 gtk_buildable_custom_finished (GtkBuildable  *buildable,
326                                GtkBuilder    *builder,
327                                GObject       *child,
328                                const gchar   *tagname,
329                                gpointer       data)
330 {
331   GtkBuildableIface *iface;
332
333   g_return_if_fail (GTK_IS_BUILDABLE (buildable));
334   g_return_if_fail (GTK_IS_BUILDER (builder));
335
336   iface = GTK_BUILDABLE_GET_IFACE (buildable);
337   if (iface->custom_finished)
338     (* iface->custom_finished) (buildable, builder, child, tagname, data);
339 }
340
341 /**
342  * gtk_buildable_get_internal_child
343  * @buildable: a #GtkBuildable
344  * @builder: a #GtkBuilder
345  * @childname: name of child
346  *
347  * Get the internal child called @child of the @buildable object.
348  *
349  * Return: the internal child of the buildable object
350  *
351  * Since: 2.12
352  **/
353 GObject *
354 gtk_buildable_get_internal_child (GtkBuildable *buildable,
355                                   GtkBuilder   *builder,
356                                   const gchar  *childname)
357 {
358   GtkBuildableIface *iface;
359
360   g_return_val_if_fail (GTK_IS_BUILDABLE (buildable), NULL);
361   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
362   g_return_val_if_fail (childname != NULL, NULL);
363
364   iface = GTK_BUILDABLE_GET_IFACE (buildable);
365   if (!iface->get_internal_child)
366     return NULL;
367
368   return (* iface->get_internal_child) (buildable, builder, childname);
369 }
370
371 #define __GTK_BUILDABLE_C__
372 #include "gtkaliasdef.c"