1 /* Fo
2  * fo-property-background-image.c: 'background-image' property
3  *
4  * Copyright (C) 2001-2006 Sun Microsystems
5  * Copyright (C) 2007-2010 Menteith Consulting Ltd
6  * Copyright (C) 2011 Mentea
7  *
8  * See COPYING for the status of this software.
9  */
10 
11 #include <string.h>
12 #include "fo-utils.h"
13 #include "fo-context.h"
14 #include "datatype/fo-datatype.h"
15 #include "property/fo-property-private.h"
16 #include "property/fo-property-font-size.h"
17 #include "property/fo-property-background-image.h"
18 
19 /**
20  * SECTION:fo-property-background-image
21  * @short_description: 'background-image' property
22  *
23  * Inherited: FALSE
24  *
25  * Shorthand: FALSE
26  *
27  * Value: &lt;uri-specification> | none | inherit
28  *
29  * Initial value: none
30  *
31  * Definition: <ulink url="http://www.w3.org/TR/xsl11/&num;background-image">http://www.w3.org/TR/xsl11/&num;background-image</ulink>
32  */
33 
34 struct _FoPropertyBackgroundImage
35 {
36   FoProperty parent_instance;
37 };
38 
39 struct _FoPropertyBackgroundImageClass
40 {
41   FoPropertyClass parent_class;
42 };
43 
44 static void _init         (FoPropertyBackgroundImage      *property_background_image);
45 static void _class_init   (FoPropertyBackgroundImageClass *klass);
46 
47 static FoDatatype * _resolve_enum (const gchar *token,
48                                    FoContext   *context,
49                                    GError     **error);
50 static FoDatatype * _validate     (FoDatatype  *datatype,
51                                    FoContext   *context,
52                                    GError     **error);
53 
54 static const gchar class_name[] = "background-image";
55 static gpointer parent_class;
56 
57 /**
58  * fo_property_background_image_get_type:
59  *
60  * Register the #FoPropertyBackgroundImage type if not already registered and
61  * return its #GType value.
62  *
63  * Return value: #GType of #FoPropertyBackgroundImage.
64  **/
65 GType
fo_property_background_image_get_type(void)66 fo_property_background_image_get_type (void)
67 {
68   static GType object_type = 0;
69 
70   if (!object_type)
71     {
72       static const GTypeInfo object_info =
73       {
74         sizeof (FoPropertyBackgroundImageClass),
75         NULL,           /* base_init */
76         NULL,           /* base_finalize */
77         (GClassInitFunc) _class_init,
78         NULL,           /* class_finalize */
79         NULL,           /* class_data */
80         sizeof (FoPropertyBackgroundImage),
81         0,              /* n_preallocs */
82         (GInstanceInitFunc) _init,
83 	NULL		/* value_table */
84       };
85 
86       object_type = g_type_register_static (FO_TYPE_PROPERTY,
87                                             class_name,
88                                             &object_info, 0);
89     }
90 
91   return object_type;
92 }
93 
94 /**
95  * _init:
96  * @background_image: #FoPropertyBackgroundImage object to initialise.
97  *
98  * Implements #GInstanceInitFunc for #FoPropertyBackgroundImage.
99  **/
100 static void
_init(FoPropertyBackgroundImage * background_image)101 _init (FoPropertyBackgroundImage *background_image)
102 {
103   FO_PROPERTY (background_image)->value =
104     g_object_ref (fo_enum_factory_get_enum_by_value (FO_ENUM_ENUM_NONE));
105 }
106 
107 /**
108  * _class_init:
109  * @klass: #FoPropertyBackgroundImageClass object to initialise.
110  *
111  * Implements #GClassInitFunc for #FoPropertyBackgroundImageClass.
112  **/
113 static void
_class_init(FoPropertyBackgroundImageClass * klass)114 _class_init (FoPropertyBackgroundImageClass *klass)
115 {
116   GObjectClass *object_class = G_OBJECT_CLASS (klass);
117   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
118 
119   parent_class = g_type_class_peek_parent (klass);
120 
121 
122   property_class->is_inherited = FALSE;
123   property_class->is_shorthand = FALSE;
124   property_class->resolve_enum =
125     _resolve_enum;
126   property_class->validate =
127     _validate;
128   property_class->get_initial =
129     fo_property_background_image_get_initial;
130 }
131 
132 
133 /**
134  * fo_property_background_image_new:
135  *
136  * Creates a new #FoPropertyBackgroundImage initialized to default value.
137  *
138  * Return value: the new #FoPropertyBackgroundImage.
139  **/
140 FoProperty*
fo_property_background_image_new(void)141 fo_property_background_image_new (void)
142 {
143   FoProperty* background_image;
144 
145   background_image =
146     FO_PROPERTY (g_object_new (fo_property_background_image_get_type (),
147                                NULL));
148 
149   return background_image;
150 }
151 
152 /**
153  * _resolve_enum:
154  * @token:   Token from the XML attribute value to be evaluated as an
155  *           enumeration token.
156  * @context: #FoContext object from which to possibly inherit values.
157  * @error:   Information about any error that has occurred.
158  *
159  * Compare @token against the enumeration tokens that are valid for the
160  * current FO property.  If @token is valid, returns either an #FoEnum datatype
161  * representing the enumeration token or a different datatype representing
162  * the enumeration token's resolved value.  If @token is not valid,
163  * sets @error and returns NULL.
164  *
165  * Return value: Resolved enumeration value or NULL.
166  **/
167 static FoDatatype *
_resolve_enum(const gchar * token,FoContext * context,GError ** error)168 _resolve_enum (const gchar *token,
169                FoContext   *context,
170                GError     **error)
171 {
172   g_return_val_if_fail (token != NULL, NULL);
173   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
174   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
175 
176   if ((strcmp (token, "none") == 0))
177     {
178       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
179     }
180   else
181     {
182       g_set_error (error,
183 		   FO_FO_ERROR,
184 		   FO_FO_ERROR_ENUMERATION_TOKEN,
185 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
186 		   class_name,
187 		   token);
188       return NULL;
189     }
190 }
191 
192 /**
193  * _validate:
194  * @datatype: #FoDatatype to be validated against allowed datatypes and
195  *            values for current property.
196  * @context:  #FoContext object from which to possibly inherit values.
197  * @error:    Information about any error that has occurred.
198  *
199  * Validates @datatype against allowed values.  Returns @datatype, a
200  * replacement datatype value, or NULL if validation failed.
201  *
202  * Return value: Valid datatype value or NULL.
203  **/
204 FoDatatype*
_validate(FoDatatype * datatype,FoContext * context,GError ** error)205 _validate (FoDatatype *datatype,
206            FoContext  *context,
207            GError    **error)
208 {
209   FoDatatype *new_datatype;
210   GError     *tmp_error = NULL;
211   gchar      *token;
212 
213   g_return_val_if_fail (datatype != NULL, NULL);
214   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
215   g_return_val_if_fail (context != NULL, NULL);
216   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
217   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
218 
219   if (FO_IS_URI_SPECIFICATION (datatype))
220     {
221       return datatype;
222     }
223   else if (FO_IS_ENUM (datatype))
224     {
225       FoEnumEnum value = fo_enum_get_value (datatype);
226 
227       if ((value == FO_ENUM_ENUM_NONE))
228 	{
229 	  return datatype;
230 	}
231       else
232 	{
233 	  gchar *datatype_sprintf = fo_object_sprintf (datatype);
234 
235 	  g_set_error (error,
236 		       FO_FO_ERROR,
237 		       FO_FO_ERROR_ENUMERATION_TOKEN,
238 		       _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
239 		       class_name,
240 		       datatype_sprintf,
241 		       g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
242 
243 	  g_object_unref (datatype);
244 
245 	  g_free (datatype_sprintf);
246 
247 	  return NULL;
248 	}
249     }
250   else if (FO_IS_STRING (datatype))
251     {
252       token = fo_string_get_value (datatype);
253 
254       new_datatype =
255         _resolve_enum (token, context, &tmp_error);
256 
257       g_object_unref (datatype);
258 
259       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
260 
261       return new_datatype;
262     }
263   else if (FO_IS_NAME (datatype))
264     {
265       token = fo_name_get_value (datatype);
266 
267       new_datatype =
268         _resolve_enum (token, context, &tmp_error);
269 
270       g_object_unref (datatype);
271 
272       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
273 
274       return new_datatype;
275     }
276   else
277     {
278       gchar *datatype_sprintf = fo_object_sprintf (datatype);
279 
280       g_set_error (error,
281 		   FO_FO_ERROR,
282 		   FO_FO_ERROR_DATATYPE,
283 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
284 		   class_name,
285 		   datatype_sprintf,
286 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
287 
288       g_object_unref (datatype);
289 
290       g_free (datatype_sprintf);
291 
292       return NULL;
293     }
294 }
295 
296 /**
297  * fo_property_background_image_get_initial:
298  *
299  * Get an instance of the property with the correct initial value.
300  *
301  * Return value: An instance of the property.
302  **/
303 FoProperty*
fo_property_background_image_get_initial(void)304 fo_property_background_image_get_initial (void)
305 {
306   static FoProperty *background_image = NULL;
307 
308   if (background_image == NULL)
309     {
310       background_image =
311 	fo_property_background_image_new ();
312     }
313 
314   return background_image;
315 }
316