Release Clutter 1.11.4 (snapshot)
[profile/ivi/clutter.git] / clutter / clutter-scriptable.c
1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Authored By Matthew Allum  <mallum@openedhand.com>
7  *             Emmanuele Bassi  <ebassi@openedhand.com>
8  *
9  * Copyright (C) 2006 OpenedHand
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23  *
24  *
25  */
26
27 /**
28  * SECTION:clutter-scriptable
29  * @short_description: Override the UI definition parsing
30  *
31  * The #ClutterScriptableIface interface exposes the UI definition parsing
32  * process to external classes. By implementing this interface, a class can
33  * override the UI definition parsing and transform complex data types into
34  * GObject properties, or allow custom properties.
35  *
36  * #ClutterScriptable is available since Clutter 0.6
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <string.h>
44 #include <stdlib.h>
45
46 #include <glib.h>
47
48 #include "clutter-scriptable.h"
49 #include "clutter-script-private.h"
50
51 #include "clutter-private.h"
52 #include "clutter-debug.h"
53
54 typedef ClutterScriptableIface  ClutterScriptableInterface;
55
56 G_DEFINE_INTERFACE (ClutterScriptable, clutter_scriptable, G_TYPE_OBJECT);
57
58 static void
59 clutter_scriptable_default_init (ClutterScriptableInterface *iface)
60 {
61 }
62
63 /**
64  * clutter_scriptable_set_id:
65  * @scriptable: a #ClutterScriptable
66  * @id_: the #ClutterScript id of the object
67  *
68  * Sets @id_ as the unique Clutter script it for this instance of
69  * #ClutterScriptableIface.
70  *
71  * This name can be used by user interface designer applications to
72  * define a unique name for an object constructable using the UI
73  * definition language parsed by #ClutterScript.
74  *
75  * Since: 0.6
76  */
77 void
78 clutter_scriptable_set_id (ClutterScriptable *scriptable,
79                            const gchar       *id_)
80 {
81   ClutterScriptableIface *iface;
82
83   g_return_if_fail (CLUTTER_IS_SCRIPTABLE (scriptable));
84   g_return_if_fail (id_ != NULL);
85
86   iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);
87   if (iface->set_id)
88     iface->set_id (scriptable, id_);
89   else
90     g_object_set_data_full (G_OBJECT (scriptable),
91                             "clutter-script-id",
92                             g_strdup (id_),
93                             g_free);
94 }
95
96 /**
97  * clutter_scriptable_get_id:
98  * @scriptable: a #ClutterScriptable
99  *
100  * Retrieves the id of @scriptable set using clutter_scriptable_set_id().
101  *
102  * Return value: the id of the object. The returned string is owned by
103  *   the scriptable object and should never be modified of freed
104  *
105  * Since: 0.6
106  */
107 const gchar *
108 clutter_scriptable_get_id (ClutterScriptable *scriptable)
109 {
110   ClutterScriptableIface *iface;
111
112   g_return_val_if_fail (CLUTTER_IS_SCRIPTABLE (scriptable), NULL);
113
114   iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);
115   if (iface->get_id)
116     return iface->get_id (scriptable);
117   else
118     return g_object_get_data (G_OBJECT (scriptable), "clutter-script-id");
119 }
120
121 /**
122  * clutter_scriptable_parse_custom_node:
123  * @scriptable: a #ClutterScriptable
124  * @script: the #ClutterScript creating the scriptable instance
125  * @value: the generic value to be set
126  * @name: the name of the node
127  * @node: the JSON node to be parsed
128  *
129  * Parses the passed JSON node. The implementation must set the type
130  * of the passed #GValue pointer using g_value_init().
131  *
132  * Return value: %TRUE if the node was successfully parsed, %FALSE otherwise.
133  *
134  * Since: 0.6
135  */
136 gboolean
137 clutter_scriptable_parse_custom_node (ClutterScriptable *scriptable,
138                                       ClutterScript     *script,
139                                       GValue            *value,
140                                       const gchar       *name,
141                                       JsonNode          *node)
142 {
143   ClutterScriptableIface *iface;
144
145   g_return_val_if_fail (CLUTTER_IS_SCRIPTABLE (scriptable), FALSE);
146   g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
147   g_return_val_if_fail (name != NULL, FALSE);
148   g_return_val_if_fail (node != NULL, FALSE);
149
150   iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);
151   if (iface->parse_custom_node)
152     return iface->parse_custom_node (scriptable, script, value, name, node);
153
154   return FALSE;
155 }
156
157 /**
158  * clutter_scriptable_set_custom_property:
159  * @scriptable: a #ClutterScriptable
160  * @script: the #ClutterScript creating the scriptable instance
161  * @name: the name of the property
162  * @value: the value of the property
163  *
164  * Overrides the common properties setting. The underlying virtual
165  * function should be used when implementing custom properties.
166  *
167  * Since: 0.6
168  */
169 void
170 clutter_scriptable_set_custom_property (ClutterScriptable *scriptable,
171                                         ClutterScript     *script,
172                                         const gchar       *name,
173                                         const GValue      *value)
174 {
175   ClutterScriptableIface *iface;
176
177   g_return_if_fail (CLUTTER_IS_SCRIPTABLE (scriptable));
178   g_return_if_fail (CLUTTER_IS_SCRIPT (script));
179   g_return_if_fail (name != NULL);
180   g_return_if_fail (value != NULL);
181
182   iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);
183   if (iface->set_custom_property)
184     iface->set_custom_property (scriptable, script, name, value);
185 }