1 /* Fo
2  * fo-property-page-position.c: 'page-position' 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-page-position.h"
18 
19 /**
20  * SECTION:fo-property-page-position
21  * @short_description: 'page-position' property
22  *
23  * Inherited: FALSE
24  *
25  * Shorthand: FALSE
26  *
27  * Value: first | last | rest | any | inherit
28  *
29  * Initial value: any
30  *
31  * Definition: <ulink url="http://www.w3.org/TR/xsl11/&num;page-position">http://www.w3.org/TR/xsl11/&num;page-position</ulink>
32  */
33 
34 struct _FoPropertyPagePosition
35 {
36   FoProperty parent_instance;
37 };
38 
39 struct _FoPropertyPagePositionClass
40 {
41   FoPropertyClass parent_class;
42 };
43 
44 static void _init         (FoPropertyPagePosition      *property_page_position);
45 static void _class_init   (FoPropertyPagePositionClass *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[] = "page-position";
55 static gpointer parent_class;
56 
57 /**
58  * fo_property_page_position_get_type:
59  *
60  * Register the #FoPropertyPagePosition type if not already registered and
61  * return its #GType value.
62  *
63  * Return value: #GType of #FoPropertyPagePosition.
64  **/
65 GType
fo_property_page_position_get_type(void)66 fo_property_page_position_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 (FoPropertyPagePositionClass),
75         NULL,           /* base_init */
76         NULL,           /* base_finalize */
77         (GClassInitFunc) _class_init,
78         NULL,           /* class_finalize */
79         NULL,           /* class_data */
80         sizeof (FoPropertyPagePosition),
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  * @page_position: #FoPropertyPagePosition object to initialise.
97  *
98  * Implements #GInstanceInitFunc for #FoPropertyPagePosition.
99  **/
100 static void
_init(FoPropertyPagePosition * page_position)101 _init (FoPropertyPagePosition *page_position)
102 {
103   FO_PROPERTY (page_position)->value =
104     g_object_ref (fo_enum_factory_get_enum_by_value (FO_ENUM_ENUM_ANY));
105 }
106 
107 /**
108  * _class_init:
109  * @klass: #FoPropertyPagePositionClass object to initialise.
110  *
111  * Implements #GClassInitFunc for #FoPropertyPagePositionClass.
112  **/
113 static void
_class_init(FoPropertyPagePositionClass * klass)114 _class_init (FoPropertyPagePositionClass *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_page_position_get_initial;
130 }
131 
132 
133 /**
134  * fo_property_page_position_new:
135  *
136  * Creates a new #FoPropertyPagePosition initialized to default value.
137  *
138  * Return value: the new #FoPropertyPagePosition.
139  **/
140 FoProperty*
fo_property_page_position_new(void)141 fo_property_page_position_new (void)
142 {
143   FoProperty* page_position;
144 
145   page_position =
146     FO_PROPERTY (g_object_new (fo_property_page_position_get_type (),
147                                NULL));
148 
149   return page_position;
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, "first") == 0) ||
177       (strcmp (token, "last") == 0) ||
178       (strcmp (token, "rest") == 0) ||
179       (strcmp (token, "any") == 0))
180     {
181       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
182     }
183   else
184     {
185       g_set_error (error,
186 		   FO_FO_ERROR,
187 		   FO_FO_ERROR_ENUMERATION_TOKEN,
188 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
189 		   class_name,
190 		   token);
191       return NULL;
192     }
193 }
194 
195 /**
196  * _validate:
197  * @datatype: #FoDatatype to be validated against allowed datatypes and
198  *            values for current property.
199  * @context:  #FoContext object from which to possibly inherit values.
200  * @error:    Information about any error that has occurred.
201  *
202  * Validates @datatype against allowed values.  Returns @datatype, a
203  * replacement datatype value, or NULL if validation failed.
204  *
205  * Return value: Valid datatype value or NULL.
206  **/
207 FoDatatype*
_validate(FoDatatype * datatype,FoContext * context,GError ** error)208 _validate (FoDatatype *datatype,
209            FoContext  *context,
210            GError    **error)
211 {
212   FoDatatype *new_datatype;
213   GError     *tmp_error = NULL;
214   gchar      *token;
215 
216   g_return_val_if_fail (datatype != NULL, NULL);
217   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
218   g_return_val_if_fail (context != NULL, NULL);
219   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
220   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
221 
222   if (FO_IS_ENUM (datatype))
223     {
224       FoEnumEnum value = fo_enum_get_value (datatype);
225 
226       if ((value == FO_ENUM_ENUM_FIRST) ||
227           (value == FO_ENUM_ENUM_LAST) ||
228           (value == FO_ENUM_ENUM_REST) ||
229           (value == FO_ENUM_ENUM_ANY))
230 	{
231 	  return datatype;
232 	}
233       else
234 	{
235 	  gchar *datatype_sprintf = fo_object_sprintf (datatype);
236 
237 	  g_set_error (error,
238 		       FO_FO_ERROR,
239 		       FO_FO_ERROR_ENUMERATION_TOKEN,
240 		       _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
241 		       class_name,
242 		       datatype_sprintf,
243 		       g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
244 
245 	  g_object_unref (datatype);
246 
247 	  g_free (datatype_sprintf);
248 
249 	  return NULL;
250 	}
251     }
252   else if (FO_IS_STRING (datatype))
253     {
254       token = fo_string_get_value (datatype);
255 
256       new_datatype =
257         _resolve_enum (token, context, &tmp_error);
258 
259       g_object_unref (datatype);
260 
261       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
262 
263       return new_datatype;
264     }
265   else if (FO_IS_NAME (datatype))
266     {
267       token = fo_name_get_value (datatype);
268 
269       new_datatype =
270         _resolve_enum (token, context, &tmp_error);
271 
272       g_object_unref (datatype);
273 
274       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
275 
276       return new_datatype;
277     }
278   else
279     {
280       gchar *datatype_sprintf = fo_object_sprintf (datatype);
281 
282       g_set_error (error,
283 		   FO_FO_ERROR,
284 		   FO_FO_ERROR_DATATYPE,
285 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
286 		   class_name,
287 		   datatype_sprintf,
288 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
289 
290       g_object_unref (datatype);
291 
292       g_free (datatype_sprintf);
293 
294       return NULL;
295     }
296 }
297 
298 /**
299  * fo_property_page_position_get_initial:
300  *
301  * Get an instance of the property with the correct initial value.
302  *
303  * Return value: An instance of the property.
304  **/
305 FoProperty*
fo_property_page_position_get_initial(void)306 fo_property_page_position_get_initial (void)
307 {
308   static FoProperty *page_position = NULL;
309 
310   if (page_position == NULL)
311     {
312       page_position =
313 	fo_property_page_position_new ();
314     }
315 
316   return page_position;
317 }
318